can someone tell me why this doesn't work right?

BlitzMax Forums/BlitzMax Beginners Area/can someone tell me why this doesn't work right?

xeemo(Posted 2009) [#1]
Here's a simple pong game I started. Sometimes the ball goes right through the paddle though and I have no idea why. I was hoping someone could help me.

Local X=320,Y=240,D1,b=1 ' coordinates of ball, direction of ball, game state (ball in motion/not in motion)

Local y2 'player 1

Local y3 = 200 'computer player center screen

Graphics 640,480

While Not KeyDown(Key_Escape)'keeps game on screen (everything is programmed within this loop)

If B=1 Then D1=6 Y2=200 'triggers ball reset. player 1 paddle is centered

DrawOval x,y,8,8 'ball is drawn on screen

DrawRect 0, y2, 5, 80 'player 1 paddle

DrawRect 635, y3, 5, 80'computer paddle

If Y -36 > Y3 And Y3 < 400 Then Y3:+6 'moves computer paddle down. keeps paddle center.

If Y -36 < Y3 And Y3 > 0 Then Y3:-6 'moves computer paddle up. keeps paddle center.



'alpha debug (not relevent to the game)

If KeyDown(Key_Space) D1=6

If KeyDown(Key_1) D1=1

If KeyDown(Key_0) D1=0

If KeyDown(Key_2) D1=2

If KeyDown(Key_3) D1=3

If KeyDown(Key_4) D1=4

If KeyDown(Key_5) D1=5

'back to game



If D1=6 Then B=0 'puts ball reset back to 0 or OFF

If D1= 0 X:-3 'ball moves left

If D1= 1 X:-3 Y:+7 'ball moves left and down

If D1= 2 X:-3 Y:-7 'ball moves left and up

If D1= 3 X:+3 'ball moves right

If D1= 4 X:+3 Y:+7 'ball moves right and down

If D1= 5 X:+3 Y:-7 'ball moves right and up

If D1= 6 Then X=320 ; Y=240 'ball reset (ball is in center of screen)

If Y >= 472 And D1=1 Then D1=2 'ball bounces back up traveling left

If Y >= 472 And D1=4 Then D1=5 'ball bounces back up traveling right

If Y <= 0 And D1=2 Then D1=1 'ball bounces back down traveling left

If Y <= 0 And D1=5 Then D1=4 'ball bounces back down traveling right



If X <= 5 And Y - Y2 <= 80 And Y - Y2 > 40 Then D1 = 4 'player hits bottom of paddle

If X <= 5 And Y - Y2 <= 40 And Y - Y2 > 20 Then D1 = 3 'player hits middle of paddle

If X <= 5 And Y - Y2 <= 20 And Y - Y2 > -8 Then D1 = 5 'player hits top of paddle

If X >= 627 And Y - Y3 <= 80 And Y - Y2 > 40 Then D1 = 1 'computer hits with bottom of paddle

If X >= 627 And Y - Y3 <= 40 And Y - Y2 > 20 Then D1 = 0 'computer hits with middle of paddle

If X >= 627 And Y - Y3 <= 20 And Y - Y2 > -8 Then D1 = 2 'computer hits with top of paddle

If KeyDown(key_down) And y2<400 Then Y2:+5 'center paddle moves down

If KeyDown(key_up) And y2>0 Then Y2:-5 'center paddle moves up



If X > 640 Or X < -8 Then B = 1



Flip;Cls 'flip draws the action cls clears the screen so the new action can be drawn

Wend


Nate the Great(Posted 2009) [#2]
try using a codebox like this

{codebox} code here {/codebox}
except replace {} with []

so here is your code in a codebox:



as for your problem um Ill have to look over it. just a minute

I honestly can't find a problem. nice pong game though


xeemo(Posted 2009) [#3]
Press 0 or 3 (to make the ball go left or right) and eventually the ball with go through one of the paddles.


Arabia(Posted 2009) [#4]


Seems to be these lines to me. You are checking the computers position (Y3). Each line you start with Y - Y3 <=80 etc. but then compare it with the players (Y - Y2) position.

I changed (all the Y2 to Y3) and it seems to work okay for me! Unless you keep your hand down on say the "3" key - in which case of course it will go through the paddle.


ImaginaryHuman(Posted 2009) [#5]
My first guess is that the ball is moving further in one step than the width of the paddle and thus jumps from one side to the other - a typical collision detection issue. ?


Arabia(Posted 2009) [#6]
The step size (x) of the ball is either -3, 0 or 3. Both paddle sizes are 5 in width - so the ball moving further than the paddle width is not the issue.

I thought the same thing initially. I even slowed the all right down to -1, 0 or 1 and it still happend.

It took me a while to notice the lines:

If X >= 627 And Y - Y3 <= 80 And Y - Y2 > 40 Then D1 = 1 'computer hits with bottom of paddle

Y = Ball Position Y
X = Ball Position X
Y3 = Top Paddle Position for Computer
Y2 = Top Paddle Position for Player

So in the above line X>=627 we are checking right side of screen (computer) for a collision, but then using Y2 (Humans Paddle) as part of the collision detection which of course is not correct.

As far as I can see, the human paddle collision code is correct, the ball was only ever passing through the computers paddle.

I'm still new enough to have not experimented with Images & Image Collision - if the ball and paddle images were Images, you could just use the pre-written image collision functions to do all this for you?

BTW, this error is one I also commonly make - happens when you use lots of short X, Y variable names. Maybe if you use:

ballX, BallY for the ball
playerY for the players paddle
computerY for the computers paddle

The original code would then change from

If X >= 627 And Y - Y3 <= 80 And Y - Y2 > 40 Then D1 = 1 'computer hits with bottom of paddle

to

If ballX >= 627 and ballY - computerY <=80 and ballY - playerY > 40 then D1 = 1

Which automatically looks wrong, where as Y2 and Y3 is not as noticeable as an error :S

Hope that makes sense :)