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:
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.
When the player reaches the target, they score a point. Let's create a scoring system to track the player's score.
Add the following code:
let target: Sprite = null sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) { info.changeScoreBy(1) target.setPosition(randint(0, 160), randint(0, 120)) })
This code increments the score by 1 every time the player sprite overlaps with the target sprite. It also randomly repositions the target sprite each time it's collected.