Winning the game
Just one more thing to do to finish the game. At the moment, the enemies can win the game by destroying the base. But the player can't really win the game. There's no clear end state to kill all the enemies. To finish the game, we will determine fixed winning and losing conditions and add a game screen to clearly tell the player whether they have won or lost the game.
Prepare for lift off
Let's create the screen graphic first by following the given steps:
- Click on the paintbrush icon to create a new sprite.
- Choose a dark yellow color and draw a large rectangle.
- Click on Fill to fill the rectangle with a light yellow color.
- Select the Text tool and write the text
YOU WIN!
in the center of the rectangle. - Duplicate the costume by selecting Duplicate.
- Edit the text in the second costume to read
GAME OVER
.
Engage thrusters
To show the game screen at the right moment and to display the proper costume, we will use a script as shown in the following steps:
- Click on the Scripts tab to add some scripts to the game screen.
- Start a new when <green flag> clicked script.
- Attach a go to x: 0 y: 0 block to place the screen at the center of the stage.
- Then, hide it to start the game without displaying the game screen.
- Start a second script with a when I receive … block.
- Create the new message
game over
. - Add a switch costume to <costume 2> block.
- Then display the game screen by clicking on show.
- Place a stop <all> block to stop all scripts to end the game.
Scripting for the win condition is a bit more work. When does the player win the game? When all the enemies have been killed or have entered the base while the base has not yet been destroyed. The last part of that condition is already taken care of because the scripts we have just written will put the game in the game over state as soon as the base is destroyed. If we haven't reached the game over state yet, the base must still be "alive".
So we only have to take care of counting the enemies and check whether we have reached the total yet.
First, count the total number of enemies that are spawned during the game. If you've used the numbers mentioned earlier, there should be 35 enemy clones moving through the game. Remember that number. The following are the steps to count the number of enemies:
- Create another variable called
enemies
. - Use the Set enemies to
0
block at the start of the game screen <green flag> script. - We will check for the win condition at the end of the script. Add a wait until … block.
- Plug in the condition enemies =
35
. - When this condition has been met, the script continues with switch to costume <costume1>.
- Click on Show to make the sprite visible.
- Use stop <all> to stop all scripts to end the game.
- Now let's start counting so the win condition can be met.
- Go to the red enemy's Scripts tab.
- Add a change <enemies> by
1
block in both clone scripts, just before a clone is destroyed. This counts all the enemies as they are killed or when they have reached the base. - Also add these blocks to the blue and yellow enemies. If you forget some, the script will never count to 35 and the player can't win. The finished game screen script should look like the following screenshot:
Objective complete – mini debriefing
Test the game to see whether you performed all the steps correctly. Temporarily show the enemies variable by checking the boxes to see if the counting works. Try killing all the enemies to get to the win condition. Also, test to see whether the enemies destroying the base will get the game to the game-over condition.