Tutorial: How to write games

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

Shooting the alien

To complete the game we need to make the bullet hit the alien.
So we have to tell the computer how to check when the bullet has hit the alien, and what to do when it happens.

We're going to keep it simple, and do the following:

So let's do it:

dim score, lives, turretx, alienx
dim bulletx, bullety, bulletOnScreen, i
lives = 3
turretx = 19
alienx = 0
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
    alienx = alienx + 1
    if alienx > 37 then
        alienx = 0
    endif
    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)
    locate alienx, 12: print ">O<"
    color (150, 150, 150)
    locate turretx, 23: print "<!>"
    if bulletOnScreen then
        color (255, 255, 50)
        locate bulletx, bullety: print "!"
    endif

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

To hit the alien, the bullet has to be on the screen, at the same row as the alien, and on a column between alienx and alienx + 2 (because the alien is 3 columns wide).
We animate the exploding alien by repeatedly drawing diagonal lines over the top, using a for..next loop to repeat the process 10 times, and a Sleep (50) delay for timing.

We placed all this code after the regular drawing code, so that everything is already on the screen when we perform our animation.

After our animation, we remove the bullet from the screen, reset the alien to the left side, add 100 hundred points and pause for 1 second.

Program complete (well, sortof!)

And that's it. That's our text based space alien game.

Currently it has a lot of restrictions, such as there's only one alien, and it doesn't shoot back (so there's not much point in having 3 lives...). But it's a playable game all the same.

Along the way we've learnt a number of programming concepts and techniques, including:

Notice how we built up the program in stages.
At each stage we had a working program that we could run and examine the results of. This allowed us to check each stage, and make sure that the code was doing what we expected before moving on. It also kept things interesting (I hope!), as we were able to see the difference that each of our changes made.

As a final touch we will add a little variety and make the alien fly past at different heights.
We can use the rnd() function, which returns a random number between 0 and about 32,000. A bit like throwing a 32,000 sided dice I guess.
With a bit of maths we can turn this into a number between 1 and 22 like so:

alieny = rnd () % 22 + 1

Here's the final version:

dim score, lives, turretx, alienx, alieny
dim bulletx, bullety, bulletOnScreen, i
lives = 3
turretx = 19
alienx = 0
alieny = 12
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
    alienx = alienx + 1
    if alienx > 37 then
        alienx = 0
        alieny = rnd () % 22 + 1
    endif
    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)
    locate alienx, alieny: print ">O<"
    color (150, 150, 150)
    locate turretx, 23: print "<!>"
    if bulletOnScreen then
        color (255, 255, 50)
        locate bulletx, bullety: print "!"
    endif

    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
    
    DrawText ()
    Sleep (75)
wend
Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>