Building a Tetris Game With Arduino and OLED
by Skylar Jones in Teachers > 1
31 Views, 0 Favorites, 0 Comments
Building a Tetris Game With Arduino and OLED
This project implements a classic Tetris game on an SSD1306 OLED display. An Arduino MAGE328P microcontroller controls the game logic and interacts with the display and three buttons. The buttons allow players to control the movement (left, right, down) and rotation of the falling Tetris blocks.
Supplies
Online Simulation
OLED Display (SSD1306): Used to display the Tetris game. Buttons: Three buttons are used to control the movement direction and rotation of the blocks. Arduino Development Board (MAGE328P): Serves as the control unit. Circuit Connections: SSD1306 OLED Display: VCC is connected to Arduino's 5V. GND is connected to Arduino's GND. SCL is connected to Arduino's A5 (SCL). SDA is connected to Arduino's A4 (SDA). Buttons: One pin of each button is connected to an Arduino digital input pin. The other pin is connected to GND.
To eliminate bouncing, a 10kΩ pull-up resistor can be added between the button and GND.
More project details here: https://www.pcbx.com/community-detail/fae5df4491ac4dafb5af59ee5defcd3f
Code Analysis and Explanation
- Include Library Files: At the beginning of the program, three library files are included for graphic display, control of the OLED display, and I2C communication.
- Define Constants: Constants are defined for the size of the OLED display, the width and height of the grid, the pixel size of the blocks, and other constants.
- Define Tetris Shapes: Seven different Tetris shapes are defined using a two-dimensional array.
- Game Grid and Variable Initialization: A boolean two-dimensional array grid is defined to represent the game grid, as well as the current block's position, shape, and rotation state.
- setup() Function: Initializes serial communication, I2C communication, and the OLED display, and sets the button modes to input pull-up.
- loop() Function: The main loop of the program, responsible for checking if the game is over, handling user input, moving the block, and drawing the game frame.
- drawFrame() Function: Clears the display and draws the game grid and the current block.
- drawShape() Function: Draws the block based on its position, shape, and rotation state.
- drawGrid() Function: Draws the game grid.
- moveShapeDown() Function: Moves the block down, and if a collision occurs, it fixes the block and checks if a line is completed.
- checkCollision() Function: Checks for collisions after the block is moved.
- fixShape() Function: Fixes the block onto the game grid.
- newShape() Function: Generates a new block.
- handleInput() Function: Handles user button input, including left move, right move, and rotation.
- checkLines() Function: Checks and clears completed lines.
- isGameOver() Function: Checks if the game is over.
- rotateShape() Function: Rotates the block.
Conclusion
While the 3D simulation feature is still a work in progress, we would love to hear your suggestions and expectations. It's an open-source community; any sharing and feedback is welcome.
Your feedback will help our engineering team enhance the platform and better serve our users.
Join the PCBX Simulation Community and create your own project to win a surprise bag now.