Accelerometer-controlled Tetris Clone
November 27, 2009
For my EGR280 project in the Winter 2009 semester, I made a clone of the classic game TETRIS using an HCS12 microcontroller.
This project replicates the functionality and logic of the original game (all coded by observervation), but replaces a traditional keypad input with a three-axis MEMS accelerometer. Turning the accelerometer left, right and down moves the block accordingly. A push button on the controller rotates the block 90ΒΊ clockwise.
A Dragon12+ microcontroller controls the game logic, music, score, sampling and interpretation of the accelerometers with code written in C. It has several arrays that store the state of the 20x10 Tetris board, communicating this state via RS-232 with a Nexys2 board programmed with Verilog, which displays the board on a standard VGA display.
The code itself is entirely interrupt-driven. Every 10.24 milliseconds, the microcontroller begins executing a sequence of functions that make up the core functionality of the game. This is the "heartbeat" of the game, in a sense.
Its main tasks are to (1) decrement counters that control if the block needs to move in a particular direction, (2) sample accelerometer data and use counters to determine the player's intent, (3) check to see if the block will hit something, and whether this will trigger: a block becoming "stuck", a line being completed, or the game being lost, (4) check to see what changes need to be written to the screen buffer in the Nexys2, and (5) change the frequency being output to the speaker for the music.
Below are all the possible Tetris pieces or "Tetrominos":
The game arrays were set up in the following manner:
The "block" array stores the shape that is currently in-play. The "stuck" array keeps track of blocks that have been stuck to the bottom in the course of gameplay. The "draw" array contains the next frame of video, while "video" contains what is currently on the screen.
The block diagram below illustrates the rotation algorithm:
The accelerometer interpretation code was programmed with defined thresholds based on the user's intent:
Here is a video of the Tetris project in action: