4. Winning and Losing at Cave Surfing
In this video, you’ll program winning and losing conditions for your game.
To start, program a winning condition. The finish line in this game is green. The player will win if the sprite touches the color green. Click the player 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. The sprite could play a sound, say something, switch to a celebratory costume that you drew… anything you’d 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 black.
From the control menu, drag an if statement into the “forever” block. Select the sensing menu, and drag out a “touching color” block as the condition. Click the color swatch, then the black 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 a “stop all” block from control. If would you like, add more activity to the losing condition. For example, change the costume to an explosion, play a sound, or use a say bubble to tell the player they lost. Add any other code for the losing condition above the “stop all” block.
As your last step today, share your project with the Scratch community so others can experience your creative work. Don’t forget to add a description and some tags.
Now, it’s your turn: Program winning and losing conditions by adding “if,” “touching color,” and “stop all” blocks.
Add features to your winning and losing conditions to make gameplay more exciting!