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
||logic: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:
||logic:if||
and ||logic: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:
||logic:if||
||logic:else||
||logic:else if||
||logic:if||
and ||logic:else||
StatementWe have seen in the previous lesson ||logic:if||
statements perform a test and if the logic test evaluates to true, then it will run code that is given.
if (info.life() > 2) {
game.splash("3 or more lives")
}
When we use an ||logic:if||
statement, we have the option to add an ||logic: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 ||logic:if||
block’s code will run, else, the ||logic:else||
block’s code will run.
To use an ||logic:else||
click on the plus sign of an existing ||logic:if||
block.
if (info.highScore() > 5) {
game.splash("Good Luck!")
} else {
game.splash("Set High Score!")
}
||logic:else if||
StatementUsing an ||logic:if||
block with an ||logic:else||
block allows us to split all comparisons into two categories - either the comparison is ||logic:true||
or ||logic:false||
(not true).
What if we needed to split a comparison into three or four categories?
We can use the ||logic:else if||
block to add additional comparisons.
By clicking the plus sign of an ||logic:if else||
block, an ||logic: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”
if (info.score() > 100) {
game.splash("you are an expert!")
} else if (info.score() > 50) {
game.splash("you are intermediate!")
} else {
game.splash("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,
let mySprite: Sprite = null
let randomPick = 0
function generate() {
randomPick = randint(0, 1)
if (randomPick == 0) {
mySprite.say("push A")
}
if (randomPick == 1) {
mySprite.say("push B")
}
}
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
generate()
})
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
generate()
})
game.splash("Push A or B as directed")
mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . 4 4 4 4 4 4 4 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 . e . . e . 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 4 . 1 1 . 4 4 . . .
. . . . . . 4 4 4 4 4 4 . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
info.startCountdown(20)
generate()
let mySprite: Sprite = null
let randomPick = 0
function generate() {
randomPick = randint(0, 1)
if (randomPick == 0) {
mySprite.say("push A")
}
if (randomPick == 1) {
mySprite.say("push B")
}
}
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
if (randomPick == 0) {
info.changeScoreBy(1)
}
generate()
})
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
if (randomPick == 1) {
info.changeScoreBy(1)
}
generate()
})
game.splash("Push A or B as directed")
mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . 4 4 4 4 4 4 4 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 . e . . e . 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 4 . 1 1 . 4 4 . . .
. . . . . . 4 4 4 4 4 4 . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
info.startCountdown(20)
generate()
||logic:else||
let mySprite: Sprite = null
let randomPick = 0
function generate() {
randomPick = randint(0, 1)
if (randomPick == 0) {
mySprite.say("push A")
} else {
mySprite.say("push B")
}
}
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
if (randomPick == 0) {
info.changeScoreBy(1)
}
generate()
})
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
if (randomPick == 1) {
info.changeScoreBy(1)
}
generate()
})
game.splash("Push A or B as directed")
mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . 4 4 4 4 4 4 4 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 . e . . e . 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 . . . . . . 4 . . .
. . . . . 4 4 . 1 1 . 4 4 . . .
. . . . . . 4 4 4 4 4 4 . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
info.startCountdown(20)
generate()
||logic:else||
with an Incorrect ResponseNow that we have the basic functionality of our game, make it so that the player loses points when they press the wrong button.
||logic:else||
statements in the button press events that run code when the player enters the wrong button||info:score||
by 1 when they press the wrong buttonNow add a timer, and congratulate the player at the end of the game, giving them a specific message based on their ||info:score||
.
||info:on countdown end||
block||info:score||
is less than 20
. If it is, use a splash block to say “Beginner score of “ and then the player’s ||info:score||
||logic:else||
block to do the same for if the player’s ||info:score||
was greater than or equal to 20
but say “Pro score of “ and then the player’s ||info:score||
Use 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.
||logic:if||
but not an ||logic:else||
?||logic:if||
- ||logic:if||
structure to an ||logic:if else||
structure. Why does it make sense to do this?if (info.score() > 10) {
game.splash("Case 1")
} else {
if (info.score() > 5) {
game.splash("Case 2")
} else {
game.splash("Case 3")
}
}
if (info.score() > 10) {
game.splash("Case 1")
} else if (info.score() > 5) {
game.splash("Case 2")
} else {
game.splash("Case 3")
}