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.
Simulation OnlineFind more details on PCBX Community
DescriptionCode 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.
Join the PCBX community to create your first project and get a $5 coupon.
https://www.pcbx.com/community?mtm_campaign=E&mtm_kwd=hack
Register now to get your first Free PCB&PCBA coupon
https://www.pcbx.com/?mtm_campaign=E&mtm_kwd=BD
It's an open-source community; any sharing and feedback is welcome.
Comments
Please log in or sign up to comment.