Baroody's Game Development Site

A Website to help you program computer games

Step 2: Setting Up

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:

General Declarations

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:

Form Load sub routine

Here's what we do in this routine

  • First, we center the window in the screen.  Remember this code?  We've used it before...it's really good, huh? 
  • Next, we set the speed for the ball (as you'll come to see, this is really the vertical speed of the ball only).
  • Next,   we store the initial location of the paddle and the ball.  We do this so that we can reset the game when we click on the "Play" command.
  • Then we set the current ball number to 1 and the timer interval to 0.  Here's the way timers work:  all the code within the timer sub procedure (this will be tmrMain_Timer in our case) will be executed every x milliseconds, where x is the interval.  So, by setting tmrMain.Interval = 1, we are saying to execute the tmrMain_Timer every millisecond.  If we set the interval to 1000, then the code would execute every 1 second.  When the interval is 0, the code in the tmrMain_Timer sub routine will not execute at all...that's where we start.

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:

New Game sub routine

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:

  • We start by making the Play and Exit command buttons invisible.  We don't want them to be there while the game is in progress.
  • Next, we make the ball visible and make the "Game Over" label invisible (note that this is only visible when a game has finished playing).
  • Next, we set the vertical ball speed (gintSpeedY).  You'll come to realize that a positive value for the vertical speed will move the ball down and a negative value will move it up...more on that later.  At this point, just know that we're setting this speed to be a constant (50 in this case).  If you think the ball is moving too slowly, then you should up this value to 100 or 200 in the Form_Load routine.
  • Then we set the horizontal ball speed (gintSpeedX).  For this, we choose a random number between -250 and 1.  As you'll come to see, a negative number will move the ball to the left and a positive number will move it to the right.  Since the ball starts on the right hand side of the "court," we want to move it to the left at the beginning.
  • Next, we set the player's score to 0 and put this value into the label we've made for tracking this (lblPlayer1Score).
  • We then set the current ball to 1 and show that to the player via the "Current Ball" label.
  • Finally, after waiting 2 seconds, we set the timer interval to 1 millisecond.  This is the line that makes everything start moving!

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.