Project thumbnail

Tetris

18 June 2018
A classic game of Tetris built from scratch

This is a classic game of Tetris. The game was build from scratch and represents the most basic variation of Tetris. Tetris consists of seven shapes, each formed out of four blocks.

Z shape
"Z" shape.
T shape
"T" shape.
S shape
"S" shape.
O shape
"O" shape.
L shape
"L" shape.
J shape
"J" shape.
I shape
"I" shape.

The player can move the shapes left and right using the left and right arrows on keyboard, they can speed up falling by pressing or holding the down arrow button, and they can also rotate the shape by pressing the up arrow button. When the player fills one line, the line gets destroyed and all the blocks above the line move one row down.

Tetris – before clearing
The game before clearing a line.
Tetris – after clearing
The game after clearing the line.

How it works: each shape is represented with a set of 2D schemas (arrays), one for each rotation of the shape. The number of schemas per shape vary; the “O” shape, for example, only has one schema, while the “L” shape has four. There is only one shape moving at a time and each position is determined according to the coordinates of the top left corner of its schema.

I used multi-threading to enable the player to pause and resume the game.

Tetris – smaller version
The grid can be any number of rows and columns.

After the core functionality was implemented, a friend of mine also added a nice welcome screen and some music to the game.

This project can be found here.