o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
In Micro:bit, write a game in TS where you are a LED at the very bottom, and other LEDs…
- User
In Micro:bit, write a game in TS where you are a LED at the very bottom, and other LEDs from the top are dropping, and you should catch all the LEDs, if you fail at catching a LED, you lose
- Assistant
Certainly! Below is the TypeScript code for a Micro:bit game where you control an LED at the bottom of the screen to catch falling LEDs from the top. If you miss a falling LED, the game ends.
let playerX = 2; // Player starts in the middle at the bottom row (row 4) let fallingLEDs: { x: number, y: number }[] = []; // Array to hold falling LEDs let gameRunning = true; // Game state let score = 0; // Score counter // Move player left when button A is pressed input.onButtonPressed(Button.A, function () { if (playerX > 0) { playerX--; } }) // Move player right when button B is pressed input.onButtonPressed(Button.B, function () { if (playerX < 4) { playerX++; } }) // Main game loop basic.forever(function () { if (!gameRunning) return; // Randomly add a new falling LED at the top row if (Math.randomRange(0, 1) == 0) { let newLEDX = Math.randomRange(0, 4); fallingLEDs.push({ x: newLEDX, y: 0 }); } // Clear the display for the new frame basic.clearScreen(); // Draw the player at the bottom row led.plot(playerX, 4); // Move and draw the falling LEDs for (let i = fallingLEDs.length - 1; i >= 0; i--) { let fallingLED = fallingLEDs[i]; fallingLED.y += 1; // Move the LED down by one row if (fallingLED.y == 4) { // The LED has reached the player's row if (fallingLED.x == playerX) { // Player catches the LED fallingLEDs.splice(i, 1); // Remove the LED from the array score++; // Increment score } else { // Player misses the LED, game over gameRunning = false; basic.showString("Game Over"); basic.showString("Score: " + score); return; // Exit the loop } } else if (fallingLED.y > 4) { // LED has moved past the screen without being caught, remove it fallingLEDs.splice(i, 1); } else { // Draw the falling LED at its new position led.plot(fallingLED.x, fallingLED.y); } } // Control the speed of the game (pause between frames) basic.pause(500); });Explanation:
-
Player Movement:
- The player is represented by an LED at the bottom row (
y = 4) on the Micro:bit's 5x5 LED grid. - Button A moves the player left, decreasing the
playerXposition if it's greater than 0. - Button B moves the player right, increasing the
playerXposition if it's less than 4.
- The player is represented by an LED at the bottom row (
-
Falling LEDs:
- Falling LEDs are represented by objects with
xandyproperties, stored in thefallingLEDsarray. - In each iteration of the main game loop, a new falling LED is added at the top row (
y = 0) with a randomxposition, with a 50% chance. - Each falling LED moves down one row in every game loop iteration (
fallingLED.y += 1).
- Falling LEDs are represented by objects with
-
Collision Detection and Game Over:
- When a falling LED reaches the bottom row (
y = 4), the game checks if itsxposition matches the player'sxposition (playerX).- If it matches, the player has caught the LED (
scoreis incremented, and the LED is removed from the array). - If it doesn't match, the player has missed the LED, and the game ends (
gameRunningis set tofalse, and "Game Over" along with the score is displayed).
- If it matches, the player has caught the LED (
- When a falling LED reaches the bottom row (
-
Drawing the Game State:
- Before drawing, the screen is cleared with
basic.clearScreen(). - The player's LED and all falling LEDs are plotted on the LED grid using
led.plot(x, y).
- Before drawing, the screen is cleared with
-
Game Loop Timing:
basic.pause(500)controls the speed of the game, making the loop run every 500 milliseconds.
How to Run the Game:
-
Setup:
- Open the Microsoft MakeCode editor for Micro:bit.
- Create a new project and switch to the JavaScript editor.
-
Paste the Code:
- Copy the above TypeScript code and paste it into the editor.
-
Play the Game:
- Simulate the game using the emulator in the editor or download it to a physical Micro:bit device.
- Use the A button to move left and the B button to move right.
- Catch the falling LEDs by moving to their
xposition before they reach the bottom row.
-
Game Over:
- When you miss catching a falling LED, the game will display "Game Over" and show your score.
Notes:
- The game can be modified to adjust difficulty by changing the speed (
basic.pause(500)) or the probability of new LEDs appearing. - To restart the game after "Game Over," you can reset the Micro:bit or modify the code to reset variables and rerun the game loop.
Hope you enjoy playing the game on your Micro:bit!
-