• Create Account

## Tutorial: Tetris in c++ from scratch

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

39 replies to this topic

### #1Loover  Members

202
Like
0Likes
Like

Posted 14 December 2008 - 01:42 PM

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]

### #2Rraawwrr  Members

322
Like
0Likes
Like

Posted 14 December 2008 - 01:43 PM

This board uses html for images and links, not BBCode. Just so you know.

### #3Loover  Members

202
Like
0Likes
Like

Posted 14 December 2008 - 01:47 PM

Thanks! Fixed!

### #4Splinter of Chaos  Members

239
Like
0Likes
Like

Posted 14 December 2008 - 04:59 PM

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;

### #5cignox1  Members

735
Like
0Likes
Like

Posted 14 December 2008 - 07:10 PM

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.

### #6Loover  Members

202
Like
0Likes
Like

Posted 14 December 2008 - 07:53 PM

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?

### #7Cantos  Members

212
Like
0Likes
Like

Posted 14 December 2008 - 08:00 PM

	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.

### #8Loover  Members

202
Like
0Likes
Like

Posted 14 December 2008 - 08:51 PM

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 :)

### #9DevFred  Members

840
Like
0Likes
Like

Posted 15 December 2008 - 01:06 AM

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.

### #10Loover  Members

202
Like
0Likes
Like

Posted 15 December 2008 - 01:22 AM

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.

### #11Evil Steve  Members

2012
Like
0Likes
Like

Posted 15 December 2008 - 01:40 AM

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.

### #12Loover  Members

202
Like
0Likes
Like

Posted 15 December 2008 - 01:47 AM

Ok :) I will change them to true / false.

### #13Captain P  Members

1092
Like
0Likes
Like

Posted 15 December 2008 - 02:35 AM

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. :)

### #14DevFred  Members

840
Like
0Likes
Like

Posted 15 December 2008 - 02:50 AM

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++.

### #15Loover  Members

202
Like
0Likes
Like

Posted 15 December 2008 - 03:00 AM

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?

### #16DevFred  Members

840
Like
0Likes
Like

Posted 15 December 2008 - 03:18 AM

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.

### #17Loover  Members

202
Like
0Likes
Like

Posted 15 December 2008 - 03:48 AM

Quote:
 Overall, I think your tutorial is nice

Ah! And thank you for the first appreciative comment.

### #18Loover  Members

202
Like
0Likes
Like

Posted 15 December 2008 - 03:59 AM

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!

### #19Chrono1081  Members

108
Like
0Likes
Like

Posted 15 December 2008 - 04:20 AM

Thank you so much for making this! I know what I'm doing tonight : )

### #20Loover  Members

202
Like
0Likes
Like

Posted 15 December 2008 - 04:29 AM

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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.