Tetris

One day I started trying to clone tetris because its a game I really like to play and I would like to build one myself. After a bit of time of implementation I looked at my initial solution and it looked really overcomplicated for a game like tetris. So I started searching for different solutions. I found a guy building tetris in youtube and he had such a simple solution for putting pieces and locking them.

At this time I was already working with python for some years for my current job but didn't really had made any game with it. So I wanted to try to do one.

Base Implementation

After checking for game engines in python I found pygame and it looked to meet my needs so I choose it.

With my tools selected I started to think about the main data I would need for the game, the map and the blocks.

We have 5 types of blocks, 'T', 'L', 'O', 'I' and 'J'. The blocks will be represented with a 4x4 matrix where the occupied blocks will have positive numbers bigger than 0 and the empty spaces will have the number 0. These numbers will be used to represent colors when the map is drawn.

The map will be a 12x20 matrix initially all filled with zeroes. The blocks will exist separated from the map data until they hit the bottom of the map or another block. In that moment the block matrix non zero values will be applied to the map matrix making the block static and exist in the map.

With the main layout of the data defined now I implemented the existing actions in the game.

The system main actions:

With the system actions we have the game rules implemented, now would be fun if we could play the game. So next, the player actions were implemented.

The player possible actions: With the game core logic implemented now we have a working tetris version!

Game Polishing

After celebrating a bit and playing some time, some problems were noted:

For the player actions some sound were applied in each movement.

The game difficulty, it was adjusted to raise slower and a max level 20 was set. Also a background colour was set to indicate the difficulty progress. This colour changes every four levels from a calm blue color in level 1 to a dangerous pressuring red color after level 16.

Making multiple lines giving extra points was already considered. But now if you survive you will get more points in higher levels to reward the play style of trying to reach higher levels instead of only trying to maximize the score in the easier ones.

Implemented a simple flashing animation when a line is done so the line doesn't disappear instantly looking like flickering, which gives us a better notion of what happened.

A configuration menu was created were sound can be deactivated, three screen resolutions were allowed 600x450, 800x600 and 1024x768. We can also adjust the fps so it works better in slower machines. (Even if the fps are almost only noted in the line clean animation or at higher levels when the movement is more frequent).

Some pseudorandom code was done so if the same block appears multiple times, its probability will be reduced in each spawn.

Result

With all the implemented polishing the resulting game was:

The code could be better, some of the game systems were just done to check fast how the game could be done. Other parts were also done to test the pygame engine. But overall I'm happy with the game and play it from time to time. Hope you also enjoy it. Try it in the repository.

Repository

If you want you can checkout the code in the bitbucket repository. Hope you enjoy playing the game. :)