# Tutorial: Tetris in c++ from scratch

## Recommended Posts

Tetris tutorial in c++ render independent in one hour After spending lot of hours this weekend I've just finished the Tutorial of how to create a clone of Tetris using c++. We are going to learn how to create a stunning and great Tetris clone from scratch using simple and clean c++. And this will take you less than a hour! This is the perfect tutorial for beginners, there are a lot of game tutorials about how to create a Tetris on Internet, but I hope this will be one of the best. 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. Let’s go! PS: Don’t forget to play with the “defines”. Crazy example: #define BLOCK_SIZE 5 // Width and Height of each block of a #define BOARD_WIDTH 90 // Board width in blocks #define BOARD_HEIGHT 90 // Board height in blocks [Edited by - Loover on December 14, 2008 8:38:34 PM]

##### Share on other sites
This board uses html for images and links, not BBCode. Just so you know.

Thanks! Fixed!

##### Share on other sites
One line that irks me, mostly because it's an example of unnecessarily complex code.

In CBoard::IsFreeBlock:
if (mBoard [pX][pY] == POS_FREE) return 1; else return 0;
should be
return mBoard [pX][pY] == POS_FREE;

##### Share on other sites
Quote:
 Original post by Splinter of ChaosOne line that irks me, mostly because it's an example of unnecessarily complex code.In CBoard::IsFreeBlock:if (mBoard [pX][pY] == POS_FREE) return 1; else return 0; should bereturn mBoard [pX][pY] == POS_FREE;

I think that the first way is cleaner to read and is fine for a beginner tutorial. In everyday code I would use the second as well.

##### Share on other sites
Thanks for taking a look. As cignox1 said, I used a bit redundant sourcecode in order to be more redeable. This is one of the examples, but there are more. For example in the main loop there is a portion of code that is duplicated and could be joint into a single function.

But like this tutorial is intended to be used by beginners I think is better this way. I'm planning to make and advanced tutorial, using sprites, background, etc. In that tutorial I will improve the sourcecode and explain why is like this.

Anyway, thank you for reading it and for the feedback.

I'm asking myself... do I have lot of english mistakes?

##### Share on other sites
	CDraw *mDraw = new CDraw ();	// Pieces	CPieces *mPieces = new CPieces;	// Board	CBoard *mBoard = new CBoard (mPieces, mScreenHeight);	// Game	CGame *mGame = new CGame (mBoard, mPieces, mDraw, mScreenHeight);	// game loop 	// ----- Free -----	delete mBoard;	delete mPieces;	delete mDraw;	delete mGame;

This makes me raise an eyebrow. You dont really need to dynamically allocate these do you? The goal should be to use the stack unless you cant.

while (!mDraw->IsKeyDown (SDLK_ESCAPE))

I wanted to highlight that your CDraw object handles input. This is not an ideal division of responsibilities.

Quote:
 I'm asking myself... do I have lot of english mistakes?

I only skimmed it and I did notice a few.

##### Share on other sites
Thank you very much for the feedback, Cantos. I've fixed...

"This makes me raise an eyebrow. You dont really need to dynamically allocate these do you? The goal should be to use the stack unless you cant."

Fixed. No more dinamically memory is used now.

"I wanted to highlight that your CDraw object handles input. This is not an ideal division of responsibilities."

Fixed. Now it is called "CIO" :), and it handles window management, keyboard input and drawing. I wanted to have al the I/O methods of SDL together in one class, because I'm not focusing the tutorial in them. The tutorial is focused in the game logic.

"I'm asking myself... do I have lot of english mistakes?" => "I only skimmed it and I did notice a few."

Please, can you write here some of them. I really want to fix them. I'm making a big effort in order to write in readeable english.

As you can see, I really appreciate feedback :)

##### Share on other sites
Quote:
 Original post by Splinter of Chaosif (mBoard [pX][pY] == POS_FREE) return 1; else return 0;

Why 1 and 0? C++ has a bool datatype.

##### Share on other sites
Yes, but I think that it is something like:

#define true 1
#define fale 0

Isn't it?

So it is the same, I guess.

##### Share on other sites
Quote:
 Original post by LooverYes, but I think that it is something like:#define true 1#define fale 0Isn't it?So it is the same, I guess.
Not exactly. But why use 0 and 1 when you're using C++? You might as well use malloc everywhere instead of new, and not use classes.

##### Share on other sites
Ok :) I will change them to true / false.

##### Share on other sites
Quote:
 Original post by LooverYes, but I think that it is something like:#define true 1#define fale 0Isn't it?So it is the same, I guess.

Even if that was so (it isn't, for more information, GoTW 26 is a nice read), then using bool is still better, because it conveys the intention much better than an int would do.

Overall, I think your tutorial is nice, but there's a few things I would change: you're declaring your loop variables beforehand, in the functions scope (as was mandatory in C), but I think it's better to declare them in the for-loop scope instead (for(int i = 0; ...) instead of int i; for(i = 0; ...)), because that's the only place where you use them and that's also a direct assignment (int i; leaves i unassigned, which, when forgotten, can lead to nasty situations).

Also, the C in front of your class' names is redundant (a decent IDE will easily tell you that you're dealing with a class) and makes looking at alphabetically sorted lists harder. I used to do the same, but in the long run, it's really not as useful as it appears to be.

I'm sorry if that comes over at nitpicking, but it's a beginners tutorial, and beginners will take over such habits. :)

##### Share on other sites
Quote:
 Original post by LooverYes, but I think that it is something like:#define true 1#define fale 0Isn't it?

No, it's not. You are thinking C. bool is a true datatype in C++.

##### Share on other sites
Thank you DevFred, I didn't know that. Captain P, I will fix that things too.

Believe it or not, I was thinking about every details you have said. I usually use the vars as you talked about, but I ported the sourcecode from a c version and I forgot to change that details.

At the end I'm going not only to teach, but to learn myself really valuable details.

Thank you very much, and please, keep giving feedback. I will fix all these things tonight.

By the way, could someone help me with the english mistakes?

##### Share on other sites
I don't see the need for mPiecesRotationDisplacement. Couldn't you just move the block correctly at the definition mPieces, so that the blue block is always at the same position? At the moment, you seem to align at the top left which is just an arbitrary choice.

##### Share on other sites
Quote:
 Overall, I think your tutorial is nice

Ah! And thank you for the first appreciative comment.

##### Share on other sites
Quote:
 I don't see the need for mPiecesRotationDisplacement. Couldn't you just move the block correctly at the definition mPieces, so that the blue block is always at the same position? At the moment, you seem to align at the top left which is just an arbitrary choice.

Yes, you can, but then you will need a bigger matrix: 5x5 for some of the pieces, like the "I". Furthere more, each time you want to change your "pivot point", you will have to change the rest of matrices, but...

... maybe this approach is better, because the pivot point is not going to change. Having bigger matrices isn't expensive at all. And we will delete "mPiecesRotationDisplacement" that is good.

Ok, I think maybe is better this way. I'll change this too, and the tutorial will be easier.

Anyway, translating the pivot point to the origin is not arbitrary. Is the usual rule in order to make a translation with a hotspot: translate to the origin, rotate, translate again.

Thanks for the feedback!

##### Share on other sites
Thank you so much for making this! I know what I'm doing tonight : )

##### Share on other sites
Quote:
 Thank you so much for making this! I know what I'm doing tonight : )

You are welcome! I will try to fix that details asap so you can enjoy it tonight!

Obligitory.

##### Share on other sites
Quote:

Do you think would be better to change all "defines" per "consts"?

##### Share on other sites
Quote:
 I don't see the need for mPiecesRotationDisplacement. Couldn't you just move the block correctly at the definition mPieces, so that the blue block is always at the same position? At the moment, you seem to align at the top left which is just an arbitrary choice.

I have been thinking about changing the matrices to 5x5 blocks and directly moving the blocks at the definition mPieces. But there is a problem, if you do that, you will still need to move each rotated piece to a proper position when you first create it in the board. I mean, you will need an array, identical to mPiecesRotationDisplacement in order to put in the correct position each piece the first time it appears on the board.

But I think I prefer that, because with my original method, some pieces where starting filling two blocks in the board. And every piece should only use 1 block when it starts.

So, thank you again for the feedback. I've fixing all the details!

##### Share on other sites
Quote:
 Original post by LooverI have been thinking about changing the matrices to 5x5 blocks

Why not leave it at 4x4? The I block can be rotated like this:
..*...*...*...*.........****......*...*...*...*.........****....

Yeah it's not perfectly symmetrical, but who cares?

Quote:
 Original post by Looveryou will still need to move each rotated piece to a proper position when you first create it in the board.

Just let them all spawn at the same position. If they are aligned somewhere in the center of the 4x4 square, it should be fine.

##### Share on other sites
This way is not respecting the pivot block, :P

Don't worry, I'm just finishing the new version. 5x5 matrices, is not a problem of storage, hehe.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628334
• Total Posts
2982147

• 9
• 24
• 9
• 9
• 13