Micro:bit TETRIS Game

Micro:bit 27-02-25
153 0

Tutorial plan

1- Presentation of Tetris game

2- Objective of our Tetris game

3- Required Components

4- Component Wiring

5- Tetris game algorithm

 

 

Presentation of Tetris game

Tetris is a classic puzzle game created by Alexey Pajitnov in 1984. The game involves stacking geometric shapes called tetrominoes within a vertical grid. The objective is to complete full horizontal lines, which then disappear, making room for new falling blocks.

Game Principles:

1- Tetrominoes of different shapes (L, T, I, O, S, Z, and J) fall from the top of the grid.

2- The player can move and rotate the tetrominoes to fit them strategically.

3- When a row is completely filled, it disappears, and the player earns points.

4- The game becomes progressively faster, making it more challenging.

5- The game ends when the blocks reach the top of the grid.

Tetris is known for its simplicity, strategy, and endless replayability, making it one of the most popular and enduring video games of all time.

Objective of our Tetris game

This version of the Tetris game is designed to run on a BBC Micro:bit, using Button A and Button B for control and an LCD I2C screen to display the game.

1- The game board (Tetris grid) is displayed on the LCD screen.

2- Falling blocks appear one by one and move downward.

3- The player can control the block position using:

Button A → Move left

Button B → Move right

Button A and Button B → Rotate the piece

4- When a block reaches the bottom or lands on another block, a new block appears.

5- If a row is completely filled, it disappears, and the score increases.

6- The game ends if the blocks reach the top of the screen.

 

Required Components

BBC Micro:bit Board

Micro:bit board

Purpose: The Micro:bit serves as the main microcontroller to handle game logic, user input, and display output.

LCD I2C Screen (16x2 or 20x4)

Pinouts of 1602 LCD display with I2C

Purpose: Displays the Tetris game board (grid) and game information (score, level, next piece).

GPIO Extension Card

The GPIO expansion card for the Micro:bit card

Purpose: Expands the Micro:bit’s limited GPIO pin accessibility, making it easier to connect multiple components.

Breadboard

Breadboard

Purpose: Used to assemble and connect components without soldering.

Jumper Wires

Jumper wires

Jumper wires will be used to make connections between the components.

 

Component Wiring

LCD Screen (I2C-based) to Micro:bit :

SDA → Pin 19 (I2C Data).

SCL → Pin 20 (I2C Clock).

VCC → 3.3V or 5V.

GND → Ground.

 

Tetris game algorithm

This algorithm details the implementation of a Tetris game on a Micro:bit using:

Button A → Moves the tetromino left.

Button B → Moves the tetromino right.

Button A+B → Rotates the tetromino.

LCD I2C Screen → Displays the game grid.

1. Game Initialization

Set up the LCD I2C screen (e.g., a 16x2 or 20x4 display).

Define the game grid (e.g., 8x16 virtual matrix stored in an array).

Create an array of tetromino shapes (L, T, I, O, S, Z, J).

Initialize game variables:

current_tetromino → Stores the active tetromino.

position_x, position_y → Tracks tetromino position.

rotation_state → Tracks the current rotation (0, 90, 180, 270 degrees).

score = 0 → Initial score.

game_speed → Determines the falling speed.

2. Main Game Loop

Repeat the following steps until the game is over:

a. Move the Tetromino Down (Gravity Effect)

Check if the tetromino can move down (position_y + 1):

Yes → Move it down.

No → Fix the tetromino in place and spawn a new one.

b. Handle Button Inputs (Player Controls)

Button A Pressed → Move the tetromino left (position_x - 1 if no collision).

Button B Pressed → Move the tetromino right (position_x + 1 if no collision).

Button A+B Pressed Together → Rotate the tetromino:

Update rotation_state (0 → 90 → 180 → 270 → 0).

Check if the new rotation is valid (no collision).

c. Check for Line Completion

If a row is completely filled with blocks:

Remove the row.

Shift all rows above it downward.

Increase score and possibly game_speed.

d. Update the LCD I2C Display

Convert the grid state into characters (# for blocks, . for empty spaces).

Refresh the LCD screen with the new grid state.

e. Check for Game Over

If a new tetromino cannot spawn, display "Game Over" on the LCD screen.

3. End of Game

Display the final score on the LCD.

Allow restarting when the user presses the reset button of Micro:bit card.

0 comment

Leave a comment

Veuillez noter s'il vous plaît*

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Purpose of this website

Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.

Contact details

Zaouiet Kontech-Jemmel-Monastir-Tunisia

+216 92 886 231

medaliprof@gmail.com

Pictures of the articles

Robotic site created by MedAli-Teacher info