As with the other games we've done, we'll start with some "set up" - defining variables and writing routines that will help us to get the game ready to play.
You should start by looking at your code, which should be blank at this point. Put in your header and the Option Explicit line to start. Also, add the following global variables:
The first two lines declare a couple of windows based functions that we'll use later on to cause a delay to occur before we start a new ball. The next three variables will be used to store the horizontal & vertical speeds for the ball and the speed that the paddle will move. The next three global variables will be used to store the original paddle and ball location, so that we can reset these when the player "loses" a point. The gintNewBall variable will be used to store the number ball being played (typically, the player gets three balls). The gintPlayer1Score variable will store the number of "hits" a player gets (e.g., the number of times the ball hits the paddle). Finally, the gintBallSpeed variable will control the speed of the ball. For now, this will simply be a fixed speed, but we'll use this to allow for changing the ball speed in later versions of the program.
OK, so far so good. Now, let's deal with what happens when we load the form. Check out the following Form_Load sub routine:
Here's what we do in this routine
OK, we've now got the form up and running. Soon we'll do the major coding, but first, we need to take care of starting the game. Take a look at the following routines:
You know what the cmdExit_Click routine does. I'm not going to explain the Wait() routine either. I actually found this procedure on a programming bulletin board when I was trying to figure out how to delay the game for a few seconds. Just know this about it...when you call this routine, you pass it a number, which is the number of milliseconds for which it does nothing! In other words, for our purposes, we just delay all processing for 2 seconds (by calling this routine with the parameter 2000).
Let's walk through the cmdNewGame_Click routine. Here's what happens:
OK...at this point, your code should run, but not much will happen. Make sure that everything is working (e.g., no compile or runtime errors) and then, when you're ready, click here or select Step 3: Collisions on the navigation bar.