4. Winning and Losing at Cave Surfing


In this video, you'll program winning and losing conditions for your game.

To start, program on winning condition.

The finish sign in this game is green.

The player will win if the sprite touches the color green.

Click the sprite.

Drag an "if" block into the forever loop.

And add a "touching color" block from sensing.

To select the finish line color, play the game until the finish line is on the stage.

Then select the color swatch.

And click the finish line.

If the sprite reaches the finish line, the game should stop.

Add a "stop all" block from Control.

Program something awesome to happen when the sprite wins.

Add code above the stop all block.

This sprite could play a sound, say something, switch to a celebratory costume that you drew, anything you like.

Next, program a losing condition.

Gameplay should stop if the player touches the edges of the cave.

Notice that the edges of the cave are all black lines.

To program a losing condition, program the game to check if the sprite is touching block.

From the Control menu, drag an "if" statement into the forever block.

Select the sensing menu and drag all the touching colored block as the condition.

Click the color swatch, then the block cave edges to select it.

If the player touches the cave edges, all code in the game should stop.

To stop all code in the program, use the stop all block from control.

If you would like, add more to lead to the losing condition.

For example, change the costume to an explosion.

Put a sound, or use the say bubble to tell the player, they lost.

Add any other code for the losing condition above the stop all block.

Then go to xy block, and place it at the top of the stack.

And enter where your sprite should start.

As your last step today, share your project with the Scratch community so others can experience your creative work.

Be sure to write a description that tells people how to use your program.

Now it's your turn.

Program winning and losing conditions, by adding "if", "touching color" and "stop all" blocks.

Then, add features to your winning and losing conditions to make gameplay more exciting.


  1. Program a winning condition for the player sprite. (hint: use if statements and color sensing blocks)
  2. Program a losing condition for the player sprite. (hint: the cave walls all have a black border)
  3. Program the game to reset when someone wins or loses. (hint: use a stop-all block)