Tutorial: How to write games

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

Space invaders with keyboard input

Okay, load up the space invaders program, and modify it to look like the following (new bits are in red.)

dim score, lives, turretx
lives = 3
turretx = 19

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        
        
    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 turretx, 23: print "<!>"
    color (255, 255, 50)
    locate 15, 17: print "!"
    Sleep (75)
wend

(Don't forget to update the locate command where it draws the turret!)

Okay, so now we can move the turret left and right by pressing the left and right arrow keys. This is our first interactive program! We're well on our way!

But first we have to understand this new if..then..endif statement.
This is called a conditional statement, because the computer sometimes runs the instructions between then and endif and sometimes doesn't. This all depends on the part between if and then, i.e. the "condition".

Let's examine the first one:

if ScanKeyDown (VK_LEFT) and turretx > 0 then

The "condition" here is: "ScanKeyDown (VK_LEFT) and turretx > 0"
This is like an expression (e.g. 1+2/3), except it evaluates to either true or false.

Conditional statements often read a lot like english. In english you might say "If it is raining then take your umbrella to work".
In this one we are telling the computer "if the player is pressing the left arrow key and the turret hasn't already moved as far as it will go, then move it slightly to the left".
Of course the computer doesn't speak english, so we need to put it in computer terms. So "the player is pressing the left arrow key" becomes "ScanKeyDown (VK_LEFT)", and "the turret hasn't already moved as far as it will go" becomes "turretx > 0".

When the computer runs this instruction and sees that the condition is true (i.e. the player is pressing the key and the turret can move further), then it will run the instructions between then and endif:

turretx = turretx - 1

Which will move our turret one column to the left (by subtracting one from it's column number).

Of course if the computer runs the if ... then instruction and the player is not pressing the left arrow key (or the turret has already reached the edge of the screen) then the computer will see that the condition is false, and it will not run the instructions between then and endif. Instead it will skip to just after the endif and keep running the program from there.

The second if..then..endif line is used to move the turret right. It works much the same way as moving left so I'll leave you to examine it and see how it works.

Buffered text output

It's time to deal with the flickering, as it is quite painful to watch!

The reason for the flickering is that the screen is updated every time we:

So each time a frame is drawn, everything dissapears, and then is drawn back in, one object at a time. So the ugly flicker is due to everything constantly dissapearing and re-appearing.

What we want is a way to prevent the user seeing our drawing until we have everything setup and ready to display. So the user never has to see a half drawn frame.
We can do this with the TextMode and DrawText commands.

dim score, lives, turretx
lives = 3
turretx = 19

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        
        
    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 turretx, 23: print "<!>"
    color (255, 255, 50)
    locate 15, 17: print "!"
    DrawText ()
    Sleep (75)
wend

Flicker problem solved!

TextMode (TEXT_BUFFERED) switches the text drawing mode to buffered mode. This means that any changes to text on the screen will not be shown until we say so. And we tell the computer we're ready to display the changes with the DrawText () command.
Sorted!

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