Adding The Level Loop
Click here to load the completed tutorial directly into the editor
SWARM-23.DBA. If you want to step through and make the modifications, click here to load SWARM-22.DBA.
At the moment we have a variety of combined systems that resembles a game put does not feel like one. The reason for this lies in the lack of a win / lose scenario that makes or breaks a game. The player has to feel challenged, but given sufficient reason to continue playing the game. We are now going to implement a level loop. What we currently use as the main loop can be thought of as our level loop, controlling the element for our game. The main difference is that a level loop will be permitted to exit if one of two conditions become true. We must exit the loop if either the player runs out of lives, or all the enemies are destroyed.
Replace the line REM * HERE M * to read:
rem Destroy player
gosub _destroy_player
Replace the line REM * HERE N * to read:
rem Destroy player
gosub _destroy_player
Replace the line REM * HERE K * to read:
rem Reposition ship and deduct lives
shipx#=rnd(200)-100
dec lives
As the destruction of the ship occurs several times throughout the program, it makes sense to create a _destroy_player subroutine for this event rather than repeating the code. You will notice the DEC command decrements the LIVES variable by one. For this command to make sense, we need to see our lives.
Replace the line REM * HERE B * to read:
rem Starting lives
lives=3
Replace the line REM * HERE A * to read:
rem Setup text
set text size 70
set text font "Arial"
Replace the line REM * HERE J * to read:
rem Display lives
text 8,410,str$(lives)
The first command sets our LIVES variable to a value of 3 before we enter our main loop. The second addition sets the font type and size of our text we shall use to display the number of lives on screen. The third addition writes our number of lives to the bottom left corner of the screen.
The TEXT command can only write a string to the screen. A string is one of three data types available to us. We have an integer, which holds whole values, real numbers that hold values with fractional parts, and strings, which hold characters and words. In order to display our value to the screen, we use the STR$ command to convert our integer LIVES value to a string, and then pass this string to the TEXT command to display.
If you press F5, you will see the lives counter decrement each time we get hit.
You will notice that our program has two main loops. We have a DO command near the top of the program and another DO command further down at the start of the game element control code. This inner loop is to be replaced by our level loop that will exit when either we run out of lives or the aliens are defeated.
Replace the line DO : REM * HERE C * to read:
rem Level loop
while lives>=0 and destroyed
Replace the line LOOP : REM * HERE D * to read:
rem End of level loop
endwhile
The WHILE command allows us to exit the loop when one or more conditions are met.
In this case, we will remain in the loop for as long as our LIVES value is greater or equal to zero and also while the number of destroyed aliens number less than the total number of aliens. The ENDWHILE command marks the end of the while loop, much like LOOP marks the end of a do loop.
Replace the line REM * HERE H * to read:
rem Destroy all objects
for o=1 to 1000
if object exist(o)=1 then delete object o
next o
Replace the line REM * HERE I * to read:
rem Release arrays
undim bullet#(0)
undim enemy#(0)
Without these next two additions our program will run into problems. We now have a situation where our main loop now encompasses the creation of the player, alien and bullet objects. When our level loop exits, for whatever reason it will naturally reach the end of the main loop and repeat everything. The problem lies in the fact that the program will attempt to create objects that have already been created, causing a runtime error.
The first addition scans the first thousand object numbers, and deletes any objects that exist within this range. This ensures we delete every object from our game. The second addition releases the memory consumed when we created the ENEMY and BULLET arrays. We do this for a very clever reason. In order to reset everything in our game, we ideally want every value reset to zero in order than a repeat game is identical to the first one. When arrays are released, and recreated their internal data values are reset to zero.
When you run the program, you will notice nothing much seems to happen when your player runs out of lives. The reason for this lies in the fact that the lives variable has not been reset to three. We only want to reset the lives to three if the game is over, and so we need some code to detect for this.
Replace the line REM * HERE F * to read:
rem Handle game result
if lives<0
lives=3 : s$="GAME OVER"
else
inc lives : s$="NEXT LEVEL"
endif
Replace the line REM * HERE G * to read:
rem Interlude message
while inkey$()=""
center text 320,240,s$
sync
endwhile
Replace the line REM * HERE E * to read:
rem Wait for key release
while inkey$()<>"" : sync : endwhile
The first addition determines whether you won or lost the game. If you have no more lives, the string variable S$ is filled with the text "GAME OVER". If you still have lives, the string variable S$ is filled with "NEXT LEVEL". As a bonus, every level completed adds a life to the players count. The second addition ensures the message stored in S$ is displayed for as long as no key is pressed. The SYNC command keeps the screen updating while the message is being displayed within the mini-loop.
The third addition is placed just after the level loop exits, and waits until the player has released all key presses. If we did not have this code, any key presses used in the game will be used to quickly exit the message loop and the user would only see the message for a split second.
Replace the line REM * HERE O * to read:
rem Aliens destroyed counter
destroyed=0
Replace the line REM * HERE L * to read:
rem Increment destroyed counter
inc destroyed
These last two additions are used to count the number of aliens the player succeeds in destroying. If you recall, our level loop will exit when the DESTROYED variable equals the total number of aliens in our game.
Click Here For The Next Tutorial
Adding A Score.