Next we'll add some code to move the space alien.
It will move from left to right. When it reaches the right hand side of the screen, we will start it again from the left.
dim score, lives, turretx, alienx lives = 3 turretx = 19 alienx = 0 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 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 "<!>" color (255, 255, 50) locate 15, 17: print "!" DrawText () Sleep (75) wend
(Again, don't forget to update the locate line where we draw the alien.)
It should be getting easier to see how this works.
We've created a new variable to store the alien's position, called alienx. Like with the turret, this stores the column that the alien is currently positioned at.
We set it to zero at the start. We didn't really have to (as zero is the default anyway), but it doesn't hurt to be specific.
We move the alien one column to the right by adding one to it's column position.
alienx = alienx + 1.
Then we check whether the alien has reached the right hand side of the screen:
if alienx > 37 then
And if it has, we move it back to the left hand side:
alienx = 0
And that's all there is to it.
The object of the game is to shoot the alien, so we need to
have a moving bullet.
This is a little bit more complicated than the other moving objects because it isn't fixed at a single height. Also, sometimes the bullet isn't on the screen at all.
Again, we need to decide what the computer needs to store in
order to handle the bullet.
We will store the bullet in three variables:
So when the user presses space, we want to "fire"
the bullet, by placing it on the screen just above the turret.
While the bullet is on the screen, it will fly up until it reaches the top, then it will be taken off the screen ready to be fired again.
When it comes time to draw the bullet, we only draw it if it's on the screen.
The modified program looks like this.
dim score, lives, turretx, alienx dim bulletx, bullety, bulletOnScreen 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 DrawText () Sleep (75) wend
(You may notice that while we can now shoot at the alien, we can't actually hit it! The bullet passes straight through. This is because we haven't told the computer to do anything when the bullet hits the alien.)
We're getting very close to a complete game now! A very simple one, but a complete one all the same.
The new code should be starting to make sense now, but you may
be wondering what true and false
You may remember we learnt earlier that conditional expressions evaluate to true or false. And that if..then..endif uses the condition between if and then to determine whether to run the instructions between then and endif.
Well we can actually store a condition result in a variable. So we set bulletOnScreen to true to say that the bullet is on the screen, and false to say that it isn't. We can then use it as the condition part of our if..then..endif statement.
We've used some if..then..endif
statements inside other if..then..endif
This is okay. The second if..then..endif simply become and instruction inside the first one.
We've also used an if..then..else..endif
statement. So you may be wondering how this new else
It's actually just a simple extension of the standard if..then..endif statement. If the condition (between if and then) evaluates to true, the computer runs the instructions between then and else, otherwise it runs the instructions between else and endif instead.
To look at what it actually does:
If the bullet is on the screen "if bulletOnScreen then", we move it up the screen by subtracting one from the row position "bullety = bullety - 1". We also check whether the bullet has reached the top of the screen "if bullety < 1 then", and if so take it off the screen. "bulletOnScreen = false".
Inside the else..endif section we say what to do if the bullet is not on the screen. In this case the player can shoot it by pressing the spacebar, so we check whether the user is pressing space "if ScanKeyDown (VK_SPACE) then" and if so, place the bullet on the screen and position it above the gun turret:
bulletOnScreen = true
bullety = 22
bulletx = turretx + 1