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 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 used 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: Teris 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: Teris 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.

Sunday, February 2, 2025

Part 2: Tic Tac Toe game in C programing | Source Code explanation

Previous Page

Watch this tutorial in YouTube.


In this article, we will discuss the following main topics. I encourage you to watch YouTube video to understand the source code in detail.

  • Technique to switch players.
  • Logic to check winner player.
  • Calculate computer player best move.

Technique to switch players

I have assigned the value 0 to the first player and the value 1 to the second player, regardless of who plays first. If the human chooses to play first, the value 0 is assigned to the human variable, and the value 1 is assigned to the computer variable, and vice versa. Throughout the project, the system checks the current player against the human or computer variable to determine the player type. The first player always uses "X," while the second player uses "O." However, the value assigned to a box depends on the player type: the value of a box chosen by the human is 3, and the value of a box chosen by the computer is 5.

Part 1: Tic Tac Toe game in C programing | Flowchart, Algorithm of Computer Player, and Data Structure

Previous Page                                                                                                              Next Page

Watch this tutorial in YouTube.

Flowchart



Explanation of Flowchart:

Start: The game process begins here.

Display Game Menu: The user can choose to play first or second.

Initialize Game: Initialize the human or computer as the first or second player. Set up the Tic-Tac-Toe board and assign initial values to the players.

Wednesday, December 4, 2024

Part 2: Snake Game in C | Snake Collision Detection, Calculate next position and Changing direction

Previous page 

In this section, we will discuss the following items.

  • Techniques to change direction of the snake based on user input.
  • Techniques to calculate the snake's next position.
  • Techniques to check for snake collision with window and its body.

Snake direction and food

We are using the gotoxy(int x, int y) function to move the snake on the screen. The top left corner of the PC window starts at (0,0). The right side of the screen is positive x-axis, and the bottom of the screen is positive y-axis. However, we can represent the direction of snake in all four directions by positive and negative axes (-x, 0), (x, 0), (0, -y), and (0, y). For the left arrow key, set direction as negative x-axis (-x,0), for the right arrow key set direction as positive x-axis (x,0), for the up arrow key set direction as negative y-axis (-y,0), and for the down arrow key set direction as positive y-axis (y,0). When the direction is changed, the snake will move perpendicular to current position, so we don't have to use both x, and y coordinates values. The coordinates system of the PC window is shown in the image below.

Part 1: Snake Game in C | Flowchart, Data Structure and Algorithm

Previous page                                                                                                                 Next Page

Flowchart

Flowchart: Snake Game in C


Explanation of Flowchart:

Start: The process begins here.

Initialize game: Initialize Snake Direction, food, snake, window, variables, and others