Tutorial: How to write games part 2

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

Back to the space aliens!

Now I promised that we were going to have multiple space aliens, and now we have the tools to do it.

So load up the space alien game from the first tutorial (or cut and paste it from the tutorial if necessary).

More of 'em

We're going to use the same techniques from above to create multiple space aliens.
But first we have to think a little bit about how this will affect the game rules. Because the game rules for the 1 alien version aren't quite suited for a multiple space alien version.
In the current version, whenever we shoot the alien it reappears on the left side of the screen. But now we would really prefer them to dissapear and stay gone, so that the player can clear the screen of all aliens and win the game (or move on to the next level etc).

So the computer also needs to remember which aliens are still on screen, and which aren't, so it knows which ones to move around and draw.

We'll start by changing the variables declared to arrays. That is we will replace (at the top of the program):

dim score, lives, turretx, alienx, alieny

With :

const alienCount = 10
dim score, lives, turretx
dim alienx (alienCount), alieny (alienCount), alienOnScreen (alienCount)
dim bulletx, bullety, bulletOnScreen, i

This will stop the program from working for now, but that doesn't matter (we still have some more changes to make before it is ready).
Notice that we've used a constant for the number of aliens, instead of simply writing 10 everywhere. This will make it easy to change the number of aliens later (e.g. if we want to make the game easier or harder.)
We've added the alienOnScreen array to store whether or not each alien is still on the screen.

Next we have to change the code that puts the aliens in their starting position. Now that we have more than one, we can't have them both start from the same position, so we will use random numbers to set their starting position.

Replace the lines (near the top):

alienx = 0
alieny = 12

With:

for i = 1 to alienCount
    alienx(i) = rnd () % 37
    alieny(i) = rnd () % 22 + 1
    alienOnScreen(i) = true
next

This should put the aliens in random start positions (rnd () % 37 makes a random number between 0 and 36, and rnd () % 22 + 1 makes a random number between 1 and 22). We obviously couldn't set them all to column 0, row 12, as otherwise they would all be drawn over the top of each other, and it would look like there is only one alien!
We still have a couple of changes to make before the program will compile and run though.

Next we will change the alien movement code.
Find the section of the program that reads:

    alienx = alienx + 1
    if alienx > 37 then
        alienx = 0
        alieny = rnd () % 22 + 1
    endif

And replace it with:

    for i = 1 to alienCount
        if alienOnScreen (i) then 
            alienx (i) = alienx (i) + 1
            if alienx (i) > 37 then
                alienx (i) = 0
                alieny (i) = rnd () % 22 + 1
            endif
        endif
    next

Notice that we've used our for..next loop to make sure the instructions get repeated multiple times - once to move each alien. We've also used an if..then..endif statement to tell the computer to only run the instructions between then and endif if the alien is on the screen. (There's no reason to move the alien if it isn't.)

We're almost there. Next we're going to update the code that draws the alien on the screen.
Find this line in the program

    locate alienx, alieny: print ">O<"

And replace it with this:

    for i = 1 to alienCount
        if alienOnScreen (i) then
            locate alienx (i), alieny (i): print ">O<"
        endif
    next

Again we tell the computer to run the same lines of code once for each alien.
And again we tell the computer only to draw the alien if it's still on the screen.

We've made quite a few changes, and the program still won't run. It's normally a good idea to try and keep the program working as we make the changes. This means that we can test each of the steps as we go along, and keeps us from getting disheartened.
So what we're going to do now is completely remove the collision detection code so that we can test the program! (Don't worry, we will add it back in again very soon.)

So find the section of the program that reads:

    if bulletOnScreen and bullety = alieny and bulletx >= alienx and bulletx <= alienx + 2 then
        color (255, 255, 100)
        for i = 1 to 10
            locate alienx, alieny: print "///"
            DrawText ()
            Sleep (50)
            locate alienx, alieny: print "\\\"
            DrawText ()
            Sleep (50)
        next
        bulletOnScreen = false
        alienx = 0
        alieny = rnd () % 22 + 1
        score = score + 100
        Sleep (1000)
    endif

And delete it!

Now run the program, and you should see a whole screenful of space aliens flying up above the turret.

If it doesn't work, then you will need to check over the program for mistakes.
To make it easier, here's the complete program at this point, with the changes in red as usual.

const alienCount = 10
dim score, lives, turretx
dim alienx (alienCount), alieny (alienCount), alienOnScreen (alienCount)
dim bulletx, bullety, bulletOnScreen, i
lives = 3
turretx = 19
for i = 1 to alienCount
    alienx(i) = rnd () % 37
    alieny(i) = rnd () % 22 + 1
    alienOnScreen(i) = true
next
bulletOnScreen = false

TextMode (TEXT_BUFFERED)
while true
    if ScanKeyDown (VK_LEFT) and turretx > 0 then
        turretx = turretx - 1
    endif
    if ScanKeyDown (VK_RIGHT) and turretx < 37 then 
        turretx = turretx + 1
    endif
    for i = 1 to alienCount
        if alienOnScreen (i) then 
            alienx (i) = alienx (i) + 1
            if alienx (i) > 37 then
                alienx (i) = 0
                alieny (i) = rnd () % 22 + 1
            endif
        endif
    next
    if bulletOnScreen then 
        bullety = bullety - 1
        if bullety < 1 then 
            bulletOnScreen = false
        endif
    else
        if ScanKeyDown (VK_SPACE) then
            bulletOnScreen = true
            bullety = 22
            bulletx = turretx + 1
        endif
    endif
        
    cls
    color (255, 255, 255)
    locate 0, 0: print "Score=" + score
    locate 30, 0: print "Lives=" + lives
    color (255, 50, 50)
    for i = 1 to alienCount
        if alienOnScreen (i) then
            locate alienx (i), alieny (i): print ">O<"
        endif
    next
    color (150, 150, 150)
    locate turretx, 23: print "<!>"
    if bulletOnScreen then
        color (255, 255, 50)
        locate bulletx, bullety: print "!"
    endif
 
    DrawText ()
    Sleep (75)
wend
Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>