4. Perdere e vincere al gioco del surf nella caverna
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 playerwill 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 finishline color, play the game until the finish line is on the stage, then select the colorswatch and click the finish line. If the sprite reaches the finish line, thegame should stop. Add a “stop all” block from control. Program something awesome tohappen when the sprite wins. The sprite could play a sound, say something, switch to a celebratorycostume that you drew… anything you’d like!
Next, program a losing condition. Gameplay should stop if the player touches the edgesof the cave. Notice that the edges of the cave are all black lines. To program a losingcondition, 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 sensingmenu, 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, allcode 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 thelosing condition. For example, change the costume to an explosion, play a sound, oruse a say bubble to tell the player they lost. Add any other code for the losing conditionabove the “stop all” block.
As your last step today, share your project with the Scratch community so others can experienceyour 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!