Tutorial: How to write games

Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>

Back to space invaders

So let's take what we've learned and extend the space invaders program. We will be adding to the program bit by bit, so each example will show the new pieces in red.

First, we will start by storing the score and lives in some variables:

dim score, lives
lives = 3

color (255, 255, 255)
locate 0, 0: print "Score=" + score
locate 30, 0: print "Lives=" + lives
color (255, 50, 50)
locate 11, 12: print ">O<"
color (150, 150, 150)
locate 17, 23: print "<!>"
color (255, 255, 50)
locate 15, 17: print "!"

Notice that we set "lives" to it's initial value at the start of the program. The initial value for "score" is 0, so we don't have to change it.

Next we will take all the drawing code, and put it into an animation loop. The program will keep on drawing new animation frames until the user quits it by pressing escape. So remember to press escape when you want to get out of the program!

dim score, lives
lives = 3

while true
    cls
    color (255, 255, 255)
    locate 0, 0: print "Score=" + score
    locate 30, 0: print "Lives=" + lives
    color (255, 50, 50)
    locate 11, 12: print ">O<"
    color (150, 150, 150)
    locate 17, 23: print "<!>"
    color (255, 255, 50)
    locate 15, 17: print "!"
    Sleep (75)
wend

This time we've used a while..wend loop, which is another sort of loop. Like the for..next loop, the instructions inside the loop are run multiple times. In this case the computer will keep on running the instructions forever, until the user quits the program by pressing escape.
(Not all while..wend loops run forever, there are many different ways to use them. The while..wend loop will be explained in more detail later.)

You may have noticed a couple of things:

We will fix the flickering later. For now we need to concentrate on making things move, and the obvious place to start is the player's turret, which will slide along the bottom of the screen.

To make the player's turret move, the computer needs to store information about the turret in variables. We need to decide exactly what information it needs to remember so that it can draw the turret on the screen and move it around when the player pressed the right keys.
We will make the turret moves as follows:

But first, we need to take another detour to introduce some more new concepts.

Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>