Search This Blog

Showing posts with label tetris. Show all posts
Showing posts with label tetris. Show all posts

Making of Tetris Game in C

tetris block block game c program
Block types in tetris
Have you ever tried to make tetris game in c. Tetris is a simple game with minimal graphics. It is much easy to program in c. If you are not familiar with graphics.h header file, you may think it almost impossible. But, graphics.h is just a simple library justs as string.h. It is very simple to learn. Let us first see how to represent tetris as data structure. There are mainly 5 types of blocks in tetris. They are L blocks, Z blocks, I blocks, S blocks and T blocks. The blocks are shown in the picture.


To make the tetris game, we first make a two dimensional array. An element in this integer array is a single block (one of the four blocks in each block type) in the game. We set all the elements to zero initially to show that the matrix is empty. We may call each element a cell. So to represent a Z block, we need four cells. In the matrix, to represent a falling block (eg: a Z block) we set corresponding elements' values to 1. We move these 1's in the 2d integer array downwards to simulate falling. Then when it reaches bottom, these 1's are changed to 2's. This is to denote that these cell cannot be moved further. Again, we start another random block from the top (first row).