pong

BlitzMax Forums/BlitzMax Beginners Area/pong

Ok, I seemed to lose all motivation for Blitz ever since I got BMX, mostly because it was such a big change. So, I'm back at the beginning. So I figure I'd make pong. No types or anything, just pong. The only thing stopping me is collision. I'm not sure why the ball doesn't bounce off the paddle like it's supposed to. Take a look:

Global paddle1,paddle2,ball1
Global py1,py2,bx,by,bsp,bdirx,bdiry

Graphics 800,600,32,60

HideMouse()

AutoMidHandle True

SeedRnd MilliSecs()

SetColor(255,255,255)

py1=275
py2=275
bx=395
by=295
bsp=2

bdirx=Rnd(1,2)
Select bdirx
	Case 1 bdirx=1
	Case 2 bdirx=-1
End Select

bdiry=Rnd(1,2)
Select bdiry
	Case 1 bdiry=1
	Case 2 bdiry=-1
End Select 

While Not KeyHit(KEY_ESCAPE)

Game()

Flip;Cls;FlushMem

Wend

Function Game()

	DrawRect(0,0,800,10)
	DrawRect(0,590,800,10)

	SetRotation(0)
	SetScale(1,1)
	SetColor(255,255,255)

	'DrawImage(paddle1,0,py1)
	DrawRect(0,py1,10,75)
	DrawRect(790,py2,10,75)
	DrawRect(bx,by,10,10)
	
	CollideRect(0,py1,10,75,0,0,1)
	'CollideRect(paddle2,790,py2,0,0,1)
	
	If KeyDown(KEY_UP) Then py1=py1-4
	If KeyDown(KEY_DOWN) Then py1=py1+4
	
	If by>py2 Then py2=py2+4
	If by<py2 Then py2=py2-4
	
	If py1<10 Then py1=10
	If py1>515 Then py1=515
	If py2<10 Then py2=10
	If py2>515 Then py2=515
	
	bx=bx+bdirx*bsp
	by=by+bdiry*bsp
	
	If by>580 Then bdiry=-1
	If by<10 Then bdiry=1	
	
	If CollideRect(bx,by,10,10,0,1,0) Then
	
		bdirx=-bdirx
		DrawText("COLLISION!",200,300)
	
	EndIf

End Function


The code that is supposed to randomize the ball's starting direction fails to work as well. Any help would be appreciated. It seems I have yet to grasp BMX collision and random numbers.

Global paddle1,paddle2,ball1
Global py1,py2,bx,by,bsp,bdirx,bdiry

Graphics 800,600,0,0

HideMouse()

AutoMidHandle True

SeedRnd MilliSecs()

SetColor(255,255,255)

py1=275
py2=275
bx=395
by=295
bsp=2

bdirx=Rnd(1,2)
Select bdirx
	Case 1 bdirx=1
	Case 2 bdirx=-1
End Select

bdiry=Rnd(1,2)
Select bdiry
	Case 1 bdiry=1
	Case 2 bdiry=-1
End Select 

While Not KeyHit(KEY_ESCAPE)

Game()

Flip;Cls;FlushMem

Wend

Function Game()

	DrawRect(0,0,800,10)
	DrawRect(0,590,800,10)

	SetRotation(0)
	SetScale(1,1)
	SetColor(255,255,255)

	'DrawImage(paddle1,0,py1)
	DrawRect(0,py1,10,75)
	DrawRect(790,py2,10,75)
	DrawRect(bx,by,10,10)
	
	CollideRect(0,py1,10,75,1,1)
	CollideRect(790,py2,10,75,1,1)
	'CollideRect(paddle2,790,py2,0,0,1)
	
	If KeyDown(KEY_UP) Then py1=py1-4
	If KeyDown(KEY_DOWN) Then py1=py1+4
	
	If by>py2 Then py2=py2+4
	If by<py2 Then py2=py2-4
	
	If py1<10 Then py1=10
	If py1>515 Then py1=515
	If py2<10 Then py2=10
	If py2>515 Then py2=515
	
	bx=bx+bdirx*bsp
	by=by+bdiry*bsp
	
	If by>580 Then bdiry=-1
	If by<10 Then bdiry=1	
	
	If CollideRect(bx,by,10,10,1,0) Then
		bdirx=-bdirx
		DrawText("COLLISION!",200,300)
	
	EndIf

End Function


You need to write the two bats to the collision mask otherwise you will have nothing to check the ball against.

Here is a test that I did when I first started out with BlitzMax.

Graphics 640, 480, 0
SetBlend ALPHABLEND

player1_y# = 240
player1_speed# = 0.0
player2_y = 240

ball_x# = 320
ball_y# = 240

startingAngle = Rand( 45, 135 )

ball_xSpeed# = Sin( startingAngle )*3
ball_ySpeed# = Cos( startingAngle )*3

While Not KeyHit( KEY_ESCAPE )

If KeyDown( KEY_UP ) Then player1_speed# :- 0.8
If KeyDown( KEY_DOWN ) Then player1_speed# :+ 0.8
player1_speed# :* 0.9
player1_y# :+ player1_speed#

player2_y = ball_y#

If player1_y < 50 Then player1_y = 50
If player1_y > 430 Then player1_y = 430
If player2_y < 50 Then player2_y = 50
If player2_y > 430 Then player2_y = 430

If ball_x# <= 18 Then
If ball_y# < player1_y+50 And ball_y# > player1_y-50 Then
ball_xSpeed# = -ball_xSpeed#
ball_ySpeed# :+ player1_speed#/4
End If
ElseIf ball_x >= 622 Then
If ball_y# < player2_y+50 And ball_y# > player2_y-50 Then
ball_xSpeed# = -ball_xSpeed#
End If
End If

If ball_y# <= 8 Or ball_y >= 472 Then
ball_ySpeed# = -ball_ySpeed#
End If

ball_x# :+ ball_xSpeed#
ball_y# :+ ball_ySpeed#

Cls

DrawRect 0, player1_y-50, 10, 100
DrawRect 630, player2_y-50, 10, 100
DrawOval ball_x#-8, ball_y#-8, 16, 16
DrawLine 320, 0, 320, 480

Flip

Wend
End

Purt: That works, but it seems to be veeerrry laggy now.. Something to do with the collisions being called over and over again in the loop or something?

Daniel: Hey, that's realy cool! I like how smooth it is, thanks. I think I will study that.

Global paddle1,paddle2,ball1
Global py1,py2,bx,by,bsp,bdirx,bdiry

Graphics 800,600,32,60

HideMouse()

AutoMidHandle True

SeedRnd MilliSecs()

SetColor(255,255,255)

py1=275
py2=275
bx=395
by=295
bsp=2

bdirx=Rnd(1,3)
Select bdirx
	Case 1 bdirx=1
	Case 2 bdirx=-1
End Select

bdiry=Rnd(1,3)
Select bdiry
	Case 1 bdiry=1
	Case 2 bdiry=-1
End Select 

While Not KeyHit(KEY_ESCAPE)

Game()

Flip;Cls;FlushMem

Wend

Function Game()

	DrawRect(0,0,800,10)
	DrawRect(0,590,800,10)

	SetRotation(0)
	SetScale(1,1)
	SetColor(255,255,255)

	'DrawImage(paddle1,0,py1)
	DrawRect(0,py1,10,75)
	DrawRect(790,py2,10,75)
	DrawRect(bx,by,10,10)
	
	ResetCollisions()
	
	CollideRect(0,py1,10,75,1,1)
	CollideRect(790,py2,10,75,1,1)
	'CollideRect(paddle2,790,py2,0,0,1)
	
	If KeyDown(KEY_UP) Then py1=py1-4
	If KeyDown(KEY_DOWN) Then py1=py1+4
	
	If by>py2 Then py2=py2+4
	If by<py2 Then py2=py2-4
	
	If py1<10 Then py1=10
	If py1>515 Then py1=515
	If py2<10 Then py2=10
	If py2>515 Then py2=515
	
	bx=bx+bdirx*bsp
	by=by+bdiry*bsp
	
	If by>580 Then bdiry=-1
	If by<10 Then bdiry=1	
	
	If CollideRect(bx,by,10,10,1,0) Then
		bdirx=-bdirx
		DrawText("COLLISION!",200,300)
	
	EndIf

End Function

Just stuck a ResetCollisions() in, seems okay now.

*EDIT* oh yeah, have a look a this to see whats happening with rnd()
Repeat
	Print Rnd(1,2)
	'Print Int(Rnd(1,2))
	'Print Int(Rnd(1,3))
Until KeyHit(KEY_ESCAPE)


Thanks Papa and Purt. I think I can take it from here. :)