arrow_back It's How You Play the Game
In this video, you’ll learn how to add a winning condition to your game.
In the sprites area, click on the stage.
You’ll program the Win/Lose condition on the backdrop, rather than on one of the sprites.
Start by programming the winning condition.
First, choose a winning number of points..
This example uses 20.
You and your friend will win if the score reaches 20.
From the control menu, drag out an “if/then” block.
Remember, an “if” statement, or conditional, checks whether something is true.
Next, fill in the condition for the computer to check.
In this case, if the score is ever greater than 19, the players will have won.
From the operators menu, place a “greater than” block inside the diamond-shaped space in the “if/then” block.
On the right side of the greater than block, type the score just before the winning one.
In this example, that’s 19.
From the data menu, place the “score” variable block in the left side of the “greater than” block.
If the players have won, the backdrop will change.
From the “looks” menu, place the “switch backdrop to” block inside the “if” block.
Click on the dropdown, and select “winning backdrop.”
Next, make the game constantly check if you and your friend have won.
From the “control” menu place a “forever” loop around the “if/then” block.
The game will start checking if you and your friend won as soon as the flag is clicked.
From the “events” menu, place a “when flag clicked” block above the forever loop.
Click on the green flag to test your code.
Then, earn a winning score with your friend.
The backdrop changes!
But wait… objects are still falling from the sky.
To end the game, from the “control” menu place the “stop all” block under the “switch backdrop to” block.
Test the code by clicking the green flag again.
When you get the high score, the backdrop changes, and the objects stop dropping.
Next, follow similar steps to add a losing condition..
Make the program check if the score is equal to a negative number, and, if it is, switch the backdrop to the losing backdrop.
If you need help programming the losing condition, ask a neighbor or put up your sticky to get your Guru’s attention.
Don’t worry if your program doesn’t work the first time.
Computer scientists like you rarely get their programs to work on their first try.
They use courage and persistence to learn from their mistakes and keep trying!
Now it’s your turn: Let the players know they’ve won using “if” “greater than” “score” and “switch backdrop to” blocks.
Make the game constantly check if the players have won using a forever loop.
Stop the game when the players win using “stop all.”