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:

  1. Click on the paintbrush icon to create a new sprite.
  2. Choose a dark yellow color and draw a large rectangle.
  3. Click on Fill to fill the rectangle with a light yellow color.
  4. Select the Text tool and write the text YOU WIN! in the center of the rectangle.
    Prepare for lift off
  5. Duplicate the costume by selecting Duplicate.
  6. Edit the text in the second costume to read GAME OVER.
    Prepare for lift off

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:

  1. Click on the Scripts tab to add some scripts to the game screen.
  2. Start a new when <green flag> clicked script.
  3. Attach a go to x: 0 y: 0 block to place the screen at the center of the stage.
  4. Then, hide it to start the game without displaying the game screen.
  5. Start a second script with a when I receive … block.
  6. Create the new message game over.
  7. Add a switch costume to <costume 2> block.
  8. Then display the game screen by clicking on show.
  9. 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:

  1. Create another variable called enemies.
  2. Use the Set enemies to 0 block at the start of the game screen <green flag> script.
  3. We will check for the win condition at the end of the script. Add a wait until … block.
  4. Plug in the condition enemies = 35.
  5. When this condition has been met, the script continues with switch to costume <costume1>.
  6. Click on Show to make the sprite visible.
  7. Use stop <all> to stop all scripts to end the game.
  8. Now let's start counting so the win condition can be met.
  9. Go to the red enemy's Scripts tab.
  10. 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.
  11. 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:
    Engage thrusters

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.176