Sunday, March 30, 2025

Develop Tetris Game in C programming Without Graphics Library and Full Source Code

Tetrix game is created in C programming without using graphic library. We draw shapes only using # character and game is played on command window. User can play game using up, left, right, and down arrow key. The up key rotate Tetrimino (Shapes) in clockwise direction, left key move Tetrimino (Shapes) in left direction toward negative x-axis, right key move Tetrimino (Shapes) in right direction toward positive x-axis, and down key rotate Tetrimino (Shapes) in anticlockwise direction. By default, Tetrimino (Shapes) will move 1 unit downward toward negative y-axis in each frame of the game.  The score is calculated based on the number of rows filled with boxes. 

The main objective of this project is to provide knowledge and technique about creating Tetris game in C programming, provide basic knowledge of linear algebra to rotate and translate shapes. User can use any graphics library to draw window, and shapes to make it similar to commercial games. However, the algorithm of the game is same in all Tetris game available in market.  

In this article, I will explain the following items:

  • Coordinate system of the game
  • Usage of linear algebra to rotate and translate shapes.
  • Data Structure used in the game
  • Collision detection and score checking Techniques


Also, Download the final version of the game Download Project from GitHub.

I have explained the game in following two articles:

Part 1: Tetriminos (Shapes), Coordinate System, Rotation and Translate



Part 2: Data Structure, Collision detection and Score checking Techniques



Part 2: Tetris Game in C programming | Data Structure, Collision detection and Score checking Techniques

Previous Page 



Data Structure

This is a 2D game where the primary object is a box. A combination of 4 boxes forms a Tetrimino (shape). Boxes are arranged on the Tetris board from the bottom upward.

Board Specifications
  • The Tetris board has a size of 11 columns (x-axis) by 21 rows (y-axis).
  • Tetriminos are arranged on the board such that if an entire row is filled with boxes, the player earns points.
Box Representation
  • Each box is defined by 2D (x, y) coordinates, which determine where it is drawn.
  • To represent boxes and Tetriminos (shapes), the game uses three structure variables:
    • Point2D
    • Tetriminos
    • TetrisBox

Part 1: Tetris Game in C programming | Tetriminos (Shapes), Coordinate System, Rotation and Translation of Tetriminos (Shapes)

Previous Page                                                                                                                  Next Page



Tetriminos (Shapes)

In this Tetris game, I use 7 shapes. While some versions only include 5 shapes, I’ve added two extra ones: the J and S shapes, which are mirror opposites of the L and Z shapes, respectively.

Each shape consists of 4 blocks, and each block is called a Mino (short for monomino). A group of 4 Minos forms a Tetrimino (also known as Tetromino or Tetramino). For consistency, I’ll use the term Tetrimino throughout this article.


Coordinate System

To simplify game development, I’ve designed a gotoxy(x, y) function that relocates the coordinate system’s origin (0, 0) from the default top-left corner of the command window to a more convenient central position at (36, 12).

How It Works:
  1. Coordinate Transformation:
    • The function offsets input coordinates:
      • x becomes x + 36
      • y becomes y - 12
    • This allows negative coordinates (e.g., (-36, 12) maps to the original (0, 0)).
  1. Purpose:
    • Centering (0, 0) simplifies calculations for symmetric gameplay elements (e.g., rotating Tetriminos around the origin).
    • Negative coordinates enable intuitive positioning relative to the new origin.