Variable Value Not Changing
BlitzPlus Forums/BlitzPlus Programming/Variable Value Not Changing
| ||
The variable playernum tells the computer if its 1 or 2 player but for some reason once you choose the first time the variable wont change when you go back to the menu and choose a different gamemode. Does anyone know why? Here Is the source code: Graphics 600,800,64,2 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") ScaleImage paddle1, .5, .5 ScaleImage paddle2, .5, .5 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() If result = 4 Then End 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 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(250,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 = 300 ball\y = 400 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 ball\x = 300 ball\xv = Rand(-8,8) ball\yv = Rand(-8,8) Flip Delay 1000 EndIf If ball\y >= 800 Then Text 300,400,"Computer Scores" comp\score = comp\score + 1 ball\y = 400 ball\x = 300 ball\xv = Rand(-8,8) ball\yv = Rand(-8,8) 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 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 MainMenu() End Function Thanks |
| ||
Learn to debug your code with these functions : Variable% = rand(1,10) debuglog("Variable = "+Variable) flushkeys() waitkey() or Variable% = rand(1,10) print("Variable = "+Variable) flushkeys() waitkey() Each time a variable does not changes as you want, you can use these functions to know exactly how a variable changes at each modification in the code. Good luck ! |
| ||
Where is playernum set to 1 or 2? |
| ||
You have set playernum to global....then used it as a parameter in a function header indicating that within that function it will be local. |