So we can see that the game indeed runs, and that there are many aliens.
This looks a bit more exciting! But if you're like me, you'll be wanting get that collision code back in so we can start blasting aliens! (It's a male thing...)
So we'll simplify what happens when the alien gets hit a little bit and just do the following:
Because there are 10 aliens, we need to tell the computer to check whether the bullet has hit each one individually. So we will need another for..next loop, to check the bullet's position against all of the aliens.
Here's the updated code:
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 if bulletOnScreen then for i = 1 to alienCount if alienOnScreen (i) then if bullety = alieny (i) and bulletx >= alienx (i) and bulletx <= alienx (i) + 2 then alienOnScreen (i) = false bulletOnScreen = false score = score + 100 endif endif next endif DrawText () Sleep (75) wend
Now this is probably more if..thens and for..nexts than we've used in combination before, but we should be ready to cope with it now :) It's the same as what we've seen before, just in a few more layers.
First we tell the computer that if the bullet is not on the screen then don't worry about checking any of the aliens:
if bulletOnScreen then
Next we tell it to repeat the instructions multiple times, once for each alien.
for i = 1 to alienCount
Then we say to only to check the alien if it's on the screen.
if alienOnScreen (i) then
And then we have the actual check to see if the alien and the bullet are at the same position on the screen.
if bullety = alieny (i) and bulletx >= alienx (i) and bulletx <= alienx (i) + 2 then
And finally, if it finds an alien at the same position as the bullet, it runs the code to remove the alien and the bullet from the screen and update the score.
alienOnScreen (i) = false
bulletOnScreen = false
score = score + 100
And that's about all there is too it.