1, 2, 3, code ! - Cycle 3 activities - Step 2.7. Ending the game: "Game over"

Summary

Students complete their program by introducing a test on the number of remaining lives: the message "game over" appears and the program stops when no more lives remain.

Key ideas
(see Conceptual scenario)

"Machines"

  • The machines all around us simply follow orders (instructions).
  • By combining basic instructions, we can make them execute complex tasks.

"Algorithms"

  • An algorithm is a method used to resolve a problem.
  • A loop allows the same action to be repeated multiple times.
  • Certain loops, known as “infinite loops,” never stop.
  • Certain loops, known as “iterative loops,” are repeated a predefined number of times.

"Languages"

  • Scratch is a graphical programming environment that uses a simple language.
  • A program is the expression of an algorithm in a programming language.
  • Certain instructions are only executed when an event is triggered. This is known as event-driven programming.
  • Certain instructions are executed one after the other. This is known as sequential programming.
  • The execution of a program is reproducible (if neither the instructions nor the data to manipulate change, the program always gives the same result).

Equipment

For each student pair

  • A computer with Scratch and the program saved from the previous lesson

The students must now make the game stop when there are no more lives. This requires several things:

  • Make "game over" appear
  • Make it so the game can no longer be played (unless it is relaunched)

 

 Activity 1: Make "game over" appear when there are no more lives (15 minutes)

There are several ways to make "game over" appear. The easiest way is to import a sprite that takes the appearance of "game over" (sprite provided). This sprite appears when the number of lives reaches zero.

This is very easy to do using the instruction "Show" in the "Looks" category. Note that you must remember to hide the "game over" sprite when the program is launched!

The program for this "game over" sprite is:

 


"Game over" sprite program

 

Teaching note:

It is also possible to import a "text" sprite from the Scratch library. This sprite is called "awesome!". By clicking on the "Costumes" tab, you can change the color and font as well as edit the text.

 

Activity 2: Stopping the game when "game over" appears (15 minutes)

As the program stands now, users can continue playing even when "game over" appears, which is not the desired outcome. To end the game, there are several strategies:

Method 1

Trigger the end of all programs when the number of lives reaches zero. This is done using the "Stop all" command from the "Control" category.

 


Rover program

 

In theory, this should stop the game. However, in practice, a bug in Scratch (not yet fixed at time of printing) means that despite using "Stop all," certain programs will continue to run (it is still possible to move the rover). As a result (and because it is visually more appealing), we prefer the second method described below.

Method 2

Make all the other sprites disappear when the number of lives reaches zero. With this solution, only the "game over" sprite remains on the screen, so the game is truly over. This method involves having one of the programs (e.g., the rover) send a message. This message is called simply "game over."

All the sprites (except the "game over" sprite) are hidden when they receive this message. Because they are told to hide at the end, you must remember to ask them to appear again when the program is launched.

 


Rover program

"Game over" sprite program

 The final instruction of the rover program (hide when "game over" is received) is also in the other sprites' programs (except the "game over" sprite, which appears at that moment).

Teaching notes:

  • A simplified variation of Method 2 is available if you do not want to send or receive messages. This variation consists in asking each sprite to hide when the number of lives reaches zero.

The one-second pause ensures that the "lives" variable in the rover program had the time to be initialized at 3. Otherwise, since its value is 0 when the program launches, the sprites will hide immediately.

  • Introducing a pause, even for a fraction of a second, is a workaround for the small bugs linked to the syncing of different programs. Scratch gives the illusion of running all programs simultaneously, but in fact, it runs them one after another (very quickly, which is why it appears to be simultaneous). Introducing a pause to send/receive a message is a way to force a program to run before another.

 

Conclusion and lesson recap activity

The students update the list of Scratch instructions they know.


 

 


<< Step 2.6 Sequence II Step 2.8 >>

 

Project partners

Aucun résultats