Creating Tetris Using Visual C++ Windows Form. Part one.

Tetris is an old and simple game of the puzzle matching kind. This game is a good example of creating a game application for every beginner developer. This tutorial is divided into two parts: one part is about game logic and the second one is about creating a Windows Forms Application that will interact with the user(player). When you develop any kind of the application, you should always keep in mind that you need to divide application's data and app's interface. The traditional Tetris game has 7 different types of the shapes: tetris Let's call thess types in the program in this way: line, square, rightL, leftL, pyramide, leftZ, rightZ. To use these notation, we can create an enum class ShapeType:
  1. #pragma once
  2. enum class ShapeType
  3. {
  4.         line, square, rightL, leftL, pyramide, leftZ, rightZ
  5. };
Now we can start to implement class Shape that will produce different types of the shapes. The shape class will have two data members:
  1. array<Boolean,2>^       cells; // 2D Array for shape
  2. ShapeType       curType; // type of shape
The constructor of the class will simply initialize the 2D array:
  1. Shape()
  2. {
  3.      cells = gcnew array<Boolean,2> (4,4);
  4. }
As you can see, the Shape in this class is represented by boolean values.So,different types of shapes are different boolean arrays. For different shape we will need the different values for 2D array. This action is done in void    NewShape(ShapeType type) function. Let's take a look on the different types of shapes produced by it. Initially we need to clear the shape array:
  1. ClearShape(cells);
  2. Int16 i;
  3. curType=type;
It's done by a simple function:
  1. void    ClearShape(array<Boolean,2>^ c)
  2.         {
  3.                 for(Int16 i = 0;i != 4;++i)
  4.                     for(Int16 j = 0; j != 4; ++j)
  5.                         c[i,j] = false;
  6.         }
Now,let's see how different types of shapes can be produced:
  1. switch(type)
  2.         {
  3.                 case ShapeType::line:          
  4.                 for(i = 0; i != 4;++i) 
  5.                           cells[0,i] = true;
  6.                 break;
  7.  
  8.                 case ShapeType::square:        
  9.                 for(i=0; i!=2; ++i)
  10.                     for(Int16 j=0; j != 2;++j)
  11.                         cells[i,j]=true;       
  12.                 break;
  13.  
  14.                 case ShapeType::leftL:         
  15.                 for(i = 0; i != 3;++i)
  16.                     cells[0,i] = true;
  17.                 cells[1,2]=true;
  18.                 break;
  19.  
  20.                 case ShapeType::rightL:        
  21.                 for(i = 0; i != 3; ++i)
  22.                     cells[0,i]=true;
  23.                 cells[1,0]=true;
  24.                 break;
  25.  
  26.                 case ShapeType::pyramide:      
  27.                 for(i = 0; i != 3; ++i)
  28.                         cells[1,i]=true;
  29.                 cells[0,1]=true;
  30.                 break;
  31.  
  32.                 case ShapeType::leftZ:         
  33.                 cells[0,0]=true;
  34.                 cells[1,0]=true;
  35.                 cells[1,1]=true;
  36.                 cells[2,1]=true;
  37.                 break;
  38.  
  39.                 case ShapeType::rightZ:        
  40.                 cells[0,1]=true;
  41.                 cells[1,0]=true;
  42.                 cells[1,1]=true;
  43.                 cells[2,0]=true;
  44.                 break;
  45.         }
This code could be understood in a very good way by drawing a 3x3 grid and placing dots in the cells with true values. The most difficult operation in the Tetris game for programmer is the rotation of the shape. Some of the shapes is really easy to rotate, but some can put you in a trouble. The rotation of the line can be done in two ways, according to it's current position:
  1. switch(curType)
  2. {
  3. case ShapeType::line:
  4. {
  5. Int16 k;
  6. if(cells[0,0]==true)
  7. {
  8.         ClearShape(cells);
  9.         for(k = 0; k != 4; ++k)
  10.                 cells[k,1] = true;
  11. }
  12. else
  13. {
  14.         ClearShape(cells);
  15.         for(k = 0; k !=4 ; ++k)
  16.                 cells[0,k]=true;
  17. }
  18. return;
  19. }
The square type should not be rotated:
  1. case ShapeType::square: return;
  2. }
All the other shapes can be rotated using an universal rotation function:
  1. array<Boolean,2>^ tempShape = gcnew array<Boolean,2>(4,4);
  2. ClearShape(tempShape);
  3. for(Int16 j=3-1 , c=0; j>=0 ; j-- , c++)
  4.         for(Int16 i=0; i<3; i++)
  5.                 tempShape[c,i]=cells[i,j];
  6.  
  7. ClearShape(cells);
  8. for(Int16 f=0; f<3; f++)
  9.         for(Int16 d=0; d<3; d++)
  10.                 cells[f,d]=tempShape[f,d];
  11. }
These piece of code just changes the position of the boolean values. And it can be used for any types of cells. Now, the Shape logic of the tetris game is implemented. In the next article you will find the information about how to create a Windows Form Application and link it's interface to the create Shape class.

Add new comment