Tutorial: Tetris in c++ from scratch
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]
One line that irks me, mostly because it's an example of unnecessarily complex code.
In CBoard::IsFreeBlock:
In CBoard::IsFreeBlock:
if (mBoard [pX][pY] == POS_FREE) return 1; else return 0;
should bereturn 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:should beif (mBoard [pX][pY] == POS_FREE) return 1; else return 0;
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?
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 :)
"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 Chaosif (mBoard [pX][pY] == POS_FREE) return 1; else return 0;
Why 1 and 0? C++ has a bool datatype.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement