Adding a little delay

Of course, bullets should only be added when the user presses a button to fire. One option could be 2ND. But we don't want the ship to fire too rapidly, so first set up a counter in the main program. We'll be using this counter for lots of things, here to make sure the fire button doesn't repeat too quickly.

PROGRAM:ASHMUP
:.SHMUP An awesome Game.
:prgmASHMUPD
:prgmASHMUPI
:Repeat getKey(15)
C+1→C :prgmASHMUPK
:.prgmASHMUPM
:ClrDraw
:Pt-Change(X,Y,GDB0)
:DispGraph
:End
:.prgmASHMUPR

The only new line of code is a C+1→C right after the main loop starts. We don't really have to initiate C because it won't make much of a difference except to make our program slightly larger.

What does matter, though, is how C affects firing. Add this code to the end of prgmASHMUPK:

PROGRAM:ASHMUPK
:..KEYS
:If getKey(1)
:Y+2→Y
:End
:If getKey(2)
:X-2→X
:End :If getKey(3)
:X+2→X
:End
:If getKey(4)
:Y-2→Y
:End
:If getKey(54)
:!If C^8 :sub(ASB,1,3,X+3,Y-4)
:End
:End

In case you don't understand this code completely, it first checks if 2ND is held down (it's 54, if you haven't memorized your keycode tables yet). Then it tests to see if the counter is a multiple of 8. This adds a little pause between consecutive bullets; otherwise, a bullet is added each pass of the loop, and you'd quickly get a continuous stream of bullets! (That's cheating!)

If the conditions are right, the program adds a "type 1" bullet with a speed of 3 pixels per pass (a bit faster than the ship at 2) placed at a position three pixels to the right of the ship and four pixels up, which positions a 2- by 4-pixel bullet nicely. Don't bother compiling yet, since we haven't added any of the actual bullet-drawing code. That's for the next section.

+ Tweet