Can someone help with this code :(

BlitzMax Forums/BlitzMax Beginners Area/Can someone help with this code :(

[edited. Now code is properly displayed] Thanks Regular K and xlsior ;)

Hi people,

I have programmed a little pong clone to help me get familiar with BMax. If you run this code and leave it to run for a few moments, you'll see the problem that i've got. The cpu score seems to shoot up to quickly as the ball is hidden, then suddenly the ball comes back into play. Can anyone please help me out.

here is the code:


Strict
Graphics 800,600

Type board

	Field x
	Field y
	Field width
	Field height
	
End Type


Type wall

	Field x
	Field y
	Field width
	Field height
	
End Type


Type ball_object

	Field x#
	Field y#
	Field vx#
	Field vy#
	Field radius 
	Field speed#
	
End Type


Type bat

	Field x
	Field y
	Field width
	Field height
	Field score
	
End Type



Global b:board=New board
b.x=0;b.y=0;b.width=800;b.height=600

Global top:wall=New wall
top.x=0;top.y=0;top.width=800;top.height=10

Global bottom:wall=New wall
bottom.x=0;bottom.y=590;bottom.width=800;bottom.height=10

Global lft:wall=New wall
lft.x=0;lft.y=0;lft.width=10;lft.height=600

Global rht:wall=New wall
rht.x=790;rht.y=0;rht.width=10;rht.height=600

Global user:bat=New bat
user.x=0;user.y=200;user.width=20;user.height=120;user.score=0

Global cpu:bat=New bat
cpu.x=780;cpu.y=200;cpu.width=20;cpu.height=120;user.score=0

Global ball:ball_object=New ball_object
ball.x=400;ball.y=200;ball.vx=5;ball.vy=5;ball.radius=20;ball.speed=1






While Not KeyHit(key_escape)
Cls

	ball.vy:*ball.speed
	ball.x:+ball.vx
	ball.y:+ball.vy
	
	If KeyDown(key_a) Then user.y:-5		' user control
	If KeyDown(key_z) Then user.y:+5
	
	If ball.y>(cpu.y+(cpu.height/2)) Then cpu.y:+5			'cpu ai
	If ball.y<cpu.y Then cpu.y:-5 
	
	
	If user.y<(top.y+top.height) Then user.y=(top.y+top.height)			'limit user movement
	If user.y+user.height>bottom.y Then user.y=(bottom.y-user.height)
	If cpu.y<(top.y+top.height) Then cpu.y=(top.y+top.height)			'limit cpu movement 
	If cpu.y+cpu.height>bottom.y Then cpu.y=(bottom.y-cpu.height)
	

	
	If ball.y<(top.y+top.height) Then ball.vy=-ball.vy				'limit balls movements 
	If ball.y+(ball.radius)>bottom.y Then ball.vy=-ball.vy
	
	
	
	If ball.x+(ball.radius)>rht.x 
		user.score:+1			'player scores a point
		ball.vx=-ball.vx
	EndIf
	
	If ball.x<(lft.x+lft.width)
		 cpu.score:+1				'cpu scores a point
		 ball.vx=-ball.vx
	EndIf 
	
	
	
	If ball.x<(user.x+user.width) And ball.y>user.y And (ball.y+ball.radius)<(user.y+user.height)
		ball.vx=-ball.vx			'ball to user collision check
	EndIf
	
	If ball.x+ball.radius>cpu.x And ball.y>cpu.y And ball.y<(cpu.y+cpu.height)
		ball.vx=-ball.vx			'ball to cpu collision check
	EndIf
	

	
	SetColor 100,100,255
	DrawRect b.x,b.y,b.width,b.height
	SetColor 0,0,255
	DrawRect top.x,top.y,top.width,top.height
	DrawRect bottom.x,bottom.y,bottom.width,bottom.height
	
	'DrawRect lft.x,lft.y,lft.width,lft.height
	'DrawRect rht.x,rht.y,rht.width,rht.height
	
	SetColor 255,255,255
	DrawRect user.x,user.y,user.width,user.height
	DrawRect cpu.x,cpu.y,cpu.width,cpu.height
	
	DrawOval ball.x,ball.y,ball.radius,ball.radius
	
	DrawText "PLAYER SCORE:"+user.score,50,10
	DrawText "CPU SCORE:"+cpu.score,650,10
	DrawText "PONG CLONE",350,20
	
	Flip
	FlushMem
	
Wend
End




Thankyou
Devil

I can't seem to reproduce the problem enough to know for sure, but it seems that when the ball bounces out of bounds, then reverses direction, then hits teh top or bottom edge of the user's paddle before the ball actually gets out into the playfield, it reverses back out of bounds again, then sort of gets stuck there for a few hundred cycles until it dislodges itself back into the playing field. You need to make sure the ball is actually traveling toward the paddle before reversing the ball. try changing
 If ball.x<(user.x+user.width) And ball.y>user.y And (ball.y+ball.radius)<(user.y+user.height)
ball.vx=-ball.vx 'ball to user collision check
EndIf

with
 If ball.x<(user.x+user.width) And ball.y>user.y And (ball.y+ball.radius)<(user.y+user.height) And ball.vx < 0
ball.vx=-ball.vx 'ball to user collision check
EndIf

See if that helps.

Hi Tom,

I have just replaced the code exactly how you said, and you were right. It works great now!!!
Thankyou ever so much, i really appriciate your help :)

Thanks again Tom
Devil

also to put the code into boxes put [ code ] at the top then [ / code ] at the bottom, without spaces
like that ^^

Ahhh, i see :)
Thankyou Regular K. I've been wondering how to do that for ages and now i know. Thankyou very much :)

you can also put it in a scrollable box using the [ codebox ] tag, like below... Can make discussions a little easier to follow if there are large pieces of code intermixed with them.

PRINT "HELLO WORLD"
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.
REM Nothing to see here, please move along.



Thankyou xlsior. Your help is greatly appriciated :)