Hello,
Got BlitzMax a few days ago, and I'm trying to do a simple pong game. But I don't have complete control over the direction the ball is supposed to move.. I provided the complete code below. I give it a starting degrees of 194, but the ball moves right (like, 180 degrees). But if I change the ballspeed to 5 or bigger, the degrees works as expected.. Why??
I'm thinking it might have something to do with the way I calculate the ball position (see code, under "Draw ball"). Maybe it's a clumsy/wrong way to do it? Please, if someone have the time.. Have a look.. :-)
'Strict
'SeedRnd MilliSecs()
'Local degrees#=Rnd(315,405)
Local degrees=194
Local width=800
Local height=600
Local paddley=0
Local opponenty=300
Local ballx=400
Local bally=300
'SeedRnd MilliSecs()
'Local bally#=Rnd(10,590)
Local ballspeed=4
Local paddlewidth=20
Local paddleheight=100
Local points:Int
Local recentcollision_user=False
Local recentcollision_top=False
Local recentcollision_bottom=False
timer=CreateTimer(60)
'- - - - - - - - - - - - - - - - -
Graphics width,height,16,60
HideMouse
While Not KeyDown(Key_ESCAPE)
'off screen (if this is removed, timing is screwed up..)
DrawText("Timer: "+WaitTimer(timer),210,-20)
'off screen
'debug text, to keep an eye on things..
DrawText("Ball Position: X "+ballx+", Y "+bally,210,10)
DrawText("Ball direction (degrees): "+degrees,210,25)
DrawText("Player Paddle Y: "+paddley,210,40)
DrawText("Opponent Paddle Y: "+opponenty,210,55)
DrawText("Recent Collision User: "+recentcollision_user,210,70)
DrawText("Recent Collision Top: "+recentcollision_top,210,85)
DrawText("Recent Collision Bottom: "+recentcollision_bottom,210,100)
'screen info
DrawText("P - Pause",680,563)
DrawText("Q - Quit",680,578)
DrawText("Point: "+points,5,578)
'Draw ball
ballx=ballx-(Cos(degrees)*ballspeed)
bally=bally-(Sin(degrees)*ballspeed)
DrawOval(ballx,bally,10,10)
'Draw user bat
paddley=MouseY()
If paddley<=5 Then paddley=5
If paddley>=490 Then paddley=490
DrawRect(5,paddley,paddlewidth,paddleheight)
'Draw other bat (dead - just following ball)
opponenty=bally-50
DrawRect(775,opponenty,paddlewidth,paddleheight)
' C H E C K C O L L I S I O N S A N D
' C H A N G E B A L L D I R E C T I O N
' S T A R T
'user paddle
If recentcollision_user=False
If ballx<=24 And paddley<=(bally+5) And paddley>=(bally-95)
recentcollision_user=True
degrees=(180-degrees)
points:+10
EndIf
EndIf
'opponent side (no actual paddle collision yet)
If ballx>=770
recentcollision_user=False
degrees=(180-degrees)
EndIf
'top of screen
If recentcollision_top=False
If bally<=1
recentcollision_top=True
recentcollision_bottom=False
degrees=(180+(180-degrees))
EndIf
EndIf
'bottom of screen
If recentcollision_bottom=False
If bally>=590
degrees=(180+(180-degrees))
recentcollision_bottom=True
recentcollision_top=False
EndIf
EndIf
If degrees>=360 Then degrees:-360
If degrees<=-1 Then degrees:+360
' C H E C K C O L L I S I O N S A N D
' C H A N G E B A L L D I R E C T I O N
' E N D
'did user miss ball?
If ballx<=-15
DrawText("C R A P . . !",350,298) ; Flip ; Delay(3000)
points=0 ; ballx=400 ; bally=300
SeedRnd MilliSecs() ; degrees=Rnd(315,405)
recentcollision_user=False
recentcollision_top=False
recentcollision_bottom=False
EndIf
If KeyHit(KEY_P)
DrawText("P A U S E",350,298) ; Flip
While Not KeyHit(KEY_P) ; Wend
EndIf
If KeyDown(KEY_Q) Then Exit
Flip ; Cls
Wend
|