Creating Multiple Aliens

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

To create a swarm of aliens, we simply have to repeat the process of creating individual aliens, but in formation. To do so, we are going to use a controlled loop and an array.

Replace the line REM * HERE B * to read:
rem Make many aliens
gosub _create_aliens

Replace the line REM * HERE E * to read:
rem Loop through eight alien columns
for x=0 to 7

Replace the line REM * HERE J * to read:
rem End of column loop
next x

In our subroutine _create_aliens we are going to create four rows, with eight aliens in each row.

The FOR and NEXT commands you have added creates a controlled loop within your program. When the program pointer reaches the NEXT command, it will jump back to the FOR command and repeat the code that sits between these two commands. Each time the loop occurs, the variable X is incremented by one. The loop ends when the X variable is 7.

Replace the line REM * HERE F * to read:
rem Calculate number for the alien object
objid=100+x+(row*8)

Replace the line REM * HERE G * to read:
rem Create a single alien
make_single_alien(objid)

By placing code inside the FOR NEXT loop, we are able to repeat the process of creating individual aliens. The variable OBJID uses a formula to calculate a unique number for our new alien object. We can use the incrementing X variable to help us create a different object number for each pass within the loop. We then use our user-defined function to create a single alien using this unique object number.

At this point, you can run the program to see your aliens. The reason you only see one alien is because they are all overlapping each other perfectly.

The next thing we need to do is position them, and for this we shall use an array.

Replace the line REM * HERE A * to read:
rem Declare global array
dim enemy#(40,5)

Replace the line REM * HERE C * to read:
rem Reset total aliens counter
enemymax=0

Replace the line REM * HERE H * to read:
rem Add alien details to enemy array
inc enemymax
enemy#(enemymax,1)=objid
enemy#(enemymax,2)=(x-3.5)*25
enemy#(enemymax,3)=(row+1.0)*20

Arrays are storage areas for large amounts of data. Where a variable is useful for storing a single value, arrays are ideally suited to storing multiple values. The DIM command creates an array by specifying how large the storage area should be. The parameters 40,5 indicate the storage area should be large enough to keep the data for 40 aliens, and each alien should have storage for 5 items of data.

The variable ENEMYMAX is used to point inside our array in order write the alien data into the right slot. By resetting this variable to zero before we start, we are making sure that we are going to start writing at the beginning of the array. The INC command is used to increment the ENEMYMAX variable so that each new alien has a unique place in the array. In the same way we assign a value to a variable, we can assign values to an array. In addition to the array name, we must provide the exact location within the array to place the value. For each unique alien indicated by the variable ENEMYMAX, three items of data are stored.

The OBJID variable value is stored in item slot 1. A horizontal position is stored in item slot 2 and a vertical position is stored in slot 3.

Replace the line REM * HERE I * to read:
rem Update alien position
position object objid,enemy#(enemymax,2),enemy#(enemymax,3),0

Press F5 and you will now see our eight aliens are forming a line. We are using the horizontal and vertical position data stored in the ENEMY array to position each of the alien objects.

Replace the line REM * HERE D * to read:
rem Loop through four rows
for row=0 to 3

Replace the line REM * HERE K * to read:
rem End of row loop
next row

Press F5 again and you will have four rows of eight aliens.

As the original FOR NEXT loop occurs inside the newly created loop, we can repeat the creation of the alien line four times. The aliens could then be spaced out vertically by using the ROW variable when we generated the vertical position data.

Click Here For The Next Tutorial Controlling The Aliens.