Tetris is a tile-matching puzzle game where players manipulate falling geometric shapes, known as Tetrominoes, to create complete horizontal lines. The game was originally designed by Alexey Pajitnov in 1984 and has since become one of the most iconic video games of all time.
1. The Playing Field
The game takes place on a rectangular grid of 10 columns and 20 rows (standard size).
Tetrominoes fall from the top of the grid and move downward.
The player must arrange and rotate the pieces to fit them together.
The goal is to create full horizontal lines without gaps.
There are seven different Tetrominoes, each consisting of four squares arranged in unique shapes (L, T, I, O, S, Z, and J).
Rotation: Pieces can rotate 90° clockwise or counterclockwise to fit different spaces.
Movement: The player can move pieces left, right, or make them fall faster.
3. Line Clearing
A line is cleared when all columns in a row are filled.
Cleared lines disappear, and the blocks above shift downward.
Players earn more points for clearing multiple lines at once:
1 line: 100 points
2 lines: 300 points
3 lines: 500 points
4 lines (Tetris!): 800 points
4. Increasing Difficulty
As the player clears more lines, the game speed increases, making it harder to place pieces correctly.
At higher levels, Tetrominoes fall faster, reducing reaction time.
5. Game Over
The game ends when the stack of Tetrominoes reaches the top of the playing field.
This project implements a Tetris game using an Arduino UNO, an LCD I2C screen, a joystick for control, and a buzzer for sound effects.
1. Initial Setup
The Arduino initializes the LCD I2C screen and buzzer.
The joystick module is set up as an input.
A random Tetromino appears at the top of the grid.
2. Tetromino Movement
The player moves the Tetromino left/right with the joystick.
Pressing the joystick rotates the Tetromino if possible.
The Tetromino automatically moves downward at regular intervals.
If the player tilts downward, the Tetromino falls faster (soft drop).
3. Collision Detection
The Arduino checks if the Tetromino hits the floor or collides with other blocks.
If a collision is detected:
a) The piece stops moving.
b) The next Tetromino appears.
c) If a full line is completed, it is cleared.
4. Line Clearing
If a row is fully filled, it disappears, and all blocks above it move down.
The LCD refreshes to reflect the new grid state.
The buzzer plays a short sound when a line is cleared.
5. Increasing Difficulty
The game gradually speeds up after a certain number of lines are cleared.
6. Game Over Condition
If a new Tetromino cannot be placed because the grid is full, the game ends.
The LCD displays "Game Over", and the buzzer plays a long tone.
Arduino UNO
Acts as the brain of the Tetris game, processing inputs and updating the display.
Reads signals from the joystick for movement and rotation of Tetrominoes.
Controls the LCD I2C screen to display the game.
Sends sound signals to the buzzer for game events.
LCD 16x2 I2C Screen
Displays the game grid (simplified version due to small size).
Shows the current score and level.
Provides visual feedback when lines are cleared or the game is over.
Joystick Module
Controls left/right movement of Tetrominoes.
Rotates the piece when the joystick button is pressed.
Moves the piece downward faster when pushed forward.
Buzzer
Generates sound effects for game actions
Breadboard
Provides an easy way to connect components without soldering.
Allows multiple components (joystick, buzzer, LCD) to share the same power and ground connections.
Jumpers
Connects all components to the Arduino UNO and breadboard.
Connect VCC to 5V on Arduino.
Connect GND to GND on Arduino.
Connect X-axis to A0 and Y-axis to A1 on Arduino.
Connect the joystick button to a digital pin (e.g., D2).
Connect SDA to A4, SCL to A5, VCC to 5V, and GND to GND.
Connect the positive pin to a digital output pin (e.g., D3) on Arduino.
Connect the negative pin to GND.
Here is the Arduino code for a simplified Tetris game using an Arduino UNO, a 16x2 LCD I2C screen, a joystick, and a buzzer. Since the LCD 16x2 is small, we create a minimalist Tetris using custom characters to represent Tetrominoes.
import this library : LiquidCrystal_I2C for I2C LCD screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
#include <Wire.h> #include <LiquidCrystal_I2C.h> #define JOYSTICK_X A2 // Joystick X-axis #define JOYSTICK_Y A3 // Joystick Y-axis #define JOYSTICK_BTN 2 // Joystick button #define BUZZER 3 // Buzzer pin LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD 16x2 with I2C address 0x27 // Tetris grid (4x8 simplified for LCD) char grid[2][16] = { " ", " " }; // Tetrominoes representation (custom characters) byte blockChar[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }; // Tetromino coordinates int tetrominoX = 7, tetrominoY = 0; // Game Variables int score = 0; bool gameOver = false; int speed = 500; // Falling speed in milliseconds void setup() { lcd.init(); lcd.backlight(); pinMode(JOYSTICK_BTN, INPUT_PULLUP); pinMode(BUZZER, OUTPUT); lcd.createChar(0, blockChar); // Create block character Serial.begin(9600); // Debugging lcd.setCursor(4, 0); lcd.print("TETRIS GAME"); delay(2000); lcd.clear(); } void loop() { if (gameOver) { lcd.clear(); lcd.setCursor(4, 0); lcd.print("GAME OVER!"); tone(BUZZER, 100, 1000); delay(2000); while (1); // Stop execution } moveTetromino(); drawGrid(); delay(speed); dropTetromino(); } // Function to move the tetromino based on joystick input void moveTetromino() { int xVal = analogRead(JOYSTICK_X); int yVal = analogRead(JOYSTICK_Y); int btnState = digitalRead(JOYSTICK_BTN); if (xVal < 400 && tetrominoX > 0) { tetrominoX--; tone(BUZZER, 200, 100); } if (xVal > 600 && tetrominoX < 15) { tetrominoX++; tone(BUZZER, 400, 100); } if (yVal > 600) { // Move down faster dropTetromino(); tone(BUZZER, 600, 100); } if (btnState == LOW) { // Rotate (Simplified) tone(BUZZER, 800, 200); delay(300); } } // Function to drop the tetromino void dropTetromino() { if (tetrominoY < 1) { tetrominoY++; } else { placeTetromino(); checkLineClear(); tetrominoX = 7; tetrominoY = 0; if (grid[0][tetrominoX] != ' ') { gameOver = true; } } } // Function to place the tetromino on the grid void placeTetromino() { grid[tetrominoY][tetrominoX] = 0; // Custom character block } // Function to check if a line is full and clear it void checkLineClear() { for (int row = 1; row >= 0; row--) { bool full = true; for (int col = 0; col < 16; col++) { if (grid[row][col] == ' ') { full = false; break; } } if (full) { for (int col = 0; col < 16; col++) { grid[row][col] = ' '; } for (int r = row; r > 0; r--) { for (int col = 0; col < 16; col++) { grid[r][col] = grid[r - 1][col]; } } score += 10; tone(BUZZER, 1000, 300); delay(500); } } } // Function to draw the grid on LCD void drawGrid() { lcd.clear(); for (int row = 0; row < 2; row++) { lcd.setCursor(0, row); lcd.print(grid[row]); } lcd.setCursor(12, 0); lcd.print("S:"); lcd.print(score); } |
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.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info