Here's a complete TypeScript code for a micro:bit game that mimics the Chrome Dino game. In this game, the player is at the bottom left corner of the LED display. Pressing button A makes the player jump to avoid oncoming obstacles. Obstacles are represented by LEDs moving from right to left on the bottom row. If the player fails to jump over an obstacle, the game ends.
let playerX = 0
let playerY = 4
let obstacles: number[] = []
let obstacleInterval = Math.randomRange(10, 20)
let isJumping = false
let jumpCounter = 0
// Function to handle the jump when button A is pressed
input.onButtonPressed(Button.A, () => {
if (!isJumping && playerY == 4) {
isJumping = true
jumpCounter = 0
}
})
// Main game loop
basic.forever(() => {
// Move obstacles left
for (let i = 0; i < obstacles.length; i++) {
obstacles[i] -= 1
}
// Remove obstacles that have moved off the screen
while (obstacles.length > 0 && obstacles[0] < 0) {
obstacles.shift()
}
// Check for collision with the player
if (obstacles.length > 0 && obstacles[0] == 0) {
if (playerY == 4) {
// Collision detected, game over
basic.showIcon(IconNames.Sad)
basic.pause(1000)
control.reset()
}
}
// Generate new obstacles at random intervals
obstacleInterval -= 1
if (obstacleInterval <= 0) {
obstacles.push(4)
obstacleInterval = Math.randomRange(10, 20)
}
// Handle the jumping motion of the player
if (isJumping) {
jumpCounter += 1
if (jumpCounter == 1) {
playerY = 3
} else if (jumpCounter == 2) {
playerY = 2
} else if (jumpCounter == 3) {
playerY = 3
} else if (jumpCounter == 4) {
playerY = 4
isJumping = false
}
}
// Update the LED display
basic.clearScreen()
led.plot(playerX, playerY)
for (let x of obstacles) {
led.plot(x, 4)
}
// Pause before the next game loop iteration
basic.pause(200)
})
Explanation:
-
Player Movement:
- The player is represented by an LED at position
(0, playerY), starting at the bottom left corner (0, 4).
- Pressing button A initiates a jump sequence where the player moves up and then back down over a few game loops.
-
Obstacles:
- Obstacles are represented as LEDs moving from right to left on the bottom row
(x, 4).
- They are stored in an array
obstacles, which holds their x-positions.
- Obstacles are generated at random intervals to increase the game's unpredictability.
-
Jump Logic:
- The
isJumping flag indicates if the player is currently jumping.
- The
jumpCounter variable controls the jump's progression over several game loops.
- The player moves up to row
2 (mid-jump) and then back down to row 4 (ground level).
-
Collision Detection:
- A collision occurs if an obstacle reaches the player's x-position
(0) while the player is on the ground (playerY == 4).
- When a collision is detected, a sad icon is displayed, and the game resets after a short pause.
-
Game Loop:
- The
basic.forever loop runs continuously, updating the game state every 200 milliseconds.
- It handles moving obstacles, checking for collisions, updating the player's jump status, and refreshing the display.
How to Use:
- Copy the code into the Microsoft MakeCode editor for micro:bit.
- Flash the code to your micro:bit device.
- Press button A to make the player jump over obstacles.
- Try to avoid colliding with the obstacles to keep the game going.
Enjoy your micro:bit Chrome Dino game clone!