pong video game
-
I am trying to do a game of pong. My problem is, i can't get the ball served to a patricular point or location. By changing a valriables value i can get the ball served either to the left or right, up or down. the ball is located in thr middle of the form. When i serve the ball it will either do up or down and then bounce off the top or bottom, so if i want to serve a ball directly to a point say in the top left hand corner. i can't. here are the variables i am using and a bit of code trying to explain what i mean i'm using a timer for the ball speed Dim XChange As Integer = -5 Dim YChange As Integer = 5 Dim BallDirX As Integer 'for direction Dim BallDirY As Integer 'for direction Dim X, Y As Integer 'unused just in case If picBall.Top < 0 Then picBall.Top = 0 XChange = -5 YChange = 5 Here I want the ball to bounce off the wall and goto a YPoint say 50 and the xpoint as zero as the ball bounces in thay direction End If thnks from Zeldacat
Regards Zeldacat
-
I am trying to do a game of pong. My problem is, i can't get the ball served to a patricular point or location. By changing a valriables value i can get the ball served either to the left or right, up or down. the ball is located in thr middle of the form. When i serve the ball it will either do up or down and then bounce off the top or bottom, so if i want to serve a ball directly to a point say in the top left hand corner. i can't. here are the variables i am using and a bit of code trying to explain what i mean i'm using a timer for the ball speed Dim XChange As Integer = -5 Dim YChange As Integer = 5 Dim BallDirX As Integer 'for direction Dim BallDirY As Integer 'for direction Dim X, Y As Integer 'unused just in case If picBall.Top < 0 Then picBall.Top = 0 XChange = -5 YChange = 5 Here I want the ball to bounce off the wall and goto a YPoint say 50 and the xpoint as zero as the ball bounces in thay direction End If thnks from Zeldacat
Regards Zeldacat
I'm assuming from the code you have given that the ball can only move by a cirtain amount each time (5pix up/down and 5pix left/right)... this might not be enough. What you need to do is calculate both the horizontal and vertical distances to the wall and then divide them by the number of ticks you want the ball to move until it reaches the wall: this is the amount you need to move the ball each timer tick... If the distance to the left wall is 100 pixels (for example) and the distance you want the ball to travel downwards is 40 pixels (giving the target position of (0, 40)) then the ball will need to move 100/N to the left and 40/N downwards each timer tick, where N is the number of ticks you want the ball to move until it reaches the wall. I believe this would work: it requires the ball's vertical/horizontal speed to be something other than 5 pixels per tick though. Essentially you need to use relative positioning rather than absolute positioning. (Small edit at 3:45). Hope this helps.
Matthew Butler
modified on Saturday, December 15, 2007 10:42:33 AM