The player is on the left half of the screen if their ||sprites:x position||
is less than half of the screen width
Activity: Logic Intro
Logic in software development allows for flexible programs that respond appropriately to different conditions. In this section, we will identify how comparisons can be used within our code.
In these activities, students will work with:
||logic:if||
block- Comparison operators
Concept: Comparisons
Comparison logic with the ||logic:if||
statement
In our games we will often want to compare values and take an action when the comparison statement is true.
Example: Is laserPower (8) greter than enemyShieldStrength (5)? If it is
||logic:true||
that laserPower is greater than enemyShieldStrength, then we will add 1 to numEnemiesDestroyed.
We perform comparison tests with ||logic:if||
We have already seen similar behaviors in events like ||sprites:on overlap||
. If the two sprites were overlapping and not ghosts, then the code for the event will run (for example, the score might increase).
||logic:if||
statements allow us to program a behavior based on the state of the game.
Concept: ||logic:if||
Statement
||logic:if||
statements perform a test, and if the test evaluates to true, then it will run code that is given. Some further examples are:
- if the score is greater than 10, then give additional countdown time
- if the player has 0 lives left, then game over
These are what are known as comparisons because they compare the value of two things.
To use an ||logic:if||
statement block, we must fill it with a comparison test. If the test is true the code in the block will run. Below is a comparison to see if high score is greater than 5.
if (info.highScore() > 5) {
game.splash("Good job!")
}
Concept: Comparison Operators
When we make comparisons, we have two numbers, in a specific order, and what is know as a comparison operator. A comparison operator allows us to specify what type of comparison we are doing. Some basic ones are:
||logic:>||
(Greater than): determines whether the first value given represents a quantity that is larger than the second value||logic:<||
(Less than): determines whether the first value given represents a quantity that is smaller than the second value||logic:=||
(Equal to): determines whether the two values given represent the same quantity
Example #1: Less than
- Play the game linked below
- Review the code that uses comparisons
- Look at how it uses
||logic:if||
logic and a less than comparison to modify the game
let mySprite: Sprite = null
let projectile: Sprite = null
sprites.onDestroyed(SpriteKind.Enemy, function (sprite) {
info.changeScoreBy(1)
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
game.over(false)
})
mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . 7 7 . . . . . . . . . . .
. . . 7 7 7 7 . . . . . . . . .
. . . 7 . . 7 7 7 . . . . . . .
. . . 7 . . . . 7 7 7 . . . . .
. . . 7 . . . . . . 7 7 7 . . .
. . . 7 . . . . . . . . 7 7 7 .
. . . 7 . . . . . . 7 7 7 . . .
. . . 7 . . . . 7 7 7 . . . . .
. . . 7 . . 7 7 7 . . . . . . .
. . . 7 7 7 7 . . . . . . . . .
. . . 7 7 . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
controller.moveSprite(mySprite, 0, 50)
mySprite.x = 8
mySprite.setStayInScreen(true)
info.setScore(0)
game.onUpdateInterval(500, function () {
projectile = sprites.createProjectile(img`
. . . . . . . . . c c 8 . . . .
. . . . . . 8 c c c f 8 c c . .
. . . c c 8 8 f c a f f f c c .
. . c c c f f f c a a f f c c c
8 c c c f f f f c c a a c 8 c c
c c c b f f f 8 a c c a a a c c
c a a b b 8 a b c c c c c c c c
a f c a a b b a c c c c c f f c
a 8 f c a a c c a c a c f f f c
c a 8 a a c c c c a a f f f 8 a
. a c a a c f f a a b 8 f f c a
. . c c b a f f f a b b c c 6 c
. . . c b b a f f 6 6 a b 6 c .
. . . c c b b b 6 6 a c c c c .
. . . . c c a b b c c c . . . .
. . . . . c c c c c c . . . . .
`, -150, 0, SpriteKind.Enemy)
projectile.setPosition(scene.screenWidth(), randint(0, scene.screenHeight()))
if (info.score() < 10) {
projectile.vx += 100
}
})
When the game creates a new enemy, it checks to see if the player’s score is less than a certain value.
If that is the case, the player has just started playing the game, so the game makes it easier for the player by decreasing the speed in which the projectiles are thrown at the player.
Student Task #1a: Scoring
- Create a new project
- Create a
||sprites:Sprite||
- When the player presses the
||controller:A||
button, increase the score by 1 - When the player presses the
||controller:B||
button, if the player’s score is less than 10, make the||sprites:Sprite||
say something
Student Task #1b: Sprite Position
- Create a new project
- Create a sprite, and use
||controller:move mySprite with buttons||
to make it move when the directional keys are pressed - When the player is on the left half of the screen and the player presses the
||controller:A||
button, make the score increase by 1
Example #2: Greater Than
- Play the game linked above
- Review the code that uses comparisons
- Look at how it uses
||logic:if||
logic and a greater than comparison to modify the game
namespace SpriteKind {
export const Cherry = SpriteKind.create();
}
let projectile: Sprite = null
let mySprite: Sprite = null
sprites.onOverlap(SpriteKind.Player, SpriteKind.Cherry, function (sprite, otherSprite) {
info.changeScoreBy(1)
otherSprite.destroy()
if (info.score() > 5) {
mySprite.sayText("Too many cherries")
}
})
mySprite = sprites.create(img`
. . . . . . 5 . 5 . . . . . . .
. . . . . f 5 5 5 f f . . . . .
. . . . f 1 5 2 5 1 6 f . . . .
. . . f 1 6 6 6 6 6 1 6 f . . .
. . . f 6 6 f f f f 6 1 f . . .
. . . f 6 f f d d f f 6 f . . .
. . f 6 f d f d d f d f 6 f . .
. . f 6 f d 3 d d 3 d f 6 f . .
. . f 6 6 f d d d d f 6 6 f . .
. f 6 6 f 3 f f f f 3 f 6 6 f .
. . f f d 3 5 3 3 5 3 d f f . .
. . f d d f 3 5 5 3 f d d f . .
. . . f f 3 3 3 3 3 3 f f . . .
. . . f 3 3 5 3 3 5 3 3 f . . .
. . . f f f f f f f f f f . . .
. . . . . f f . . f f . . . . .
`, SpriteKind.Player)
controller.moveSprite(mySprite, 100, 100)
mySprite.setStayInScreen(true)
game.splash("Collect 5 cherries")
game.onUpdateInterval(500, function () {
projectile = sprites.createProjectile(img`
. . . . . . . . . . . 6 6 6 6 6
. . . . . . . . . 6 6 7 7 7 7 8
. . . . . . 8 8 8 7 7 8 8 6 8 8
. . e e e e c 6 6 8 8 . 8 7 8 .
. e 2 5 4 2 e c 8 . . . 6 7 8 .
e 2 4 2 2 2 2 2 c . . . 6 7 8 .
e 2 2 2 2 2 2 2 c . . . 8 6 8 .
e 2 e e 2 2 2 2 e e e e c 6 8 .
c 2 e e 2 2 2 2 e 2 5 4 2 c 8 .
. c 2 e e e 2 e 2 4 2 2 2 2 c .
. . c 2 2 2 e e 2 2 2 2 2 2 2 e
. . . e c c e c 2 2 2 2 2 2 2 e
. . . . . . . c 2 e e 2 2 e 2 c
. . . . . . . c e e e e e e 2 c
. . . . . . . . c e 2 2 2 2 c .
. . . . . . . . . c c c c c . .
`, randint(-30, 30), randint(-30, 30), SpriteKind.Cherry)
})
When the player collects a cherry, if the player has collected more than 5, then the sprite says “Too many cherries”.
Student Task #2: Follow me!
- Start a new project
- Create 2 sprites, a leader and a follower
- Set the
||sprites:x position||
of the leader at a random value between 100 and 140 and set the||sprites:x position||
of the follower at 20. - Make it so that when the player presses the
||controller:A||
button, if the leader’s||sprites:x position||
is greater than the follower’s, then make the follower change their||sprites:x position||
by 10
Example #3: Equality
- Play the game linked above
- Review the code that uses comparisons
- Look at how it uses
||logic:if||
logic and an equality comparison to modify the game
let projectile: Sprite = null
let mySprite: Sprite = null
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
otherSprite.destroy()
info.changeLifeBy(-1)
if (info.life() == 1) {
mySprite.say("Last chance")
mySprite.setImage(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . 2 2 . . . . . . . . . . .
. . . 2 2 2 2 . . . . . . . . .
. . . 2 . . 2 2 2 . . . . . . .
. . . 2 . . . . 2 2 2 . . . . .
. . . 2 . . . . . . 2 2 2 . . .
. . . 2 . . . . . . . . 2 2 2 .
. . . 2 . . . . . . 2 2 2 . . .
. . . 2 . . . . 2 2 2 . . . . .
. . . 2 . . 2 2 2 . . . . . . .
. . . 2 2 2 2 . . . . . . . . .
. . . 2 2 . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
`)
}
})
mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . 7 7 . . . . . . . . . . .
. . . 7 7 7 7 . . . . . . . . .
. . . 7 . . 7 7 7 . . . . . . .
. . . 7 . . . . 7 7 7 . . . . .
. . . 7 . . . . . . 7 7 7 . . .
. . . 7 . . . . . . . . 7 7 7 .
. . . 7 . . . . . . 7 7 7 . . .
. . . 7 . . . . 7 7 7 . . . . .
. . . 7 . . 7 7 7 . . . . . . .
. . . 7 7 7 7 . . . . . . . . .
. . . 7 7 . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
controller.moveSprite(mySprite, 100, 100)
mySprite.setStayInScreen(true)
info.setLife(3)
forever(function () {
projectile = sprites.createProjectile(img`
. . . . . . . . . c c 8 . . . .
. . . . . . 8 c c c f 8 c c . .
. . . c c 8 8 f c a f f f c c .
. . c c c f f f c a a f f c c c
8 c c c f f f f c c a a c 8 c c
c c c b f f f 8 a c c a a a c c
c a a b b 8 a b c c c c c c c c
a f c a a b b a c c c c c f f c
a 8 f c a a c c a c a c f f f c
c a 8 a a c c c c a a f f f 8 a
. a c a a c f f a a b 8 f f c a
. . c c b a f f f a b b c c 6 c
. . . c b b a f f 6 6 a b 6 c .
. . . c c b b b 6 6 a c c c c .
. . . . c c a b b c c c . . . .
. . . . . c c c c c c . . . . .
`, -100, randint(-50, 50), SpriteKind.Enemy)
pause(500)
})
When the player is on their final life, the image of the sprite changes and the sprite says “Last chance”.
Student Task #3: Count to 10
- Create a new project
- Make it so that when the player presses the
||controller:A||
button the score increases by 1 - Make it so that after the score is increased, if the score is equal to 10, use the
||game:game over||
block to end the game
Example #4: Multiple ||logic:if||
Comparisons
- Play the game linked below
- Review the code that uses comparisons
- Look at how it uses
||logic:if||
logic and multiple comparisons to modify the game
let mySprite: Sprite = null
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
if (mySprite.x < scene.screenWidth() / 2) {
info.changeScoreBy(1)
}
if (mySprite.y < scene.screenHeight() / 2) {
info.changeLifeBy(-1)
}
})
mySprite = sprites.create(img`
. . . . . . 5 . 5 . . . . . . .
. . . . . f 5 5 5 f f . . . . .
. . . . f 1 5 2 5 1 6 f . . . .
. . . f 1 6 6 6 6 6 1 6 f . . .
. . . f 6 6 f f f f 6 1 f . . .
. . . f 6 f f d d f f 6 f . . .
. . f 6 f d f d d f d f 6 f . .
. . f 6 f d 3 d d 3 d f 6 f . .
. . f 6 6 f d d d d f 6 6 f . .
. f 6 6 f 3 f f f f 3 f 6 6 f .
. . f f d 3 5 3 3 5 3 d f f . .
. . f d d f 3 5 5 3 f d d f . .
. . . f f 3 3 3 3 3 3 f f . . .
. . . f 3 3 5 3 3 5 3 3 f . . .
. . . f f f f f f f f f f . . .
. . . . . f f . . f f . . . . .
`, SpriteKind.Player)
controller.moveSprite(mySprite, 100, 100)
info.setLife(5)
Notice how when the player presses the ||controller:A||
button, if they are on the left half of the screen, the score will increase, and if they are on the top half of the screen, their lives will decrease.
Student Task #4: Equal and Greater Than
- Create a new project
- Create a
||sprites:sprite||
- Make it so that when the player presses the
||controller:A||
button, the score increases by 1 - In this event, if the player’s score is above 10, make the sprite congratulate the player on their high score
- In the same event, if the player’s score is an even number, change the background to a random color
- Challenge: add projectiles that fire from the
||sprites:sprite||
when the score is increased to a value greater than 5 - Challenge: if the score reaches 20, change the
||sprites:sprite||
‘s image
What did we learn?
- What is an
||logic:if||
statement? What is a case in which you would use one? - What is a logic comparison? What is a case in which you would use one?