For challenge: velocity can be set using the set sprite blocks
let mySprite: Sprite = null
mySprite.vx = 0
mySprite.vy = 0
Random numbers provide an element of chance to our games that make them look and feel more natural. They can also provide surprises and introduce elements of “good” or “bad” luck.
In this activity, students will use:
||math:pick random||
to generate random numbersWe can pick a random number in a range using ||math:pick random||
. First, we will use this to display a random value on the screen.
let randomNumber = 0
randomNumber = randint(0, 10)
game.splash("Random Number is " + randomNumber)
||variables:anotherRandom||
to pick a random number from 20 to 30||game:splash||
for the ||variables:anotherRandom||
variableGames often have an element of luck and surprise to keep the player engaged. In this example, we will use random numbers to place a sprite on the screen in a random location. We can use a random range because we know the dimensions of the screen.
||math:pick random||
in the sprite location block. The code does not assign the chosen random number to a variable before using itlet mySprite: Sprite = null
mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . 5 . . 5 . . 5 . . . . . .
. . . . 5 . 5 . 5 . . . . . . .
. . . . . 5 7 5 . . . . . . . .
. . . . 5 7 2 7 5 . . . . . . .
. . . . . 5 7 5 . . . . . . . .
. . . . . . 5 . . . . . . . . .
. . . . . . 1 . . . . . . . . .
. . 2 1 1 1 1 1 1 1 2 . . . . .
. . . . . . 1 . . . . . . . . .
. . . . . . 1 . . . . . . . . .
. . . . . . 1 . . . . . . . . .
. . . . . 1 . 1 . . . . . . . .
. . . . 7 . . . 7 . . . . . . .
. . . 7 . . . . . 7 . . . . . .
. . 2 2 . . . . . 2 2 . . . . .
`, SpriteKind.Player)
mySprite.setPosition(randint(15, 145), randint(15, 105))
||controller:on A button pressed||
block||controller:A||
button move a sprite to a new random position||controller:B||
button||controller:A||
button, and make the ||controller:B||
button give one of the sprites a random velocity (use small numbers that can be both positive and negative)||sprites:kind||
in the overlap eventnamespace SpriteKind {
export const Hat = SpriteKind.create();
}
let mySprite: Sprite = null
let hat: Sprite = null
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
mySprite.setPosition(randint(15, 145), randint(15, 105))
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Hat, function (sprite, otherSprite) {
sprite.say("Excuse Me!", 500)
})
hat = sprites.create(img`
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 4 4 4 4 4 4 4 4 4 4 . . . . . . . . . . . . . . .
. . . . . . . 4 7 7 7 7 7 7 7 7 4 . . . . . . . . . . . . . . .
. . . . . . . 4 7 7 7 7 7 7 7 7 4 . . . . . . . . . . . . . . .
. . . . . . . 4 7 7 7 7 7 7 7 7 4 . . . . . . . . . . . . . . .
. . . . . . . 4 7 7 7 7 7 7 7 7 4 . . . . . . . . . . . . . . .
. . . . . . . 4 7 7 7 7 7 7 7 7 4 . . . . . . . . . . . . . . .
. . . . . . . 4 7 7 7 7 7 7 7 7 4 . . . . . . . . . . . . . . .
. . . . . . . 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 . . . . . . .
. . . . . . . 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . . 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . . 6 4 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . 6 6 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . 6 6 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . 6 6 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . . 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 6 6 6 6 6 . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
`, SpriteKind.Hat)
mySprite = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . 5 . . 5 . . 5 . . . . . .
. . . . 5 . 5 . 5 . . . . . . .
. . . . . 5 7 5 . . . . . . . .
. . . . 5 7 2 7 5 . . . . . . .
. . . . . 5 7 5 . . . . . . . .
. . . . . . 5 . . . . . . . . .
. . . . . . 1 . . . . . . . . .
. . 2 1 1 1 1 1 1 1 2 . . . . .
. . . . . . 1 . . . . . . . . .
. . . . . . 1 . . . . . . . . .
. . . . . . 1 . . . . . . . . .
. . . . . 1 . 1 . . . . . . . .
. . . . 7 . . . 7 . . . . . . .
. . . 7 . . . . . 7 . . . . . .
. . 2 2 . . . . . 2 2 . . . . .
`, SpriteKind.Player)
mySprite.setPosition(randint(15, 145), randint(15, 105))
hat.setPosition(35, 60)
||sprites:on overlap||
event that results in a new behavior that uses ||math:pick random||
(for example, set velocity, set location, change location by, and so on), and causes the sprite to ||sprites:say||
something||sprites:overlap||
works with different sprites of the same ||sprites:kind||
||controller:A||
button and give two of the sprites a random velocity (use a range across negative and positive for ||sprites:vx||
and ||sprites:vy||
)||math:pick random||
that you would like to design into a future game - especially something we don’t know how to do yet. Be descriptive of the game and how a random value would be needed.