Baroody's Game Development Site

A Website to help you program computer games

Step 4: Ending the game

You're getting there!  If only the game would actually end when it's supposed to...

Let's think about what's really happening here.  If you look closely, you'll see that when the ball goes below the bottom wall, it actually keeps moving until it gets to be right underneath the paddle...once this happens, it moves to the top of the paddle again.  If you think about it, that's exactly what we've told it to do, right?  Our "hit the paddle" if statement just says that if the ball is below the paddle AND it's between the left and right sides of the paddle, then move it to the top of the paddle and rebound it up.  That's what's happening here.

However, we want the game to start a new ball if we miss, right?  Check out the following new code: 

tmrMain_Timer sub routine - Step 7

Let's see what happens here...if the ball is below the paddle (and not between the left and right sides, as that would be caught by the previous IF statement and the ball would have been moved up), then we move the ball to its original position and move the paddle to its original position. Then, we reset the ball speeds to move the ball down and to the right, as we did at the beginning of the game.

Makes sense, right?  Now, the only problem is that when we reset, we don't change the ball number and the ball starts moving immediately...I need some recovery time, don't you?  Let's see what we can do about that:

tmrMain_Timer - Step 8

With this new code, we up the ball number by one and then check to see if the current ball is one of the first 3.  If it is, we update the ball number label and delay the program for 2 seconds.  That gives old guys like me the time I need to get ready for the next ball!

Now, our program is almost there.  It counts the number of times we hit the ball with the paddle, if we miss, it ups the ball number (as long as it's one of the first 3 balls!) and resets the game.  The only problem now is that after the 3rd ball, it keeps going (e.g., it keeps playing, but doesn't update the ball number or delay).  Let's see what we can do to remedy that situation: 

tmrMain_Timer sub routine - Step 9 

Now, we check the ball number again.  If this is greater than 3 (in other words, 4!), we turn off the timer, make the ball invisible, and make the "Game Over" label and the "Exit" and "Play" command buttons be visible.  Try adding this code and see how the game works...hopefully it's fully functioning!


Congratulations!  You've just completed your first arcade game!  You've also completed the requirements for the Intro to Computer Programming in VB course except for one thing...the final project.  For this, you can create your own game (or another arcade game of your choosing), or you can add options to your Pong game.

If you want to see some ideas for options to add to Pong, click here or select Step5: Options in the navigation bar.