moving a ball??
BlitzPlus Forums/BlitzPlus Beginners Area/moving a ball??
| ||
Hello everyone, I'm trying to make a Pong game and I have no idea how to correctly move the ball. I've searched the forums but can't find anything useful to me. Would someone post some sample code and explain how it works? I can get the ball to move in a straight line and hit the bat but it won't bounce back. I've also looked in the sample programs and seen some working code but I don't understand how or why it works. Thanks |
| ||
How long have you been programming, Jester? I need to know before I come up with some suggestions. I made a pong game myself once, with the "original" graphics style, i.e. all rectangles; it was just for fun, and I'll send you a copy of the code if you like! |
| ||
ADAM, I've been programming for about 3 weeks give or take a few days. If your code has plenty of comments explaining it,I'd love to see it.If not I may not fully understand whats going on. I've got the ball and bats on the screen and can move the bats. It's getting the ball to move like I want it that has me stuck. Once I get the ball moving like I want,I'm pretty sure I can add scores and reset the ball with no problems. Also this is a 2 player pong game with no AI bat movement. I plan to add a 1 player option after getting the 2 player part working like I want it and my programming skills develop a little more. Thanks |
| ||
Okay, I'll re-comment my code. I should have it ready in a few days... Hang tight, man! |
| ||
here is one way modified a bit from that book game programming for teens first make a type ;The ball type: for the ball Type ball Field x,y,xv,yv ;x, y coordinates, and x, y velocity End Type then initialize your values to whatever you want ball\x = whatever ball\y = whatever ball\xv = whatever ball\xy = whatever Then in your main loop you will have DrawImage (ballimage,ball\x,ball\y) then you will want a checkcollisions function and in that function you will have something like if ball\y <= 0 or ball\y >= 600 ;ball hits top or bottom ball\yv = -ball\yv + rand(-2,2) ;add a little randomness endif That book game programming for teams has the full source code for a pong game and I think it is like $60 bucks at Borders |
| ||
almost forgot the most important part! you need to update your ball somewhere maybe right before you draw it in your main loop ;update ball's position on screen ball\x = ball\x + ball\xv ball\y = ball\y + ball\yv |
| ||
I LOVE THAT BOOK!!! GPFT gave me my big break into the field! |
| ||
I have the second edition of that book on the way. Hopefully it will get here soon. Thanks again! |