Open your web browser and navigate to MakeCode Arcade. Click on 'New Project' to start a new game project. Name your project 'Beat The Clock'.
Let's set up some basic game elements. We will create a player sprite and a target sprite. The player will try to reach the target within a certain time limit.
Add the following code and design your player and target sprites:
let player = sprites.create(img` . . . . . . # # # . . # # # . . # # # . . . . . . `, SpriteKind.Player) let target = sprites.create(img` . . # . . . # . # . # . . . # . # . # . . . # . . `, SpriteKind.Food)
Now let's add code to move the target and player in your game. This will make your game more interactive and challenging. You will add code to allow the player to move their sprite using the arrow keys and to move the target sprite to a random location on the screen every second.
Add the following code:
let player = sprites.create(img`...`, SpriteKind.Player) controller.moveSprite(player) let target = sprites.create(img`...`, SpriteKind.Food)
let target: Sprite = null
game.onUpdateInterval(5000, function() {
target.setPosition(Math.randomRange(0, 160), Math.randomRange(0, 120))
})'Math.randomRange' generates a random number between the two values provided, in this case between the dimensions of the screen (0 to 160 for x, and 0 to 120 for y).
Now, let's set up the game timer. We will use the inbuilt countdown block which will automatically start the countdown from a specified time.
Add the start countdown block:
let target: Sprite = null
let player2 = sprites.create(img`
. . . . .
. 1 1 1 .
. 1 1 1 .
. 1 1 1 .
. . . . .
`, SpriteKind.Player)
controller.moveSprite(player2)
target = sprites.create(img`
. . 1 . .
. 1 . 1 .
1 . . . 1
. 1 . 1 .
. . 1 . .
`, SpriteKind.Food)
info.startCountdown(60)
This code starts the countdown timer at 60 seconds. When the countdown reaches 0, the game will automatically end.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.