You are on page 1of 44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Javilop Portfolio & Blog

Home

Gamedev

UX & UI design

3d design

About me

Tetris tutorial in C++ platform independent focused in game logic for beginners
138 comments | in Tutorials | about Gamedev | December 14, 2008

We are going to learn how to create a Tetris clone from scratch using simple and clean C++. And this will take you less than an hour! This is the perfect tutorial for beginners. Just enjoy it and leave a comment if you want me to explain something better. I know my English sucks, so if you see some mistakes, please, tell me. Lets go! Updated! 03/04/2012

Download sourcecode
Here it is the complete sourcecode.

Windows platforms
The sourcecode comes with SDL includes and libs ready to compile in Visual C++ Express Edition 2008. In Release folder there is also an executable file just in case you want to try it directly.

Other platforms
Thanks to lmelior and to Javier Santana, there is a Linux version of this tutorial. The sourcecode is platform independent and comes with a makefile. However, under Linux, you need libsdl-gfx1.2-dev and libsdl1.2-dev (If you are using Ubuntu you can get them this way: sudo apt-get install libsdl1.2-dev libsdl-gfx1.2-dev)

Keys
E S C z x Q u i tt h eg a m e R o t a t ep i e c e D r o pp i e c e

L e f t ,R i g h t ,D o w n Iw i l ln o to f f e n dy o u ri n t e l l i g e n c e

Step 0: Introduction
We are going to focus on the game logic, using only rectangle primitives (SDL) for the rendering. All the game logic is isolated from the drawing, so you can expand the tutorial easily. Im planning making a second tutorial of how to improve this Tetris clone using sprites, background, effects, etc. But right now, lets focus on the game logic. This is how your prototype will look after you finish the tutorial:

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

1/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

In this tutorial you will learn: How to store the pieces and board using matrices (multidimensional arrays). How to solve the rotation problem in Tetris, in a really easy way, without using complex maths or anything difficult, just using an intelligent hack. How to check collisions between the pieces and the board. How the main loop of a Tetris game works. What you are supposed to already know: C++ A little bit of graphical programming if you want expand the tutorial with improved graphics. Dont worry about that if you just want to learn the Tetris game logic. What do you need? A compiler or programming IDE. Ive used Visual C++ Express Edition for this tutorial, that is a free C++ IDE. But you can use the one of your choice, of course. Desire to learn What is the license of the sourcecode? The sourcecode is under the Creative Commons Attribution 3.0 Unported. That means you can copy, distribute and transmit the work and to adapt it. But you must attribute the work (but not in any way that suggests that they endorse you or your use of the work). The manner of attribution is up to you. You can just mention me (Javier Lpez). A backlink would be also appreciated.

Step 1: The pieces


First, we are going to create a class for storing all the pieces. There are 7 different types of pieces: square, I, L, L-mirrored, N, N-mirrored and T. But, how can we define each piece? Just check out the figure:

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

2/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

As you can see, this piece is defined in a matrix of 55 cells. 0 means no block, 1 means normal block and 2 means pivot block. The pivot block is the rotation point: yes, the original Tetris game has a rotation point for each piece And how can we store that using C++? Easy: using a bidimensional array of 55 ints (or bytes, if you are a fanatic of optimization). The previous piece is stored like that: 1 2 3 4 5 { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,1 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 }

Now that we already now how to store each piece lets think about rotations. We can solve the rotation problem in a lot of different ways. In other tutorials, Ive seen them use complex rotation algebra in order to rotate the piece but we can solve this problem easily. If we can store each piece why dont we just store each piece rotated too? There are four possible rotations for each piece:

As you can see, the longer piece is only 4 block widht. But we are using 5 blocks matrices in order to be able to store all the rotations respeting the pivot block. In a previous version of this tutorial, I was using 4-block matrices, but then it was necessary to store translations of the pivot to the origin. This way, we are using some bytes more but the sourcecode is cleaner. In total we only use 448 bytes to store all the pieces. Thats nothing So, in order to store all this information we need a 4-dimensional array (wow!), in order to store the 4 possible rotations (matrices of 55) of each piece: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 / /P i e c e sd e f i n i t i o n c h a rm P i e c e s[ 7/ * k i n d* /] [ 4/ *r o t a t i o n* /] [ 5/ *h o r i z o n t a lb l o c k s* /] [ 5/ *v e r t i c a lb l o c k s* / { / /S q u a r e { { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } }
3/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2 8 3 8 4 8 5 8 6 8 7 8 8 8 9 9 0 9 1 9 2 9 3 9 4 9 5 9 6 9 7 9 8 9 9 1 0 0 1 0 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0 6 1 0 7 1 0 8 1 0 9 1 1 0 1 1 1 1 1 2 1 1 3

} , / /I { { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,2 ,1 ,1 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,2 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 1 ,1 ,2 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,2 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } } , / /L { { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,2 ,0 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,2 ,1 ,0 } , { 0 ,1 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,1 ,0 ,0 } , { 0 ,0 ,2 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,1 ,0 } , { 0 ,1 ,2 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } } , / /Lm i r r o r e d { { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,2 ,0 ,0 } , { 0 ,1 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,0 ,0 ,0 } , { 0 ,1 ,2 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 }
4/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 2 0 1 2 1 1 2 2 1 2 3 1 2 4 1 2 5 1 2 6 1 2 7 1 2 8 1 2 9 1 3 0 1 3 1 1 3 2 1 3 3 1 3 4 1 3 5 1 3 6 1 3 7 1 3 8 1 3 9 1 4 0 1 4 1 1 4 2 1 4 3 1 4 4 1 4 5 1 4 6 1 4 7 1 4 8 1 4 9 1 5 0 1 5 1 1 5 2 1 5 3 1 5 4 1 5 5 1 5 6 1 5 7 1 5 8 1 5 9 1 6 0 1 6 1 1 6 2 1 6 3 1 6 4 1 6 5 1 6 6 1 6 7 1 6 8 1 6 9 1 7 0 1 7 1 1 7 2 1 7 3 1 7 4 1 7 5 1 7 6 1 7 7 1 7 8 1 7 9 1 8 0 1 8 1 1 8 2 1 8 3 1 8 4 1 8 5 1 8 6 1 8 7 1 8 8 1 8 9 1 9 0 1 9 1 1 9 2 1 9 3

} , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,0 ,2 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,2 ,1 ,0 } , { 0 ,0 ,0 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } } } , / /N { { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,1 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,2 ,0 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,1 ,2 ,0 ,0 } , { 0 ,1 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } ,

{ { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,1 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } } , / /Nm i r r o r e d { { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,0 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,1 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,0 ,0 ,0 } , { 0 ,1 ,2 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,1 ,0 } , { 0 ,1 ,2 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } }
5/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 9 4 1 9 5 1 9 6 1 9 7 1 9 8 1 9 9 2 0 0 2 0 1 2 0 2 2 0 3 2 0 4 2 0 5 2 0 6 2 0 7 2 0 8 2 0 9 2 1 0 2 1 1 2 1 2 2 1 3 2 1 4 2 1 5 2 1 6 2 1 7 2 1 8 2 1 9 2 2 0 2 2 1 2 2 2 2 2 3 2 2 4 2 2 5 2 2 6

} , / /T { { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,2 ,1 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,1 ,2 ,1 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,1 ,2 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } , { { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,1 ,0 ,0 } , { 0 ,1 ,2 ,1 ,0 } , { 0 ,0 ,0 ,0 ,0 } , { 0 ,0 ,0 ,0 ,0 } } } } ;

Great! Now, in order to rotate a piece we just have to choose the following stored rotated piece. There is something important that we have to take in count. Each different piece must be correctly positioned every time it is created on the top of the screen. In other words, it needs to be translated to the correct position (in order to show ONLY one row of blocks in the board and to be centered, upper blocks should be OUTSIDE the board). Like each piece is different (some are lower or smaller than others in the matrices), each one needs a different translation every time it is created. We will store these translations in another array, one translation per rotated piece. Take your time to understand this.

The translation are two numbers (horizontal tranlastion, vertical translation) that we have to store for each piece. We will use these numbers later in Game class when creating the pieces each time a new piece appears, so it will be initialized in the correct position. This is the array that stores these displacements: 1 / /D i s p l a c e m e n to ft h ep i e c et ot h ep o s i t i o nw h e r ei ti sf i r s td r a w ni nt h eb o a r dw h e ni ti sc r e a t e d
6/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3

i n tm P i e c e s I n i t i a l P o s i t i o n [ 7/ * k i n d* /] [ 4/ *r 2 o t a t i o n* /] [ 2/ *p o s i t i o n* / ]= { / *S q u a r e* / { { 2 ,3 } , { 2 ,3 } , { 2 ,3 } , { 2 ,3 } } , / *I* / { { 2 ,2 } , { 2 ,3 } , { 2 ,2 } , { 2 ,3 } } , / *L* / { { 2 ,3 } , { 2 ,3 } , { 2 ,3 } , { 2 ,2 } } , / *Lm i r r o r e d* / { { 2 ,3 } , { 2 ,2 } , { 2 ,3 } , { 2 ,3 } } , / *N* / { { 2 ,3 } , { 2 ,3 } , { 2 ,3 } , { 2 ,2 } } , / *Nm i r r o r e d* / { { 2 ,3 } , { 2 ,3 } , { 2 ,3 } , { 2 ,2 } } , / *T* / { { 2 ,3 } , { 2 ,3 } , { 2 ,3 } , { 2 ,2 } } , } ;

And with that we have solved one of the most tricky parts of this tutorial. We can now create our Pieces class, this file is called Pieces.h: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 # i f n d e f_ P I E C E S _ # d e f i n e_ P I E C E S _ / // / P i e c e s / /c l a s sP i e c e s { p u b l i c : i n tG e t B l o c k T y p e ( i n tp P i e c e ,i n tp R o t a t i o n ,i n tp X ,i n tp Y ) ; i n tG e t X I n i t i a l P o s i t i o n( i n tp P i e c e ,i n tp R o t a t i o n ) ; i n tG e t Y I n i t i a l P o s i t i o n( i n tp P i e c e ,i n tp R o t a t i o n ) ;

} ;

# e n d i f/ /_ P I E C E S _

The 3 methods that you can see in the header returns some information that we will need later. Their implementation is trivial:
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 7/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0

/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = R e t u r nt h et y p eo fab l o c k( 0=n o b l o c k ,1=n o r m a lb l o c k ,2=p i v o tb l o c k ) P a r a m e t e r s : > >p P i e c e : P i e c et od r a w > >p R o t a t i o n :1o ft h e4p o s s i b l er o t a t i o n s > >p X : H o r i z o n t a lp o s i t i o ni nb l o c k s > >p Y : V e r t i c a lp o s i t i o ni nb l o c k s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / i n tP i e c e s : : G e t B l o c k T y p e( i n tp P i e c e ,i n tp R o t a t i o n ,i n tp X ,i n tp Y ) { r e t u r nm P i e c e s[ p P i e c e ] [ p R o t a t i o n ] [ p X ] [ p Y ] ; } / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = R e t u r n st h eh o r i z o n t a ld i s p l a c e m e n to ft h ep i e c et h a th a st ob ea p p l i e di no r d e rt oc r e a t ei ti nt h e c o r r e c tp o s i t i o n . P a r a m e t e r s : > >p P i e c e : P i e c et od r a w > >p R o t a t i o n :1o ft h e4p o s s i b l er o t a t i o n s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / i n tP i e c e s : : G e t X I n i t i a l P o s i t i o n( i n tp P i e c e ,i n tp R o t a t i o n ) { r e t u r nm P i e c e s I n i t i a l P o s i t i o n[ p P i e c e ] [ p R o t a t i o n ] [ 0 ] ; } / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = R e t u r n st h ev e r t i c a ld i s p l a c e m e n to ft h ep i e c et h a th a st ob ea p p l i e di no r d e rt oc r e a t ei ti nt h e c o r r e c tp o s i t i o n . P a r a m e t e r s : > >p P i e c e : P i e c et od r a w > >p R o t a t i o n :1o ft h e4p o s s i b l er o t a t i o n s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / i n tP i e c e s : : G e t Y I n i t i a l P o s i t i o n( i n tp P i e c e ,i n tp R o t a t i o n ) { r e t u r nm P i e c e s I n i t i a l P o s i t i o n[ p P i e c e ] [ p R o t a t i o n ] [ 1 ] ; }

Step 2: The board


Now we are going to learn how to store the pieces in the board and check collisions. This class stores a bidimensional array of N x N blocks that are initialized to POS_FREE. The pieces will be stored by filling these blocks when they fall down updating the block to POS_FILLED. In this class we need to implement methods in order to store a piece, check if a movement is possible, delete lines, etc. Our board is going to be very flexible, we will be able to choose the amount of horizontal and vertical blocks and the size of each block. This is the header of the class (Board.h): 1 2 3 4 5 6 7 8 9 1 0 1 1 # i f n d e f_ B O A R D _ # d e f i n e_ B O A R D _ / /-I n c l u d e s# i n c l u d e" P i e c e s . h " / /-D e f i n e s# d e f i n eB O A R D _ L I N E _ W I D T H6 # d e f i n eB L O C K _ S I Z E1 6 / /W i d t ho fe a c ho ft h et w ol i n e st h a td e l i m i tt h eb o a r d / /W i d t ha n dH e i g h to fe a c hb l o c ko fap i e c e
8/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9

# d e f i n eB O A R D _ P O S I T I O N3 2 0 # d e f i n eB O A R D _ W I D T H1 0 # d e f i n eB O A R D _ H E I G H T2 0 # d e f i n eM I N _ V E R T I C A L _ M A R G I N2 0 # d e f i n eM I N _ H O R I Z O N T A L _ M A R G I N2 0 # d e f i n eP I E C E _ B L O C K S5

/ /C e n t e rp o s i t i o no ft h eb o a r df r o mt h el e f to ft h es c r e e n / /B o a r dw i d t hi nb l o c k s / /B o a r dh e i g h ti nb l o c k s / /M i n i m u mv e r t i c a lm a r g i nf o rt h eb o a r dl i m i t / /M i n i m u mh o r i z o n t a lm a r g i nf o rt h eb o a r dl i m i t / /N u m b e ro fh o r i z o n t a la n dv e r t i c a lb l o c k so fam a t r i xp i e c e

/ // / B o a r d / /c l a s sB o a r d { p u b l i c : B o a r d i n tG e t X P o s I n P i x e l s i n tG e t Y P o s I n P i x e l s b o o lI s F r e e B l o c k b o o lI s P o s s i b l e M o v e m e n t v o i dS t o r e P i e c e v o i dD e l e t e P o s s i b l e L i n e s b o o lI s G a m e O v e r p r i v a t e : ( P i e c e s* p P i e c e s ,i n tp S c r e e n H e i g h t ) ; ( i n tp P o s ) ; ( i n tp P o s ) ; ( i n tp X ,i n tp Y ) ; ( i n tp X ,i n tp Y ,i n tp P i e c e ,i n tp R o t a t i o n ) ; ( i n tp X ,i n tp Y ,i n tp P i e c e ,i n tp R o t a t i o n ) ; ( ) ; ( ) ;

e n u m{P O S _ F R E E ,P O S _ F I L L E D} ; / /P O S _ F R E E=f r e ep o s i t i o no ft h eb o a r d ;P O S _ F I L L E D=f i l l e i n tm B o a r d[ B O A R D _ W I D T H ] [ B O A R D _ H E I G H T ] ;/ /B o a r dt h a tc o n t a i n st h ep i e c e s P i e c e s* m P i e c e s ; i n tm S c r e e n H e i g h t ; v o i dI n i t B o a r d ( ) ; v o i dD e l e t e L i n e( i n tp Y ) ;

} ;

# e n d i f/ /_ B O A R D _

Now, lets see each different method. InitBoard method is just a nested loop that initializes all the board blocks to POS_FREE. 1 2 3 4 5 6 7 8 9 1 0 1 1 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = I n i tt h eb o a r db l o c k sw i t hf r e ep o s i t i o n s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dB o a r d : : I n i t B o a r d ( ) { f o r( i n ti=0 ;i<B O A R D _ W I D T H ;i + + ) f o r( i n tj=0 ;j<B O A R D _ H E I G H T ;j + + ) m B o a r d [ i ] [ j ]=P O S _ F R E E ; }

StorePiece method, just stores a piece in the board by filling the appropriate blocks as POS_FILLED. There is a nested loop that iterates through the piece matrix and store the blocks in the board. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = S t o r eap i e c ei nt h eb o a r db yf i l l i n gt h eb l o c k s P a r a m e t e r s : > >p X : H o r i z o n t a lp o s i t i o ni nb l o c k s > >p Y : V e r t i c a lp o s i t i o ni nb l o c k s > >p P i e c e : P i e c et od r a w > >p R o t a t i o n :1o ft h e4p o s s i b l er o t a t i o n s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dB o a r d : : S t o r e P i e c e( i n tp X ,i n tp Y ,i n tp P i e c e ,i n tp R o t a t i o n ) { / /S t o r ee a c hb l o c ko ft h ep i e c ei n t ot h eb o a r d f o r( i n ti 1=p X ,i 2=0 ;i 1<p X+P I E C E _ B L O C K S ;i 1 + + ,i 2 + + )
9/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7

f o r( i n tj 1=p Y ,j 2=0 ;j 1<p Y+P I E C E _ B L O C K S ;j 1 + + ,j 2 + + ) { / /S t o r eo n l yt h eb l o c k so ft h ep i e c et h a ta r en o th o l e s i f( m P i e c e s > G e t B l o c k T y p e( p P i e c e ,p R o t a t i o n ,j 2 ,i 2 )! =0 ) m B o a r d [ i 1 ] [ j 1 ]=P O S _ F I L L E D ; }

IsGameOver checks if there are blocks in the first row. That means the game is over. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = C h e c ki ft h eg a m ei so v e rb e c a s eap i e c eh a v ea c h i v e dt h eu p p e rp o s i t i o n R e t u r n st r u eo rf a l s e = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / b o o lB o a r d : : I s G a m e O v e r ( ) { / / I ft h ef i r s tl i n eh a sb l o c k s ,t h e n ,g a m eo v e r f o r( i n ti=0 ;i<B O A R D _ W I D T H ;i + + ) { i f( m B o a r d [ i ] [ 0 ]= =P O S _ F I L L E D )r e t u r nt r u e ; } } r e t u r nf a l s e ;

DeleteLine is the method that erases a line and moves all the blocks of upper positions one row down. It just starts from the line that has to be removed, and then, iterating through the board in a nested loop, moves all the blocks of the upper lines one row done. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = D e l e t eal i n eo ft h eb o a r db ym o v i n ga l la b o v el i n e sd o w n P a r a m e t e r s : > >p Y : V e r t i c a lp o s i t i o ni nb l o c k so ft h el i n et od e l e t e = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dB o a r d : : D e l e t e L i n e( i n tp Y ) { / /M o v e sa l lt h eu p p e rl i n e so n er o wd o w n f o r( i n tj=p Y ;j>0 ;j ) { f o r( i n ti=0 ;i<B O A R D _ W I D T H ;i + + ) { m B o a r d [ i ] [ j ]=m B o a r d [ i ] [ j 1 ] ; } } }

DeletePossibleLines is a method that removes all the lines that should be erased from the board. It works by first checking which lines should be removed (the ones that have all their horizontal blocks filled). Then, it uses the DeleteLine method in order to erase that line and move all the upper lines one row down. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = D e l e t ea l lt h el i n e st h a ts h o u l db er e m o v e d = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dB o a r d : : D e l e t e P o s s i b l e L i n e s( ) { f o r( i n tj=0 ;j<B O A R D _ H E I G H T ;j + + ) { i n ti=0 ; w h i l e( i<B O A R D _ W I D T H ) { i f( m B o a r d [ i ] [ j ]! =P O S _ F I L L E D )b r e a k ; i + + ; } i f( i= =B O A R D _ W I D T H )D e l e t e L i n e( j ) ;
10/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 8 1 9

IsFreeBlock is a trivial method that checks out if a board block is filled or not. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = R e t u r n s1( t r u e )i ft h et h i sb l o c ko ft h eb o a r di se m p t y ,0i fi ti sf i l l e d P a r a m e t e r s : > >p X : H o r i z o n t a lp o s i t i o ni nb l o c k s > >p Y : V e r t i c a lp o s i t i o ni nb l o c k s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / b o o lB o a r d : : I s F r e e B l o c k( i n tp X ,i n tp Y ) { i f( m B o a r d[ p X ] [ p Y ]= =P O S _ F R E E )r e t u r nt r u e ;e l s er e t u r nf a l s e ; }

Until now we have been always talking about blocks. But in order to draw them to the screen we need to specify the position in pixels. So, we need two methods (GetXPosInPixels and GetYPosInPixels ) in order to obtain the horizontal and vertical position in pixels of a given block. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = R e t u r n st h eh o r i z o n t a lp o s i t i o n( i np i x e l s )o ft h eb l o c kg i v e nl i k ep a r a m e t e r P a r a m e t e r s : > >p P o s : H o r i z o n t a lp o s i t i o no ft h eb l o c ki nt h eb o a r d = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / i n tB o a r d : : G e t X P o s I n P i x e l s( i n tp P o s ) { r e t u r n ((B O A R D _ P O S I T I O N-( B L O C K _ S I Z E*( B O A R D _ W I D T H/2 ) ))+( p P o s*B L O C K _ S I Z E )) ; } / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = R e t u r n st h ev e r t i c a lp o s i t i o n( i np i x e l s )o ft h eb l o c kg i v e nl i k ep a r a m e t e r P a r a m e t e r s : > >p P o s : H o r i z o n t a lp o s i t i o no ft h eb l o c ki nt h eb o a r d = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / i n tB o a r d : : G e t Y P o s I n P i x e l s( i n tp P o s ) { r e t u r n(( m S c r e e n H e i g h t-( B L O C K _ S I Z E*B O A R D _ H E I G H T ) )+( p P o s*B L O C K _ S I Z E )) ; }

IsPossibleMovement is the last and most complex method of Board class. This method will be used later in the main loop to check if the movement of a piece is possible or not. The method compares all the blocks of a piece with the blocks already stored in the board and with the board limits. That comparison is made by iterating through the piece matrix and comparing with the appropriate 55 area in the board. If there is a collision that means the movement is not possible, so it returns false. If there is no collision, the movement is possible and it returns true.

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

11/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3

/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = C h e c ki ft h ep i e c ec a nb es t o r e da tt h i sp o s i t i o nw i t h o u ta n yc o l l i s i o n R e t u r n st r u ei ft h em o v e m e n ti s p o s s i b l e ,f a l s ei fi tn o tp o s s i b l e P a r a m e t e r s : > >p X : H o r i z o n t a lp o s i t i o ni nb l o c k s > >p Y : V e r t i c a lp o s i t i o ni nb l o c k s > >p P i e c e : P i e c et od r a w > >p R o t a t i o n :1o ft h e4p o s s i b l er o t a t i o n s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / b o o lB o a r d : : I s P o s s i b l e M o v e m e n t( i n tp X ,i n tp Y ,i n tp P i e c e ,i n tp R o t a t i o n ) { / /C h e c k sc o l l i s i o nw i t hp i e c e sa l r e a d ys t o r e di nt h eb o a r do rt h eb o a r dl i m i t s / /T h i si sj u s tt oc h e c kt h e5 x 5b l o c k so fap i e c ew i t ht h ea p p r o p r i a t ea r e ai nt h eb o a r d f o r( i n ti 1=p X ,i 2=0 ;i 1<p X+P I E C E _ B L O C K S ;i 1 + + ,i 2 + + ) { f o r( i n tj 1=p Y ,j 2=0 ;j 1<p Y+P I E C E _ B L O C K S ;j 1 + + ,j 2 + + ) { / /C h e c ki ft h ep i e c ei so u t s i d et h el i m i t so ft h eb o a r d i f( i 1<0 | | i 1>B O A R D _ W I D T H -1 | | j 1>B O A R D _ H E I G H T-1 ) { i f( m P i e c e s > G e t B l o c k T y p e( p P i e c e ,p R o t a t i o n ,j 2 ,i 2 )! =0 ) r e t u r n0 ; } / /C h e c ki ft h ep i e c eh a v ec o l l i s i o n e dw i t hab l o c ka l r e a d ys t o r e di nt h em a p i f( j 1> =0 ) { i f( ( m P i e c e s > G e t B l o c k T y p e( p P i e c e ,p R o t a t i o n ,j 2 ,i 2 )! =0 )& & ( ! I s F r e e B l o c k( i 1 ,j 1 ) )) r e t u r nf a l s e ; }

/ /N oc o l l i s i o n r e t u r nt r u e ;

Step 3: The game


Now we are going to implement a general class, called Game, that itializes the game, draws the board and pieces by drawing each block as a rectangle (using another class that we will see later called IO that uses SDL) and creates new random pieces. This is the header, Game.h: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 # i f n d e f_ G A M E _ # d e f i n e_ G A M E _ / /-I n c l u d e s# i n c l u d e" B o a r d . h " # i n c l u d e" P i e c e s . h " # i n c l u d e" I O . h " # i n c l u d e< t i m e . h > / /-D e f i n e s# d e f i n eW A I T _ T I M E7 0 0

/ /N u m b e ro fm i l l i s e c o n d st h a tt h ep i e c er e m a i n sb e f o r eg o i n g1b l o c kd o

/ // / G a m e / /c l a s sG a m e { p u b l i c :
12/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8

G a m e

( B o a r d* p B o a r d ,P i e c e s* p P i e c e s ,I O* p I O ,i n tp S c r e e n H e i g h t ) ;

v o i dD r a w S c e n e( ) ; v o i dC r e a t e N e w P i e c e( ) ; i n tm P o s X ,m P o s Y ; i n tm P i e c e ,m R o t a t i o n ; p r i v a t e : i n tm S c r e e n H e i g h t ; / /S c r e e nh e i g h ti np i x e l s i n tm N e x t P o s X ,m N e x t P o s Y ; / /P o s i t i o no ft h en e x tp i e c e i n tm N e x t P i e c e ,m N e x t R o t a t i o n ; / /K i n da n dr o t a t i o no ft h en e x tp i e c e B o a r d* m B o a r d ; P i e c e s* m P i e c e s ; I O* m I O ; i n tG e t R a n d( i n tp A ,i n tp B ) ; v o i dI n i t G a m e ( ) ; v o i dD r a w P i e c e( i n tp X ,i n tp Y ,i n tp P i e c e ,i n tp R o t a t i o n ) ; v o i dD r a w B o a r d( ) ; / /P o s i t i o no ft h ep i e c et h a ti sf a l l i n gd o w n / /K i n da n dr o t a t i o nt h ep i e c et h a ti sf a l l i n gd o w n

} ;

# e n d i f/ /_ G A M E _

As you can see, the current piece is defined using 4 variables: mPosX, mPosY (the position of the piece in blocks), mPiece (the type of the piece), mRotation (the current matrix that defines the piece, as we have seen, each piece has four matrices, one for each rotation). Lets see the implementation of the methods. GetRand is a trivial method that returns a random number between two boundaries. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = G e tar a n d o mi n tb e t w e e nt oi n t e g e r s P a r a m e t e r s : > >p A :F i r s tn u m b e r > >p B :S e c o n dn u m b e r = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / i n tG a m e : : G e t R a n d( i n tp A ,i n tp B ) { r e t u r nr a n d( )%( p B-p A+1 )+p A ; }

InitGame, takes care of the initialization of the game by selecting the first and next piece randomly. The next piece is shown so the player can see which piece will appear next. This method also sets the position in blocks of that pieces. We use two methods that we have seen before in Pieces class: GetXInitialPosition and GetYInitialPosition in order to initialize the piece in the correct position. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = I n i t i a lp a r a m e t e r so ft h eg a m e = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dG a m e : : I n i t G a m e ( ) { / /I n i tr a n d o mn u m b e r s s r a n d( ( u n s i g n e di n t )t i m e ( N U L L ) ) ; / /F i r s tp i e c e m P i e c e m R o t a t i o n m P o s X m P o s Y / / N e x tp i e c e m N e x t P i e c e m N e x t R o t a t i o n m N e x t P o s X m N e x t P o s Y =G e t R a n d( 0 ,6 ) ; =G e t R a n d( 0 ,3 ) ; =( B O A R D _ W I D T H/2 )+m P i e c e s > G e t X I n i t i a l P o s i t i o n( m P i e c e ,m R o t a t i o n ) ; =m P i e c e s > G e t Y I n i t i a l P o s i t i o n( m P i e c e ,m R o t a t i o n ) ; =G e t R a n d( 0 ,6 ) ; =G e t R a n d( 0 ,3 ) ; =B O A R D _ W I D T H+5 ; =5 ;

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

13/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

CreateNewPiece method sets the next piece as the current one and resets its position, then selects a new next piece. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = C r e a t ear a n d o mp i e c e = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dG a m e : : C r e a t e N e w P i e c e ( ) { / /T h en e wp i e c e m P i e c e =m N e x t P i e c e ; m R o t a t i o n =m N e x t R o t a t i o n ; m P o s X =( B O A R D _ W I D T H/2 )+m P i e c e s > G e t X I n i t i a l P o s i t i o n( m P i e c e ,m R o t a t i o n ) ; m P o s Y =m P i e c e s > G e t Y I n i t i a l P o s i t i o n( m P i e c e ,m R o t a t i o n ) ; / /R a n d o mn e x tp i e c e m N e x t P i e c e =G e t R a n d( 0 ,6 ) ; m N e x t R o t a t i o n =G e t R a n d( 0 ,3 ) ;

DrawPiece is a really easy method that iterates through the piece matrix and draws each block of the piece. It uses green for the normal blocks and blue for the pivot block. For drawing the rectangles it calls to DrawRectangle method of the class IO that we will see later. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = D r a wp i e c e P a r a m e t e r s : > >p X : H o r i z o n t a lp o s i t i o ni nb l o c k s > >p Y : V e r t i c a lp o s i t i o ni nb l o c k s > >p P i e c e : P i e c et od r a w > >p R o t a t i o n :1o ft h e4p o s s i b l er o t a t i o n s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dG a m e : : D r a w P i e c e( i n tp X ,i n tp Y ,i n tp P i e c e ,i n tp R o t a t i o n ) { c o l o rm C o l o r ; / /C o l o ro ft h eb l o c k / /O b t a i nt h ep o s i t i o ni np i x e li nt h es c r e e no ft h eb l o c kw ew a n tt od r a w i n tm P i x e l s X=m B o a r d > G e t X P o s I n P i x e l s( p X ) ; i n tm P i x e l s Y=m B o a r d > G e t Y P o s I n P i x e l s( p Y ) ; / /T r a v e lt h em a t r i xo fb l o c k so ft h ep i e c ea n dd r a wt h eb l o c k st h a ta r ef i l l e d f o r( i n ti=0 ;i<P I E C E _ B L O C K S ;i + + ) { f o r( i n tj=0 ;j<P I E C E _ B L O C K S ;j + + ) { / /G e tt h et y p eo ft h eb l o c ka n dd r a wi tw i t ht h ec o r r e c tc o l o r s w i t c h( m P i e c e s > G e t B l o c k T y p e( p P i e c e ,p R o t a t i o n ,j ,i ) ) { c a s e1 :m C o l o r=G R E E N ;b r e a k ; / /F o re a c hb l o c ko ft h ep i e c ee x c e p tt h ep i v o t c a s e2 :m C o l o r=B L U E ;b r e a k ; / /F o rt h ep i v o t } i f( m P i e c e s > G e t B l o c k T y p e( p P i e c e ,p R o t a t i o n ,j ,i )! =0 ) m I O > D r a w R e c t a n g l e ( m P i x e l s X+i*B L O C K _ S I Z E , m P i x e l s Y+j*B L O C K _ S I Z E , ( m P i x e l s X+i*B L O C K _ S I Z E )+B L O C K _ S I Z E-1 , ( m P i x e l s Y+j*B L O C K _ S I Z E )+B L O C K _ S I Z E-1 , m C o l o r ) ;

DrawBoard is similiar to the previous method. It draws two blue columns that are used as the limits of the boards. Then draws the board blocks that are flagged as POS_FILLED in a nested loop. 1 2 3 4 5 6 7 8 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = D r a wb o a r d D r a wt h et w ol i n e st h a td e l i m i tt h eb o a r d = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dG a m e : : D r a w B o a r d( )
14/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2

{ / /C a l c u l a t et h el i m i t so ft h eb o a r di np i x e l s i n tm X 1=B O A R D _ P O S I T I O N-( B L O C K _ S I Z E*( B O A R D _ W I D T H/2 ) )-1 ; i n tm X 2=B O A R D _ P O S I T I O N+( B L O C K _ S I Z E*( B O A R D _ W I D T H/2 ) ) ; i n tm Y=m S c r e e n H e i g h t-( B L O C K _ S I Z E*B O A R D _ H E I G H T ) ; / /C h e c kt h a tt h ev e r t i c a lm a r g i ni sn o tt os m a l l / / a s s e r t( m Y>M I N _ V E R T I C A L _ M A R G I N ) ; / /R e c t a n g l e st h a td e l i m i t st h eb o a r d m I O > D r a w R e c t a n g l e( m X 1-B O A R D _ L I N E _ W I D T H ,m Y ,m X 1 ,m S c r e e n H e i g h t-1 ,B L U E ) ; m I O > D r a w R e c t a n g l e( m X 2 ,m Y ,m X 2+B O A R D _ L I N E _ W I D T H ,m S c r e e n H e i g h t-1 ,B L U E ) ; / /C h e c kt h a tt h eh o r i z o n t a lm a r g i ni sn o tt os m a l l / / a s s e r t( m X 1>M I N _ H O R I Z O N T A L _ M A R G I N ) ; / /D r a w i n gt h eb l o c k st h a ta r ea l r e a d ys t o r e di nt h eb o a r d m X 1+ =1 ; f o r( i n ti=0 ;i<B O A R D _ W I D T H ;i + + ) { f o r( i n tj=0 ;j<B O A R D _ H E I G H T ;j + + ) { / /C h e c ki ft h eb l o c ki sf i l l e d ,i fs o ,d r a wi t i f( ! m B o a r d > I s F r e e B l o c k ( i ,j ) ) m I O > D r a w R e c t a n g l e(m X 1+i*B L O C K _ S I Z E , m Y+j*B L O C K _ S I Z E , ( m X 1+i*B L O C K _ S I Z E )+B L O C K _ S I Z E-1 , ( m Y+j*B L O C K _ S I Z E )+B L O C K _ S I Z E-1 , R E D ) ; } }

DrawScene, just calls the previous methods in order to draw everything. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = D r a ws c e n e D r a wa l lt h eo b j e c t so ft h es c e n e = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / v o i dG a m e : : D r a w S c e n e( ) { D r a w B o a r d( ) ; D r a w P i e c e( m P o s X ,m P o s Y ,m P i e c e ,m R o t a t i o n ) ; D r a w P i e c e( m N e x t P o s X ,m N e x t P o s Y ,m N e x t P i e c e ,m N e x t R o t a t i o n ) ; }

/ /D r a wt h ed e l i m i t a t i o nl i n e sa n db / /D r a wt h ep l a y i n gp i e c e / /D r a wt h en e x tp i e c e

Step 4: Easy drawing, window management and keyboard input using SDL, isolated from the game logic
IO.cpp and IO.h are the files that implement the IO class. It uses SDL in order to create the window, clear it, update the screen and take care of the keyboard input. You can check out IO.cpp and IO.h files in order to see its implementation. Im not going to explain the methods that are SDL related. You can change this class in order to use a different renderer (like IndieLib, Allegro, OpenGL, Direct3d, etc). This is the header (IO.h): 1 2 3 4 5 6 7 8 9 1 0 # i f n d e f_ I O _ # d e f i n e_ I O _ / /-I n c l u d e s# i f n d e fL I N U X # i n c l u d e" S D L / i n c l u d e / S D L . h " # i n c l u d e" S D L / S D L _ G f x P r i m i t i v e s / S D L _ g f x P r i m i t i v e s . h " # e l s e # i n c l u d e< S D L / S D L . h >
15/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2

# i n c l u d e" S D L / S D L _ G f x P r i m i t i v e s / s d l _ g f x p r i m i t i v e s . h " # e n d i f # p r a g m ac o m m e n t( l i b ," S D L / l i b / S D L . l i b " ) # p r a g m ac o m m e n t( l i b ," S D L / S D L _ G f x P r i m i t i v e s / S D L _ G f x P r i m i t i v e s _ S t a t i c . l i b " ) / /-E n u m se n u mc o l o r{ B L A C K ,R E D ,G R E E N ,B L U E ,C Y A N ,M A G E N T A ,Y E L L O W ,W H I T E ,C O L O R _ M A X } ;/ /C o l o r s / // / I O / /c l a s sI O { p u b l i c : I O v o i dD r a w R e c t a n g l e v o i dC l e a r S c r e e n i n tG e t S c r e e n H e i g h t i n tI n i t G r a p h i n tP o l l k e y i n tG e t k e y i n tI s K e y D o w n v o i dU p d a t e S c r e e n } ; # e n d i f/ /_ I O _ ( ) ; ( i n tp X 1 ,i n tp Y 1 ,i n tp X 2 ,i n tp Y 2 ,e n u mc o l o rp C ) ; ( ) ; ( ) ; ( ) ; ( ) ; ( ) ; ( i n tp K e y ) ; ( ) ;

Step 5: The main loop


The main loop is quite simple. In each frame we draw everything. Later, we use keyboard input in order to move the piece. Before each movement, we first check out if it is possible. We also measure the time in order to move the piece down every n milliseconds. When the piece fall down one block, we check out if that movement is possible, if not, we store the piece in the board. We also check out if there are blocks in the upper row, if so, the game is over. Lets see Main.cpp step by step: First, we initialize all the classes. Then, we get the actual milliseconds, which will be used to determine when the piece should move down. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 # i n c l u d e" G a m e . h " # i f n d e fL I N U X # i n c l u d e< w i n d o w s . h > # e n d i f / * = = = = = = = = = = = = = = = = = = M a i n = = = = = = = = = = = = = = = = = = * / i n tW I N A P IW i n M a i n( H I N S T A N C Eh I n s t a n c e ,H I N S T A N C Eh P r e v I n s t a n c e ,L P S T Rl p C m d L i n e ,i n tn C m d S h o w ) { / /-V a r s/ /C l a s sf o rd r a w i n gs t a f f ,i tu s e sS D Lf o rt h er e n d e r i n g .C h a n g et h em e t h o d so ft h i sc l a s s / /i no r d e rt ou s ead i f f e r e n tr e n d e r e r I Om I O ; i n tm S c r e e n H e i g h t=m I O . G e t S c r e e n H e i g h t ( ) ; / /P i e c e s P i e c e sm P i e c e s ; / /B o a r d B o a r dm B o a r d( & m P i e c e s ,m S c r e e n H e i g h t ) ; / /G a m e G a m em G a m e( & m B o a r d ,& m P i e c e s ,& m I O ,m S c r e e n H e i g h t ) ;
16/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

2 8 2 9 3 0 3 1 3 2 3 3

/ /G e tt h ea c t u a lc l o c km i l l i s e c o n d s( S D L ) u n s i g n e dl o n gm T i m e 1=S D L _ G e t T i c k s ( ) ;

This is the main loop. We can exit by pressing ESC. In each frame we clear and update the screen and draw everything. 1 2 3 4 5 6 7 8 9 / /-M a i nL o o pw h i l e( ! m I O . I s K e y D o w n( S D L K _ E S C A P E ) ) { / /-D r a wm I O . C l e a r S c r e e n( ) ; m G a m e . D r a w S c e n e( ) ; m I O . U p d a t e S c r e e n( ) ; / /C l e a rs c r e e n / /D r a ws t a f f / /P u tt h eg r a p h i cc o n t e x ti nt h es c r e e n

We start with the input. If we press left, down or right we try to move the piece in that directions. We only move the piece if the movement is possible. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 / /-I n p u ti n tm K e y=m I O . P o l l k e y ( ) ; s w i t c h( m K e y ) { c a s e( S D L K _ R I G H T ) : { i f( m B o a r d . I s P o s s i b l e M o v e m e n t( m G a m e . m P o s X+1 ,m G a m e . m P o s Y ,m G a m e . m P i e c e ,m G a m e . m R o t a t i o n ) ) m G a m e . m P o s X + + ; b r e a k ; } c a s e( S D L K _ L E F T ) : { i f( m B o a r d . I s P o s s i b l e M o v e m e n t( m G a m e . m P o s X-1 ,m G a m e . m P o s Y ,m G a m e . m P i e c e ,m G a m e . m R o t a t i o n ) ) m G a m e . m P o s X ; b r e a k ; } c a s e( S D L K _ D O W N ) : { i f( m B o a r d . I s P o s s i b l e M o v e m e n t( m G a m e . m P o s X ,m G a m e . m P o s Y+1 ,m G a m e . m P i e c e ,m G a m e . m R o t a t i o n ) ) m G a m e . m P o s Y + + ; b r e a k ; }

By pressing x, the piece will fall down directly to the ground. This is really easy to implement by trying to move the piece down until the movement is not possible. Then we store the piece, delete possible lines and check out if the game is over, if not, we create a new piece. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9

c a s e( S D L K _ x ) : { / /C h e c kc o l l i s i o nf r o mu pt od o w n w h i l e( m B o a r d . I s P o s s i b l e M o v e m e n t ( m G a m e . m P o s X ,m G a m e . m P o s Y ,m G a m e . m P i e c e ,m G a m e . m R o t a t i o n ) ){m G a m e . m m B o a r d . S t o r e P i e c e( m G a m e . m P o s X ,m G a m e . m P o s Y-1 ,m G a m e . m P i e c e ,m G a m e . m R o t a t i o n ) ; m B o a r d . D e l e t e P o s s i b l e L i n e s( ) ; i f( m B o a r d . I s G a m e O v e r ( ) ) { m I O . G e t k e y ( ) ; e x i t ( 0 ) ; } m G a m e . C r e a t e N e w P i e c e ( ) ; } b r e a k ;

By pressing z we rotate the piece. With the methods that we have already implement this is an easy task. The rotation is in fact to
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 17/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

change to the next rotated stored piece. We first should check that the rotated piece will be drawn without colliding, if so, we sets this rotation as the current one. 1 2 3 4 5 6 7 8

c a s e( S D L K _ z ) : { i f( m B o a r d . I s P o s s i b l e M o v e m e n t( m G a m e . m P o s X ,m G a m e . m P o s Y ,m G a m e . m P i e c e ,( m G a m e . m R o t a t i o n+1 )%4 ) m G a m e . m R o t a t i o n=( m G a m e . m R o t a t i o n+1 )%4 ; } b r e a k ;

If WAIT_TIME passed, the piece should fall down one block. We have to check out if the movement is possible, if not, the piece should be stored and we have to check if we can delete lines. We also see if the game is over, if not, we create a new piece. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 / /-V e r t i c a lm o v e m e n tu n s i g n e dl o n gm T i m e 2=S D L _ G e t T i c k s ( ) ;

i f( ( m T i m e 2-m T i m e 1 )>W A I T _ T I M E ) { i f( m B o a r d . I s P o s s i b l e M o v e m e n t( m G a m e . m P o s X ,m G a m e . m P o s Y+1 ,m G a m e . m P i e c e ,m G a m e . m R o t a t i o n ) ) { m G a m e . m P o s Y + + ; } e l s e { m B o a r d . S t o r e P i e c e( m G a m e . m P o s X ,m G a m e . m P o s Y ,m G a m e . m P i e c e ,m G a m e . m R o t a t i o n ) ; m B o a r d . D e l e t e P o s s i b l e L i n e s( ) ; i f( m B o a r d . I s G a m e O v e r ( ) ) { m I O . G e t k e y ( ) ; e x i t ( 0 ) ; } } } m G a m e . C r e a t e N e w P i e c e ( ) ;

m T i m e 1=S D L _ G e t T i c k s ( ) ;

} }

r e t u r n0 ;

And thats all! Please leave a comment if you see some mistakes, language errors or if you have any doubts or just to say thanks!

Credits
Javier Lpez Special thanks: Imelior, who fixed English mistakes and compiled the tutorial under Linux. Special thanks: Javier Santana, who added #ifndef sentences and pointed that was necessary to use libsdl-gfx1.2-dev and libsdl1.2dev under Linux.

Bonus
Dont forget to play with the defines. Crazy example: 1 2 3 # d e f i n eB L O C K _ S I Z E5 # d e f i n eB O A R D _ W I D T H9 0 # d e f i n eB O A R D _ H E I G H T9 0 / /W i d t ha n dH e i g h to fe a c hb l o c ko fa / /B o a r dw i d t hi nb l o c k s / /B o a r dh e i g h ti nb l o c k s
18/44

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

You should follow me on Twitter


Did you like the tutorial? Then you should follow me on twitter here.

Comments 138 comments


miguelSantirso
Muy bueno el tutorial! La verdad es que estos tutoriales son una currada tremenda, pero tambin ayudan a mucha gente Espero que tengas nimo para seguir con ello!
December 14, 2008

AdrianMG
Nice tut thanks!
December 15, 2008

Joe Larson
I like your solution to having the output be scalable. I also find your rotation solution interesting. I wouldnt have taken that approach. 16 lines of data per piece! Draws the code out, which is fine in most cases. But personally I prefer one piece declaration and procedurally rotate it. It makes the code a bit harder to read, tho, but thats the way I did Alleytris. (Hope you dont mind the plug *insert smiley*)
December 15, 2008

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

19/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Javier Lpez
Thank you taking a look to the tutorial, Joe. I usually prefer to have everything precalculated. It is usually easier to understand and faster. But a procedurally rotation is also interesting. Alleytris is a great example of a good tetris clones. Good work!
December 15, 2008

nihed
Good job! +1
December 15, 2008

Francois MAROT
Nice tutorial, it reminds me of all the love and effort I put into my first C++ programm that was Tetris !!! I had just learned what templates and subclasses were and used all that without knowing about pointers etc But it was fun ! Anyway, as it was my 1st programm, it was in no way as clean as your solution, but what fun ! Thanks for reminding me this good ol time
December 15, 2008

Javier Lpez
Francois, Tetris also make me remember the first games I tried to develope 15 years ago using Basic in my old MSX. Snif! Snif!
December 15, 2008

Javier Lpez
Thank you for the appreciative comments, Adrin and Miguel
December 16, 2008

Javier Lpez
Thanks, nihed.
December 16, 2008

Ivan Guardado
Really is a great tutorial! Im hoping to create my first game!
December 16, 2008

Javier Lpez
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 20/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

@Ivan, thank you! Dont forget to post your game.


December 16, 2008

Emeka
This is great, I have been thinking of how to great this for something now, and I have always being intimidated. Now, you have done the hard job for me. But, I feel like asking some stupid questions. Could you support me get started in game programming? Or will you be there to answer my questions?
December 22, 2008

Javier Lpez
Welcome Emeka, If you want to get started in game programming, I think this is the correct place. Feel free to ask whatever questions you have! Ill try to help you, of course! I think a good start is also visiting IndieLib.com. IndieLib is a 2d game engine I developed for rapid game prototyping under c++. I think is a good engine in order to start learning game programming. See you there and here! Soon more tutorials!
December 23, 2008

Emeka
Javier, Thanks so much, I checked out IndieLib game engine tutorial and I liked what I found. However, the tutorial is based on only visual C++ 2008 Express Edition. I have different compiler. Hope IndieLib would work in a environment. Emeka
December 24, 2008

Javier Lpez
Dont worry, post your doubts on IndieLib forums. Im sure we will be able to help you.
December 24, 2008

Santiago
Muy bueno el tutorial! Una pregunta, estoy usando la librera IO que hiciste para hacer mi versin del tetris, pero me sale un error de undefined reference to _boxColor cuando compilo con el CodeBlocks. Te agradezco si me pods ayudar. saludos!
December 28, 2008

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

21/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Javier Lpez
Bienvenido, Santiago! El error posiblemente se deba a que no se ha linkado correctamente SDL_GfxPrimitives_Static.lib. La tienes en tetris_tutorial_sdlSDLSDL_GfxPrimitives. Yo us vc2008 Express Edition y para linkarla us la directiva pragma. Es decir, puse: #pragma comment (lib, SDL/lib/SDL.lib) #pragma comment (lib, SDL/SDL_GfxPrimitives/SDL_GfxPrimitives_Static.lib) Puede que en Code Blocks los linkados se hagan de manera distinta. No te lo s decir porque no lo uso. Busca o pregunta cmo se linka una .lib en un proyecto de Code Blocks y supuestamente resolvers el problema. Mantenme al tanto, espero que lo puedas solucionar Si decides pasarte a vc20008 express edition, te recuerdo slo que es gratis. Un saludo y suerte!
December 28, 2008

Santiago
Me anduvo con el vc++2008 usando el mismo proyecto del tutorial pero con mis archivos, muchas gracias!
January 1, 2009

Callum
Thanks for the well commented tutorial! I was wanting to create tetris but was unsure how i could implement the blocks and i certainly like your method Just a quick question, how does the maths behind the GetXPosInPixels work? i just cant seem to figure out how you manage to convert block size into pixels and would love to find out how it actually does. Thanks
January 8, 2009

Javier Lpez
You are welcome! So, for getting the X coordinate in pixeles we have: return ( ( BOARD_POSITION (BLOCK_SIZE * (BOARD_WIDTH / 2)) ) + (pPos * BLOCK_SIZE) ); Maybe this line seems a bit tricky, so maybe you could try to use pen and paper in order to change the variables by real numbers. Lets say: BOARD_POSITION = 320 (in pixels) BLOCK_SIZE = 16 (in pixels)
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 22/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

BOARD_WIDTH = 10 (in blocks) pPos = 2 (Horizontal position of the block (in blocks) So, having that, we now we want to get the x coordinate in pixels of a block that is in a third column (it is in pPos 2 and we have to start counting by 0). So, with this line: ( ( BOARD_POSITION (BLOCK_SIZE * (BOARD_WIDTH / 2)) ) We just one to calculate the x coordinate in pixels of the left corner of the board. Remember that BOARD_POSITION is the center of the board, in pixels. So we have to substract to that value the amount in pixels of the half of the blocks that fill in a board. After that we add to this value this line: (pPos * BLOCK_SIZE) This is just for getting the width of 2 blocks, that is the same as getting the x coordinate in order to draw a block in position 3. I hope this was explanatory enough.
January 9, 2009

Shotaro
This is brilliant! Thank you so much! Really, really helpful.
January 15, 2009

Johan
Great tutorial, there is only one thing thats unclear for me and that is that in step 1 Im not sure in what header file I should put the information of the blocks.
January 17, 2009

Javier Lpez
In fact I put the information not in the header but in the .cpp (check out the sourcecode you can download). But it could be in the header if you wish.
January 17, 2009

Alexess
very good!! thanks you.
January 20, 2009

Javier Lpez
Thank to all you for the appreciative comments
January 20, 2009

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

23/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

arbi
hey Javier Lopez I am studying game and simulation programming at devry and I got many years to get to programm a game engine. I am just curious what books you read or what helped you learn c++ to make a game engine by yourself. Wow that is amazing what you have done.. I love your work.
January 23, 2009

Emeka
I have not been able to get my head around this method mPiecesInitialPosition , please explain it further.
January 24, 2009

Bob
Ive written a Java Tetris years ago its an applet and should run in your browser (if Javas installed). The game does not use matrices or anything the positions are hard-coded too, but in an even simpler way
January 25, 2009

Javier Lpez
@Emeka, mPiecesInitialPosition is just an array that has the displacement that each piece (the 4 possible rotations) has to suffer in order to be positioned when it is created. Just check out the two images that shows that in the tutorial: http://gametuto.com/images/wrong_and_good_tetris_positions.png http://gametuto.com/images/wrong_and_good_tetris_positions.png In the first image, we has drawn the piece without applying the translation, so it is not correctly positioned when created. On the second image, we have applied the translation to the piece, so it is correcly positioned when created. I hope this will clarify your doubt.
January 25, 2009

Javier Lpez
@arbi, thank you very much for the appreciative words. It took me lot of years to in order to arrive to the 1.0 release. I think I spent more or less 5 years, but only working in spare time and with breaks of several months. Im quite autodidact, I learnt c++ by myself, and before that Pascal, and Basic. I also work using Java and php. And Im starting to enjoy c#. Ive been programming since I was 9 years old and Im currently 27 years old. I dont know any book I can tell you to take a look. Anything I needed was is on Google, specially reading source code from other open source engines and talking in forums. I used to write a lot on stratos-ad.com forums (spanish). Now, I usually write on IndieLib forums: http://www.indielib.com/forum It would be great if you come to IndieLib forum and join our community. People like you, motivated and well
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 24/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

prepared, will give the community a lot of value. Furthermore, if you are interested on engine programming, maybe you want to join the IndieLib development team, what do you think? See you!
January 25, 2009

Javier Lpez
@Bob, that seems cool. What about explaining it a bit more? Im interested!
January 25, 2009

Vitor Almeida
Very nice tutorial (simple, easy to understand and detailed). Backlinked to my blog. Keep up the good work.
January 26, 2009

Javier Lpez
@Victor Almeida, thank you very much neighbour (iberic)
January 26, 2009

arbi
Javier, Thank you for your reply sorry I am replying so late. Been busy programming. Taking 2 courses now. I am not so much a self learner but I am starting to be. Anyways the book that I use right now are C++ primer plus, Problem solving with C++, game programming with C++, and i look online for http://www.cplusplus.com/tutorial. It will take me some time just like you to really get a grasp of this language. One question I had did you first truely understand one language before you jumped into another or did you learn two at the same time. I am studying c++ but I know Java is easier since it has a garbage collector. Also C++ is ideal for game development but java is still used. I will for sure joing your forum. Thank you again for the reply. Sorry for the late reply
February 6, 2009

Javier Lpez
@arbi Hello again! That books you are talking about seems really interesting. Answering your question, sometimes, like right now, I have to deal with learning different languages at the same time. For example, currently Im learning Php, because I need it for my day work. But Im still learning C++ because you know: you never knows everything about nothing The best way to go is to have a good knowledge of POO programming and the different programming metodologies. Then is just a matter of getting some new concepts and keywords when learning a new language.

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

25/44

14/10/13
February 8, 2009

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Veiko
Hi Javier. This is really good tutorial. Its clean, its simple and it explains really good whats going on. The big bonus is pictures. I have searched such Tetris tutorial for a long time. I have bookmarked your site and this tutorial. Thanks alot Javier.
February 10, 2009

Javier Lpez
@Veiko, thanks alot to you for reading!
February 10, 2009

BERNARD
This is good man. Do some of these code lines change when using borland c++. I am a beginner in c++ programming.
February 15, 2009

vkson
Thanks Javier Lpez. Tutor is detail. Easy to understand. But Could you add rotation solution into project is very good Thanks
February 15, 2009

Niko
Javier thank you so much for very good tutorial/lesson. Keep up good work!
February 16, 2009

Dave
Thanks that was a really great tutorial! I want to get into programming games and have found this really useful. Please write more soon! I was motivated to build tetris by this great lecture about an academic course that looks inspiring. http://video.google.com/videoplay?docid=7654043762021156507 http://www1.idc.ac.il/tecs/ Thought you might enjoy these fun tetris vids:
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 26/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

http://www.youtube.com/watch?v=SYRLTF71Sow&feature=related http://www.notsonoisy.com/tetris/index.html
February 20, 2009

Bobic
Very Good! Thanks.
February 24, 2009

Nick
Thanks for writing a good tutorial. Ive found it to be in-depth and very easy to understand. Just one question why the use of WinMain and windows.h in main.cpp? From what I have done in the past using SDL on Windows, you can just use the normal int main(int argc, char** argv) entry point.
March 2, 2009

chris
Thank you so much for this tuitorial. I have some questions, How can i execute this game? Right now im figuring it out but failed to execute it. Do i need to debug it in C++? and what file? or how can i make an exe DOS file out of it, to make it work?
March 15, 2009

mya
nice ,awesome !!!!!!!! i expect more such ideas in upcoming game
April 18, 2009

Roque Terrani
Muchsimas gracias por el cdigo y el tutorial, me ayudo bastante a terminar de darme la idea de como afrontar la mecnica del juego. Te comento que yo lo estoy programando en C para *PIC (microcontrolador de la empresa Microchip) y ya tenia bastante pensado los diagramas de la lgica del juego pero tu cdigo me ayuda en la cuestin tcnica de la programacin. Obviamente ni bien lo tenga terminado paso a dejar fotos del bicho funcionando asi todos nos sentimos orgullosos. pd: Para los amantes de la electrnica dejo una breve explicacin de lo que estoy haciendo. Pic 16f877a, 4 registros de desplazamiento para las filas, 6 matrices de 75 leds(2,5 cm x 5,3 cm en total), 3 display de 7 segmentos para las lineas y 4 pulsadores que hacen de teclas (Luego sern remplazados con una interfaz a teclado ps/2)
April 19, 2009

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

27/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Wai Kam
Love it, and thx!!!
May 5, 2009

Battie
Thank you very much for this wonderfull tutorial However i want to implement something that it computes what the highest point of the POS_FILLED is. The time to drop a block should always be 6 no matter how much there POS_FILLED there are. in addition to that there should be 4 seconds inbetween a block is dropped and a new block appears in the air. Is this possible or not?
May 18, 2009

Soo
Nice tutorial, thanks you a lot for that. I just have one question, i got 1 error when i tried to compile this project. It is about an undefined reference to boxColor. Can anyone help me with this plz ?
May 29, 2009

Sam L.
Hi Javier, I translate your tutorial to Brazilian Portuguese. Download: http://www.4shared.com/file/95111285/e93a8160/Tetris_tutorial_para_iniciantes.html vlw!
June 13, 2009

sshow
Great tutorial! Im using this to write my first game in C#. Planning on implementing multi-player features like in TetriNET. Great introduction for me, even though Ive never used C++. The logic was basically what I was looking for, and you are explaining things _well_! Thanks!
June 16, 2009

Alekh
Hi a really nice tutorial. But I want to add some animations to this like the line complete animation. How do i
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 28/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

implement such animations? Pls help me on this.. I really need help..


June 17, 2009

Tom
Hey thanks alot for this tutorial its very enlightening. However, I am having trouble understanding the StorePiece method in the Board class. I just cant seem to follow it. Could you explain in more detail what this is doing? Also, why are you adding the PIECE_BLOCKS constant to pX and pY? Thanks
June 23, 2009

Emeka
Hello, I cant find where this field mScreenHeight was assigned value? Emeka
June 23, 2009

jimmy
Had a lot of fun going through this great tutorial, but I run into the craziest problem when I try to alter the values of the #defines the compiler still keeps the original #define values! Ive tried changing the #defines to const ints, and Ive even completely commented out the #defines and the compiler still doesnt mind. How does this happen??
June 29, 2009

roxlu
Wow amazing! What a clear, nice code! Though Im wondering, in Board::StorePiece() you make a call to mPieces->GetBlockType(pPiece, pRotation, j2, i2) .. should this be: mPieces->GetBlockType(pPiece, pRotation, i2, j2) .. for clearity sake?
July 15, 2009

Heather
You know, you can very easily, without complex code, rotate the tetris pieces and cut down on your piece array. In a nested loop where the array is 5 wide by 5 wide: No rotation: newarray[j][i] = oldarray[j][i] 1 step counterclockwise: newarray[4 - i][j] = oldarray[j][i]
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 29/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

2 step counterclockwise: newarray[4 - j][4 - i] = oldarray[j][i] 3 step counterclockwise: newarray[i][4-j] = oldarray[j][i]


July 19, 2009

~MoZzY~
Very good tutorial2 thumbs up
July 28, 2009

FFUUU
Not another Tetris clone!
July 31, 2009

Joe
Greetings, Has anyone managed to run this in MacOSX? I managed to build it but when I tried to run it I got the following error message: Terminating app due to uncaught exception NSInternalInconsistencyException, reason: Error (1002) creating CGSWindow
August 23, 2009

Skaruts
Just a minor detail I noticed: I remember in the original tetris that the pieces rotations only cycled through 2 positions. Taking the I shape example figures, figures 3 and 4 wouldnt exist. They would only cycle through 1 and 2. Despite that, very nice and neat tut. Thanks.
September 16, 2009

adnoctum
Thx man, i will help from this because i will make a tetris in VB, thanx again
November 11, 2009

feng
vr nice n well explained tutorial reli appreciate it! but i was stuck in here : bool Board::IsGameOver() { //If the first line has blocks, then, game over
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 30/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

for (int i = 0; i < BOARD_WIDTH; i++) { if (mBoard[i][0] == POS_FILLED) return true; } return false; } xxxxxxxxxx 00 00 00 00 00 00 00 00 00 1111111111 this function is it checking the xxxxxx row? then i tot suppose the if statement sud look like this if (mBoard[i][0] == POS_FILLED) return true; anyone pls correct me if im wrong
November 13, 2009

feng
(sorry the upper comment is wrong, pls refer to this) vr nice n well explained tutorial reli appreciate it! but i was stuck in here : bool Board::IsGameOver() { //If the first line has blocks, then, game over for (int i = 0; i < BOARD_WIDTH; i++) { if (mBoard[i][0] == POS_FILLED) return true; } return false; } xxxxxxxxxx 00 00 00 00 00 00 00 00 00
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 31/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

1111111111 this function is it checking the xxxxxx row? then i tot suppose the if statement sud look like this if (mBoard[0][i] == POS_FILLED) return true; anyone pls correct me if im wrong
November 13, 2009

feng
oohh.. i understand d mBoard[x(width)][y(height)]
November 13, 2009

supreme aryal
Thank you for the tutorial. I implemented it in C. The code looks much the same, but it is in one large file. You can see it in action at: http://www.youtube.com/watch?v=WD52EoZHNJA
November 14, 2009

soitsthateasy
great tut, just one difficulty im having with it. forgive me if its obvious, im new to game programming!!! I created a new piece ( a cross) and im not sure how to make the random piece function thingy include it in its selections if someone could post and say all the places i would have to change, it would be greatly appreciated
November 28, 2009

afshin
thanks javier.youre my hero
November 30, 2009

feng
very nice, neat and easy understanding tetris tutorial! appreciate this so much !
December 3, 2009

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

32/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Pilot Boy
Nice tut! Just what I needed!
December 22, 2009

amir yazdanbakhsh
Dear Lpez, Thx for your brilliant game and tutorial. Im going to write a simple game that are somehow similar with Tetris. I want capture mouse clicks. If player click on the one of these blocks, that blocks disapper, and if the blocks reach bottom of the board the user get a negative point. Id appreciate if you could help me. Best Regards
December 26, 2009

Hyoungseok
I was looking for a good tutorial making TETRIS! so here I am Im not good in english but Id like to say thank you very much from south korea
January 13, 2010

zhunie chen
heLLo!! ammhppanyone cud help me how to make a bingo chess game in Turbo C???? pLz
February 8, 2010

Mahi
Hi, I went through the whole code and i really really appriciate it, though i am a rookie in programming. So can anyone help me getting this code with direct3d please? i will really appreciate it Thnx, Mahi
March 15, 2010

Eko
Hello going through your code, but stopped at
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 33/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

int Pieces::GetXInitialPosition (int pPiece, int pRotation) { return mPiecesInitialPosition [pPiece [pRotation][0]; } the method only have 2 parameter, but return 3? What is the third doing [0] ? And why doesnt it say anything about it in description above? Ty for help.
March 26, 2010

fina
tahnk u so much for your knowledge:)
April 3, 2010

hordi
This tutorial shows Tetris logic very nicely. I started to make a Tetris game with C# and this really helps. One thing though, I fail to see the point of the pivot points since theyre not even used to rotate the piece.
April 8, 2010

Mariano
Que compilador o programa usas?(Compiler or what program do you use?)
April 21, 2010

Douglas
estou comeando agora a programar jogos, voc tem algum tutorial de como fazer engine pra principiantes ou algum livro para me recomendar
April 21, 2010

lyzen
i want a program c++ games or ATM before thanks,,,
May 21, 2010

mnlgarbe
Muchas Gracias sinceras, un gran trabajo que ayuda a muchisima gente que como yo, empezamos en esto
June 3, 2010

AbduRahman
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 34/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

thanks for this amazing tutorial ,this what i was looking for ,logic explanation,not just code ready to run that you have no idea what is it about .but for a beginner like me , it taught me great things about games programming ,specially dealing with arrays .i wrote the game using c#. i hope you make similar tutorials
June 4, 2010

Chris Cansler
Janvier, excellent tutorial! On Board.cpp, the comment for GetYPosInPixels() indicates that parameter pPos is the Horizontal position of the block in the board. I think it should be Vertical position of the block in the board instead.
July 14, 2010

Esteban Hernandez
Excelent tuto dude, helps to solve some issue on my game
July 23, 2010

Albert
Hello! This tetrix tuto is really illustrative! I need it because I was requested to develop a Puyo Puyo console application, should be easier but I cant find anything on the net. Very good Tuto!
August 6, 2010

M Salazar
Thanks a ton! This is an awesome tutorail, I really appreicaite it!
August 17, 2010

Andrey
Thanks for the tutorial. Can someone help me, please? I want to add the text before the game board starts. Can you help me? Please?
September 6, 2010

jayden
hi, recently i managed to rebuild spaceinvaders and also pacman with java on a mac osx, compiled it with unix javac. some difficulties at the beginning, but managed to compile the games and had them working. but actually i want to learn c++ rather then java. and now i have some problems compiling c++ with unix, tried some sources, but didnt work out till now. can you give me some advice, on how i get your tetrisclone compiled? muchas gracias, jayden

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

35/44

14/10/13
September 17, 2010

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

jayden
can you give me some advice, on how i get your tetris-clone compiled? compiled on unix, i meant. thanks
September 17, 2010

forkon
Thanks a lot!Its very usful for me!
October 27, 2010

Craya
I am not clear of the mPiecesInitialPosition[7][4][2]array,such as the initial of square,why is it [-2][-3]? how did you definite the number -2 and -3?
November 21, 2010

Craya
In the IO.cc, you didnt give the implementation of the boxColor(), I could figure out it, could you add it? Thanks a lot!
November 23, 2010

heaven
JAVIER, can you translate sdl to directx for me?? i learn directx,and i dont understand about sdl?
November 24, 2010

ali
hi tnx a lot for this good luck javier
January 2, 2011

kpc
Oh-my-god! Man, you just saved my life with tomorrows assignment! Cheers!
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 36/44

14/10/13
January 19, 2011

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

bharat C. Soni
Thank you sir, Tetris tutorial in c++ is really good. i am going to develop my own demo game based on this tutorials. thank you again and keep doing this.
January 28, 2011

Cynth
Is there no easier way to explain how to make this tetris game? The pieces created, in what file is it saved to, is it to the main.cpp or the pieces.h file? Im a lil confused here :-s
February 8, 2011

yraz
its so nice tutorial. i can learn much about game programing from this. arigato gozaimasu
March 2, 2011

Ahmed Ahsan Khan


hey man dude i went through your tutorial several times. i have decided to make a tetris game for my OOP project. dude i didnt find the function definitions of the IO.h header file. are the functions predefined or what? would you give us the definitions of that header file functions too?. thanks..
May 2, 2011

Srinivas
Thank you very much. This tutorial and game source code will help me for my project in my college, as I am doing Game Programming course. Thank you once again!!
May 29, 2011

Camilo Muoz
Gracias por el tutorial. Me ha servido mucho para entrarme en este mundo de programacin de videojuegos. Excelente lgica, excelentes soluciones. De nuevo gracias
June 7, 2011

Tony
hello! tell me please, what type of project should i create?
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 37/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

i am using visual c++ 2008 express edition. but when i am trying to compile i get a lot of error LNK2019. all of them seems like that: error LNK2019: unresolved external symbol public: void __thiscall Game::CreateNewPiece(void) (? CreateNewPiece@Game@@QAEXXZ) referenced in function _WinMain@16. help me please. big thanx!!!
June 19, 2011

Timothy
I dont understand the function of int mPiecesInitialPosition [7 /*kind */ ][4 /* rotation */ ][2 /* position */] How does it work? What is the initial position of the block?
July 27, 2011

Timothy
The Wrong Initial Position is on the left upper corner of the board? So we have to shift two to three boxes to get the correct initial position? Can the wrong initial position on the right corner? Why it is located at the upper left?
July 27, 2011

Timothy
And the second question is out of the 55 cells There are nothing?(except the filled position) nothing means we dont have to assign any memory location to those black background
July 27, 2011

Timothy
I think i have solved the first question
July 27, 2011

CoreAn_Crack3rZ
Hi! Ive been reading this tutorial since I was a game development trainee. Thanks a lot for this! you a link for this.
August 3, 2011

Ill give

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

38/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

MICRO_
I SEE ERROR :Program cant start because SDL.dll is missing from your computer Try reinstalling the program to fix probleM PLEASE HELP ME TO FIX IT
August 16, 2011

Rafael
Hola, tena el motor del libro Teach yourself game programmint in 24 hours de Michael Morrison. Implement la mquina virtual de Lua y reprogram ste tutorial de Tetris en Lua. Despus de algunos quebraderos de cabeza con la forma de trabajar con tablas, valores de true y false, bucles, etc (no conoca el lenguaje y ha sido una buena forma de aprender), por fin termin. Muchas gracias por el tutorial.
September 4, 2011

Owais
Great Work Sir! I really need this tutorial
September 8, 2011

Mau Jabur
Hi! Great tutorial! Im working on porting your code to Processing. Ill give you the credits, so, would you please tell me how you want the comment on the top of the file? Thank you
September 9, 2011

mr.Assasin
thanks to you because it help me.. so much
September 26, 2011

ingles
Thank you very much! The tutorial is very good.
October 7, 2011

Ximena
Is there any way to stay in touch with you? Id like to ask you some things thanks!

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

39/44

14/10/13
November 9, 2011

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

zalook
Thanks
April 2, 2012

YongYe
Hi,Javier Lpez: I have already developed a very powerful Tetris Game based on a Shell Script(bash) which i had made it a open soure project at https://github.com/yongye/shell all by myself , you can see the screenshot and the matrix equation for the core algorithm at http://bbs.chinaunix.net/thread-3614425-1-1.html. In my way, all the movements of the pieces are performed by mathematics calculation which is the key to make the game so flexible and powerful that you can perform you own customizations for any kinds of the shapes of the pieces dynamically! Im self-learning C++ now, id love to port my Shell-based Tetris to a c++ one, ill take some time to learn your tutorial although we perform the task in such a different way!
April 24, 2012

samie
Hi, I got a copy of your the program in form environment in visual studio c++, then i clicked on the debug the program error events associated with Wayne did not compile: error C1083: Cannot open include file: Pieces.h: No such file or directo.what program have this program?
June 21, 2012

Mark
Hi, Javier Lpez: I am making this tetris game using only the ASCII characters because I am still not knowledgeable about graphics.. but I did it in a not noticeable way that it is an ASCII chars. I put up all those char to make a square of block and put it up again to make a tetris piece. the drawing is done but I am having a hard time to make this collision detection, I googled about the collision detection and landed in here. I found your code in collision detection very confusing for a newbie like me, and I dont really understand how you put the pieces in the board anyway, just to finish my tetris game, I copied your code and put in mine. I based by collision detection and storing of pieces on your code and the drawing stuffs is mine. and poof! it is done..I am glad that I did it with the help of you. Thank you sir!
June 24, 2012

Joshua
Great tutorial. Quick point: for people with fedora, you should be able to install sdl libraries necessary using sudo yum install SDL*
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 40/44

14/10/13
August 6, 2012

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

nintendo games
Nice blog here! Also your site a lot up fast! What web host are you the usage of? Can I get your associate hyperlink to your host? I wish my web site loaded up as fast as yours lol
August 29, 2012

Andy
In IsPossibleMovement : if ( i1 BOARD_WIDTH 1 || j1 > BOARD_HEIGHT 1) { .. } Shouldnt be i1 < BOARD_WIDTH 1 * BLOCK_SIZE ?? If i1 == BOARD_WIDTH 2, it still can be a problem can't it ? Extra tutorial, thanks a lot !
October 1, 2012

rtt
Hi, I am using vs2008 and try to build the debug version. The compile is ok. When running, I got the error: Runtime Error! R6034 An application has made an attempt to load the C runtime libraty incorrectly. Please contact teh applications support team for more information. Any help will be appreciated!
November 16, 2012

Protegon
No pude ejecutarlo en QT
December 18, 2012

orly
i really appreciate it.. i am a first year college IT student and our prof. told us to make a tetris game in tc++ but the pieces.h dont work. // Includes #include Pieces.h // Defines why??? pls help me..

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

41/44

14/10/13
December 20, 2012

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Wedge
Dnde est el tutorial en espaol?
March 19, 2013

Ste
Thank you, Javier Lpez! I want to know how to imply a Teris game, and this is just what Im looking for! I really like the data structure you used to store the pieces and the methods with which you check the collision. Great job!
March 25, 2013

Ste
And here is a tutorial on its implementation. [http://stewannahavefun.blogspot.com/]
April 8, 2013

Jose De Jesus
Hi, i have a question, how did i print the pieces, can you help me wih that ? Thanks, EXCELLENT tutorial
April 16, 2013

aakash
anybody have Sudoku source code using turbo c.
April 18, 2013

Alexey
ooo! thanks, this is what I need. chic tutorial
April 25, 2013

Alagaola
Thanks ! ^_^
April 27, 2013

Wendy
Thank you very much for the tutorial. However, I couldnt have it run. My system is Ubuntu 12.04. After tying to complie, it gives me the follwoing error message:
javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/ 42/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

main.cpp: (.text+0X8d):undefiend reference to SDL_GetTicks main.cpp: (.text+0X389):undefiend reference to SDL_GetTicks main.cpp: (.text+0X484):undefiend reference to SDL_GetTicks IO.o: In function IO::UpdateScreen();: IO.cpp:(.text+0xd5):undefined reference to SDL_Flip IO.o: In function IO::Pollkey();: IO.cpp:(.text+0xd5):undefined reference to SDL_PollEvent .. .. .. (etc, a lot of similar messages) collect2: ld returned 1 exit status make:*** [tetris] Error 1 Any idea? I tried everything that comes to my mind Thank you in advance!
May 12, 2013

Wendy
Well, I found the error. Im running g++ on Linux, and the makefile is not compatible with my setting. So instead of directly using the make file, just run it directly: g++ Main.cpp Game.cpp Board.cpp Pieces.cpp IO.cpp -o tetris -lSDL -lSDL_gfx
May 14, 2013

Jason
Hello, Im having some problem with compiling. Im using Code::Blocks on Windows 7. It compiles with 2 warnings and my Build Log says this: Warning: .drectve -defaultlib:MSVCRT unrecognized Warning: .drectve -defaultlib:OLDNAMES unrecognized Cannot export _aacircleColor: symbol not found Cannot export _aacircleRGBA: symbol not found Has anyone else run into this? Any success in fixing?
July 29, 2013

toby
undefined reference to boxColor ? Can anyone help ?
August 15, 2013

Add your comment

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

43/44

14/10/13

Tetris tutorial in C++ platform independent focused in game logic for beginners | Javilop

Name Email Write your question or comment

Add comment

You should follow me on Twitter, Dribbble & Forrst

Contact me

javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

44/44

You might also like