Controlling The Ship

Click here to load the completed tutorial directly into the editor SWARM-09.DBA. If you want to step through and make the modifications, click here to load SWARM-08.DBA.

Now we have a ship gliding through the stars, it takes only a few more commands to breathe life into your program. You will notice a new line has been added to the main loop of the program that reads GOSUB _control_player. If you look further down the program, you will find the subroutine responsible for controlling the player.

Replace the line REM * HERE A * to read:
rem Handle movement keys
if leftkey()=1 then shipx#=shipx#-3.0
if rightkey()=1 then shipx#=shipx#+3.0

In order to move the ship via the arrow keys, we must use what is called a condition. The IF command uses conditions to decide whether or not to perform an action associated with it. If the condition is true, the IF command will run the commands placed after the THEN statement. If the condition is false, the commands after the THEN statement are ignored. If the left arrow key is pressed, the LEFTKEY() command will return a value of one, otherwise zero is returned. The same applies to the RIGHTKEY() command.

In order to control the horizontal position of our ship, we are using a variable called SHIPX#. The statement SHIPX#=SHIPX#-3.0 will deduct three from the current value stored in the variable, SHIPX#=SHIPX#+3.0 will add three.

Replace the line REM * HERE B * to read:
rem Position the ship
position object 50,shipx#,-90,-10

This command will take the value stored in SHIPX# and use it to alter the horizontal position of your ship. Press F5 to put the ship under your control!

Click Here For The Next Tutorial Creating A Single Alien.