![]() |
OO Game Programming tutorial - Part 8 |
Ball movementThe most complex object of the game is the ball. As we mentioned before, the ball has a vector which tells us direction the ball is travelling. If you don't know about vectors, a basic physics/science book will go into depth explaining it. The vector contains information describing how far to move on each axis. For example: A vector where xi = 1 and yi = -2, would mean we would move 1 unit right on the x-axis and 2 units down on the y-axis. So having a vector for a ball, we can easily make the ball go whatever direction we want just by changing the values of each component. I have decided to add another attribute to the ball class. It will have a speed attribute which will allow us to alter the ball speed easily as the game progresses making it faster so there's a challenge. Again change it's value in the constructor if it is too slow/fast for your system. In combination with the ball vector we can write the Update() function: x += xi * speed; We will update the ball's current position every game loop in Game_Main() by calling the Update() function in the ball class. Making these changes to the code we can now see the ball fly straight off the screen. Looks like we'll need to add some collision detection, that's next! Project: Pong08.zip
|