Controlling The Aliens

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

To give the aliens a fighting chance, and to present the player with a suitable threat, the aliens should steadily make their way down the screen.

Replace the line REM * HERE A * to read:
rem Control aliens
gosub _control_aliens

We shall use the subroutine _control_aliens to handle our alien advance. As our program continues to grow, you can appreciate how subroutines break up the logic of the game into easily digestible chunks.

Replace the line REM * HERE B * to read:
rem Move alien shift position
if alienshiftdirection=0
inc alienshiftx#,0.5
else
dec alienshiftx#,0.5
endif

Another way of using an IF command is by complimenting it with an ENDIF and optional ELSE statement. If the value stored in the variable ALIENSHIFTDIRECTION is 0 then the commands between the IF and ELSE are executed. If the IF condition is false however, the commands between ELSE and ENDIF are executed instead. In the above addition, the variable ALIENSHIFTX is incremented when the direction variable is zero, and is decremented if the direction variable is any other value.

Replace the line REM * HERE C * to read:
rem If get too far from center
if abs(alienshiftx#)>50.0

Replace the line REM * HERE E * to read:
rem End of IF statement
endif

Replace the line REM * HERE D * to read:
rem Reverse direction and drop
alienshiftdirection=1-alienshiftdirection
dec alienshifty#,8.0

The ABS command takes a value and returns the positive equivalent of it. In this case, when the variable ALIENSHIFTX grows less than -50.0 or greater than 50.0, the IF condition becomes true. When the condition is true, two things will happen. The ALIENSHIFTDIRECTION variable will have its value toggled and the ALIENSHIFTY variable will be incremented by 8. When we toggle, we make zero become one and one become zero. We toggle this variable to control the code we added earlier that controls the shifting on the horizontal position. Although we cannot see it yet, we have created the logic for the shifting of our aliens. All we need to do now is update the alien positions on screen.

Replace the line REM * HERE F * to read:
rem Update each alien object
for e=1 to enemymax

Replace the line REM * HERE I * to read:
rem End of alien update loop
next e

By creating a FOR NEXT loop, we can step through our ENEMY array, enabling us to update each alien in turn.

Replace the line REM * HERE G * to read:
rem Get the object identity of the alien
objid=enemy#(e,1)

Replace the line REM * HERE H * to read:
rem Position the alien based on the shift coordinates
position object objid,alienshiftx#+enemy#(e,2), alienshifty#+enemy#(e,3),0

Within the FOR NEXT loop, we can use the E variable to point at each alien in our ENEMY array and get the OBJID we need to find the alien object. The aliens are updated by taking the original position of the alien and adding the shift position stored in ALIENSHIFTX and ALIENSHIFTY.

Press F5 and you will now see our aliens making their way slowly down the screen.

Click Here For The Next Tutorial Creating Bullets.