Tutorial: Tetris in c++ from scratch

Started by
38 comments, last by Loover 15 years, 3 months ago
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 Tetris Tutorial c++ [Edited by - Loover on December 14, 2008 8:38:34 PM]
Advertisement
This board uses html for images and links, not BBCode. Just so you know.

[size=1]Visit my website, rawrrawr.com

Thanks! Fixed!
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;
Quote:Original post by Splinter of Chaos
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;


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.
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?
	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.
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 :)
Quote:Original post by Splinter of Chaos
if (mBoard [pX][pY] == POS_FREE) return 1; else return 0;


Why 1 and 0? C++ has a bool datatype.
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.

This topic is closed to new replies.

Advertisement