Flip command not working

BlitzPlus Forums/BlitzPlus Programming/Flip command not working

nyybaseball(Posted 2014) [#1]
In the following code, if you start playing "Singleplayer" or "Multiplayer", after a little while the screen will start flashing black. Does anyone know why? Thanks.

source code:

Graphics 600,800,64,2

SetBuffer BackBuffer()

SeedRnd MilliSecs()

AutoMidHandle True

Global bluepaddle = LoadImage("BluePaddle.bmp")

Global redpaddle = LoadImage("RedPaddle.bmp")

Global puck = LoadImage("Puck.bmp")

Type puck
Field x,y
Field xv, yv
End Type
Global ball.puck = New puck

ball\x = 300
ball\y = 400
ball\xv = Rand(-5,5)
ball\yv = Rand(-5,5)

Type player1
Field x,y
Field score
Field wins
Field losses
End Type

Global player.player1 = New player1

player\x = 400
player\y = 750
player\score = 0
player\wins = 0
player\losses = 0

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

Global comp.enemy = New enemy

comp\x = 400
comp\y = 50
comp\score = 0
comp\wins = 0
comp\losses = 0


mainmenu()

Function startgame(playernum)

While Not KeyDown(1)

Cls

testinput()
drawhud()
updateball()
TestBallWithWalls()
TestBallWithPaddle()
If playernum = 1 Then
testAI()
ElseIf playernum = 2 Then
testinput2()
EndIf

DrawImage puck, ball\x, ball\y
DrawImage bluepaddle, player\x, player\y
DrawImage redpaddle, comp\x, comp\y
Flip

Delay 10

Wend

End

End Function

Function testinput()

If KeyDown(203) Then
player\x = player\x - 10
EndIf

If KeyDown(205) Then
player\x = player\x + 10
EndIf

If KeyDown(25) Then
Cls
Text 400,400,"Press Any Key To Unpause"
Flip
WaitKey()
EndIf

If KeyDown(19) Then
ball\x = 300
ball\y = 400
ball\xv = Rand(-5,5)
ball\yv = Rand(-5,5)
EndIf

If KeyDown(50) Then
mainmenu()
EndIf

End Function

Function drawhud()

Text 10,10,"Player Score: " + player\score
Text 10,20,"Computer Score: " + comp\score
Flip
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)
EndIf

If ball\x >= 600 Then
ball\xv = -ball\xv + Rand(-2,2)
EndIf

If ball\y <= 0 Then

Text 300,400,"Player Scores"
Flip
player\score = player\score + 1
Delay 1000

ball\x = 300
ball\y = 400
ball\xv = Rand(-5,5)
ball\yv = Rand(-5,5)
EndIf

If ball\y >= 800 Then

Text 300,400,"Computer Scores"
Flip
comp\score = comp\score + 1
Delay 1000

ball\x = 300
ball\y = 400
ball\xv = Rand(-5,5)
ball\yv = Rand(-5,5)
EndIf
End Function

Function TestBallWithPaddle()

If ImagesOverlap(puck,ball\x, ball\y,bluepaddle,player\x, player\y) Then
ball\yv = -ball\yv + Rand(-5,5)
EndIf
If ImagesOverlap(puck, ball\x, ball\y, redpaddle, comp\x, comp\y) Then
ball\yv = -ball\yv + Rand(-5,5)
EndIf

End Function

Function testAI()

If comp\x < ball\x Then
comp\x = comp\x + 5
EndIf

If comp\x > ball\x Then
comp\x = comp\x - 5
EndIf

End Function

Function mainmenu()
Cls
player\score = 0
comp\score = 0
ball\xv = Rand(-5,5)
ballyv = Rand(-5,5)
ball\x = 300
ball\y = 400

result = menu()

If result = 1 Then
playernum = 1
startgame(playernum)
EndIf

If result = 2 Then
playernum = 2
startgame(playernum)
EndIf

If result = 3 Then help()

If result = 4 Then credits()

If result = 5 Then End

End Function

Function menu()

Cls

Rect 200,100,200,50,0
Text 230,125,"Single Player"

Rect 200,250,200,50,0
Text 230,265,"Multiplayer"

Rect 200,360,200,50,0
Text 230,375,"Help"

Rect 200,470,200,50,0
Text 230,485,"Credits"

Rect 200,580,200,50,0
Text 230,600,"Quit"
Flip

While result = 0

If MouseHit(1) Then
If RectsOverlap(200,100,200,50,MouseX(),MouseY(),1,1) Then result = 1

If RectsOverlap(200,250,200,50,MouseX(),MouseY(),1,1) Then result = 2

If RectsOverlap(200,360,200,50,MouseX(),MouseY(),1,1) Then result = 3

If RectsOverlap(200,470,200,50,MouseX(),MouseY(),1,1) Then result = 4

If RectsOverlap(200,580,200,50,MouseX(),MouseY(),1,1) Then result = 5

EndIf

Wend

Return result

End Function

Function help()
Cls
Text 100,100,"Multiplayer Player"
Text 250,110,"A = Top Player Left"
Text 250,130,"D = Top Player Right"
Text 250,150,"Left Arrow = Bottom Player Left"
Text 250,170,"Right Arrow = Bottom Player Right"
Text 100,250,"Single Player"
Text 250,260,"Right Arrow = Move Right"
Text 250,290,"Left Arrow = Move Left"
Text 100,310,"Independant"
Text 200,320,"P = Pause"
Text 200,340,"M = Menu"
Text 200,360,"R resets Ball In Random Direction"
Text 200,380,"ESC = Quit"
Text 200,450,"Press Any Key To Go Back"
Flip
WaitKey
mainmenu()
End Function

Function credits()
Cls
Color 23,34,76
Text 100,400,"Made By Daniel Mellas At VWCC on October 13, 2014"
Flip
Color 255,255,255
WaitKey
mainmenu()
End Function

Function testinput2()

If KeyDown(30) Then
comp\x = comp\x -10
EndIf

If KeyDown(32) Then
comp\x = comp\x + 10
EndIf

End Function


Floyd(Posted 2014) [#2]
You would have better luck getting help if you posted code that people can actually run. That means no external images, sounds etc.

In general you would want double buffering to go something like this:

Start of loop
   Do everything here
   Flip
   Cls
End of loop


Here's a simple example:

Graphics 500,500, 0, 2
SetBuffer BackBuffer()

While Not KeyDown(1)
	Text 200, 200, MilliSecs()
	Flip
	Delay 50
	Cls      ; clears back buffer, ready for next screen to display
Wend