Sign in to follow this  
Loover

Tutorial: Tetris in c++ from scratch

Recommended Posts

Loover    202
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]

Share this post


Link to post
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 this post


Link to post
Share on other sites
cignox1    735
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.

Share this post


Link to post
Share on other sites
Loover    202
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 this post


Link to post
Share on other sites
Cantos    212

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 this post


Link to post
Share on other sites
Loover    202
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 this post


Link to post
Share on other sites
DevFred    840
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.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by Loover
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.
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 this post


Link to post
Share on other sites
Captain P    1092
Quote:
Original post by Loover
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.


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 this post


Link to post
Share on other sites
DevFred    840
Quote:
Original post by Loover
Yes, but I think that it is something like:

#define true 1
#define fale 0

Isn't it?

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

Share this post


Link to post
Share on other sites
Loover    202
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 this post


Link to post
Share on other sites
DevFred    840
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 this post


Link to post
Share on other sites
Loover    202
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 this post


Link to post
Share on other sites
Loover    202
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!

Share this post


Link to post
Share on other sites
Loover    202
Quote:
More helpfully, you should think about making your code const-correct.


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

Share this post


Link to post
Share on other sites
Loover    202
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 this post


Link to post
Share on other sites
DevFred    840
Quote:
Original post by Loover
I 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 Loover
you 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 this post


Link to post
Share on other sites
Loover    202
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this