By clicking the plus sign of a game over block, you can let the game know whether or not the player won.
This will change the dialog shown on the game over screen.
In our games we will often want to compare values, and take an action based on the result of the comparison.
Example: Is my value smaller than a test value? If it is true that my value (3) is smaller than the test value (5) then we will add to my value. Otherwise, we will subtract from the value.
We could do a comparison test to see:
if and else statements allow us to make our programs behave in different ways based on the state of the game.
In this activity, students will work with:
We have seen in the previous lesson if statements perform a test and if the logic test evaluates to true, then it will run code that is given.
When we use an if statement, we have the option to add an else statement. An else block will only run in the event that the logic test given evaluates to false. In other words, if the test is true, then the if block’s code will run, else, the else block’s code will run.
To use an else click on the plus sign of an existing if block.
Using an if block with an else block allows us to split all comparisons into two categories - either the comparison is true or false (not true).
What if we needed to split a comparison into three or four categories?
We can use the else if block to add additional comparisons.
By clicking the plus sign of an if else block, an else if block will appear. This allows for another logic test that splits the cases after the original logic test evaluates to false.
We can compare the score with 3 possible results.
For example, consider the case where we want to split scores into three groups: beginner, intermediate, and expert.
- if score greater than 100 “you are an expert”
- or else if greater than 50 “you are intermediate”
- or else, “you are a beginner”
This code will first check if the high score for the game is greater than 100. If it is, then it will identify the player is an “expert” and skip the rest of the comparison tests.
If it is not greater than 100, then the second logic test is run to see if the score is greater than 50. If it is, then it will identify the player as “intermediate.”
If it is not greater than 50, it will run the else section and rank the player a “beginner.”
Make sprite alternate between saying “A” and “B”
For each example below,
Now that we have the basic functionality of our game, make it so that the player loses points when they press the wrong button.
Now add a timer, and congratulate the player at the end of the game, giving them a specific message based on their score.
20
. If it is, use a splash block to say “Beginner score of “ and then the player’s score20
but say “Pro score of “ and then the player’s scoreUse a game over block to let the game know that it is over and that the player won
Challenge: make the sprite have a shake or bump effect each time it has a say so can see when letter updates even when it is the same as the previous time.