Variable not working right

BlitzPlus Forums/BlitzPlus Programming/Variable not working right

nyybaseball(Posted 2014) [#1]
Why is the variable playernum always 1? Heres the programs

Graphics 600,800
SetBuffer BackBuffer()
font=LoadFont("Arial",15)
SetFont font
SeedRnd MilliSecs()
AutoMidHandle True
Global paddle1 = LoadImage("BPaddle.bmp")
Global paddle2 = LoadImage("Rpaddle.bmp")
Global puck = LoadImage("Puck.bmp")
Global player1 = LoadImage("1Player.bmp")
Global player2 = LoadImage("2Player.bmp")
Global mouse = LoadImage("Mouse.bmp")
Global controls = LoadImage("Controls.bmp")
Global options = LoadImage("Options.bmp")
Global table = LoadImage ("HockeyTable.bmp")
ScaleImage paddle1, .5, .5
ScaleImage paddle2, .5, .5
ScaleImage player1, .3, .3
ScaleImage player2, .3, .3
ScaleImage mouse, .4, .4
ScaleImage controls, .2, .2
ScaleImage options, .2, .2
Global playernum

Type puck
Field x,y
Field xv,yv
End Type

Global ball.puck = New puck
ball\x = 300
ball\y = 400
ball\xv = Rand(-8,5)
ball\yv = Rand(-8,5)

Type user
Field x,y
Field score
End Type

Global player.user = New user
player\x = 300
player\y = 750
player\score = 0

Type enemy
Field x,y
Field xv,yv
Field score
End Type

Global comp.enemy = New enemy
comp\x = 300
comp\y = 50
comp\score = 0
Global result

While result=0
result=MainMenu()
;put these options inside the menu loop, as these
;return 0 - continuing the mainmenu loop.
If result=2 Then result=StartGame(2)
If result=3 Then result=help()
Wend
If result=1 Then StartGame(1)
;if the result isn't 1, then it must be 4 - hence QUIT
End

Function MainMenu()
player\score = 0
comp\score = 0
result = 0
Cls
Text 300,50,"Main Menu",1,1
Rect 250,100,150,50,0
Text 300,125,"Single Player",1,1
Rect 250,200,150,50,0
Text 300,225,"Multiplayer",1,1
Rect 250,300,150,50,0
Text 300,325,"Help",1,1
Rect 250,400,150,50,0
Text 300,425,"Quit",1,1
Flip
result=0
While result=0

mx=MouseX():my=MouseY()
If MouseHit(1) Then
If RectsOverlap(250,100,150,50,mx,my,1,1) Then result = 1
If RectsOverlap(250,200,150,50,mx,my,1,1) Then result = 2
If RectsOverlap(200,300,150,50,mx,my,1,1) Then result = 3
If RectsOverlap(200,400,150,40,mx,my,1,1) Then result = 4
End If
Wend
Return result
End Function

Function Options()
Cls
Text 400,300,"Options Screen! Any key to return to main menu",1,1
Flip
WaitKey()
Return 0
End Function

Function Credits()
Cls
Text 400,300,"Credits!!",1,1
Text 400,330,"Made by BlackD",1,1
Text 400,400,"Any key to return",1,1
Flip
WaitKey()
Return 0
End Function

Function StartGame(playernum)
While Not KeyDown(16)
Cls

If player\score >= 7 Then
gameover(1)
ElseIf comp\score >= 7 Then
gameover(2)
EndIf
drawHUD()
testinput()
updateball()
testballwithwalls()
testballwithpaddle()
If playernum = 1 Then
singleplayer()
ElseIf playernum = 2 Then
multiplayer()
EndIf


DrawImage puck, ball\x, ball\y
DrawImage paddle1,player\x, player\y
DrawImage paddle2, comp\x, comp\y
Text 10, 70,"playernum " + playernum
Text 10,85,"result " + result

Flip
Delay 10
Wend

End Function

Function drawhud()

Text 10,10,"Player score: " + player\score
Text 10,20, "Computer Score: " + comp\score

End Function

Function testinput()

If KeyDown(203) Then
player\x = player\x - 10
If player\x <= 10 Then
player\x = 10
EndIf
ElseIf KeyDown(205) Then
player\x = player\x +10
If player\x >=590 Then
player\x = 590
EndIf
EndIf
If KeyDown(50) Then
MainMenu()
EndIf
If KeyDown(19) Then
ball\x = 400
ball\y = 300
ball\xv = Rand(-8,8)
ball\yv = Rand(-8,8)
EndIf
If KeyHit(1) Or KeyDown(1) Then
Cls
Text 10,10,"Press Any Key To unpause"
WaitKey
EndIf

End Function

Function updateball()
ball\x = ball\x + ball\xv
ball\y = ball\y + ball\yv



End Function

Function testballwithwalls()

If ball\x < 0 Then
ball\xv = -ball\xv + Rand(-2,2)
ElseIf ball\y <= 0 Then
Text 300,400,"Player Scores"
player\score = player\score + 1
ball\y = 400
Flip
Delay 1000
EndIf

If ball\y >= 800 Then
Text 300,400,"Computer Scores"
comp\score = comp\score + 1
ball\y = 400
Flip
Delay 1000
EndIf

If ball\x >= 600 Then

ball\xv = -ball\xv + Rand(-2,2)

EndIf

End Function

Function testballwithpaddle()

If ImagesCollide(puck, ball\x, ball\y,0, paddle1,player\x, player\y,0) Then

ball\yv = -ball\yv+Rand(-2,2)
ElseIf ImagesCollide(puck, ball\x, ball\y,0, paddle2, comp\x, comp\y,0)

ball\yv = -ball\yv+Rand(-2,2)
EndIf

End Function

Function testai()

If KeyDown(30)

comp\x = comp\x - 10

ElseIf KeyDown(32) Then

comp\x = comp\x + 10


EndIf

End Function
Function gameover(winner)
If winner = 1 Then
Text 300,400,"Game Over. Player Wins"
Flip
WaitKey
EndIf
If winner = 2 Then
Text 300,400,"Game Over. Computer Wins"
Flip
WaitKey
EndIf
Delay 2000
playernum = 0
result = 0
MainMenu()
End Function

Function multiplayer()

If KeyDown(30)

comp\x = comp\x - 10

ElseIf KeyDown(32) Then

comp\x = comp\x + 10

EndIf

End Function

Function singleplayer()

If comp\x < ball\x Then
comp\x = comp\x + 10
ElseIf comp\x > ball\x Then
comp\x = comp\x - 10
EndIf

End Function

Function menu()
player\score = 0
comp\score = 0
While Not MouseHit(1)
Cls
DrawImage player1,300,250
DrawImage player2, 300,350
DrawImage controls, 300, 430
DrawImage options, 300,500
DrawImage mouse, MouseX(), MouseY()
Flip
If ImagesOverlap(mouse, MouseX(), MouseY(), player1, 300,250) And MouseHit(1)
playernum = 2
ElseIf ImagesOverlap(mouse, MouseX(), MouseY(), player2, 300,350) And MouseHit(1)
playernum = 1
ElseIf ImagesOverlap(mouse, MouseX(), MouseY(), controls, 300,430) And MouseHit(1)
help()
Else
Text 0,0," "
EndIf
Wend
End Function

Function help()

Cls

Text 250,300,"A = Top player move left"
Text 250,315,"D = top player move right"
Text 250,330,"Left Arrow = bottom player move left"
Text 250,345,"Right arrow = bottom player move right"
Text 250,360, "ESC = Pause"
Text 250,375,"M = Menu"
Text 250,390,"Click to go back to menu"
Flip

Delay 10
WaitKey
menu()

End Function


H&K(Posted 2014) [#2]
Im not saying this is the reason, however.

Function menu()
.
While Not MouseHit(1)
..
If ImagesOverlap(mouse, MouseX(), MouseY(), player1, 300,250) And MouseHit(1)
playernum = 2
ElseIf ImagesOverlap(mouse, MouseX(), MouseY(), player2, 300,350) And MouseHit(1)
playernum = 1
ElseIf ImagesOverlap(mouse, MouseX(), MouseY(), controls, 300,430) And MouseHit(1)
help()
Else 
Text 0,0," "
EndIf 
Wend
Reads,
IF No One has hit the Mouse, check to see where the mouse is, and if any one has hit it.

Have One "MouseHit" this should be in a specific place, which I think is just after a Flip
If it hasn't been hit don't bother checking where it is, just background and screen update
The while wend needs a better condition (On release IF there is a EXIT/POP from loop command in B+, Id "repeat forever"), But atm stick a keyDown in there.