Triple Buffer
Official Development Community Other
Home Tutorials Forum Contact Us
Team Pages Articles Links Special Thanks
Team Projects Tools Gurus  


  Article - Tetris in an Hour
  Written by: Evil_Greven, Edited by: Ali Akhterzadeh
Published: 24/Apr/2006




Setting Up


I often see people asking "How to make Tetris?" or "how should I start my first game?" Well, here's a short and sweet lesson on making a simple Tetris clone. No fancy stuff this time, just the basics.

This mini-tutorial is strictly for C++. I'll assume at least basic familiarization. You must create this project as a Win32 Application project. It includes only three files, main.cpp, bitmapobject.cpp, and bitmapobject.h.

In main.cpp:

//FALLING BLOCK GAME! //main.cpp //tell compiler to not include the evil MFC overhead. #define WIN32_LEAN_AND_MEAN //need this for windows stuff. #include //need this for srand and rand #include //now let's include our bitmapobject definitions #include "bitmapobject.h"

That's all you need to start off. Next, let's include some handy-dandy defines that makes editing your program a thousand times easier. Seriously.


//let's give our window a name #define WINDOWCLASS "FallingBlockGame" //let's give our window a title...er caption. #define WINDOWTITLE "A Falling Block Game!"

That takes care of Windows. Now let's think ahead. What should we use if we're going to make a tetris clone? Hmm... well, seems to me map tiles would work perfectly! Let's make a size for each tile, then make a size for the map, PLUS an 8-block wide sidebar.


//since we're using square blocks, let's only use a single size. const int TILESIZE=16; //now for the map... const int MAPWIDTH=10; const int MAPHEIGHT=30; const int GREY=8;

Easy! Now, let's have some (you can use enumerations if you prefer) variables for the future bitmap. We need 9 colors, and 1 Does Not Draw, so 10 in all.


const int TILENODRAW=-1; const int TILEBLACK=0; const int TILEGREY=1; const int TILEBLUE=2; const int TILERED=3; const int TILEGREEN=4; const int TILEYELLOW=5; const int TILEWHITE=6; const int TILESTEEL=7; const int TILEPURPLE=8;

Alright, that's done. Let's do some function prototypes. We will need Game Init, Game Loop, Game Done, Draw Tile, Draw Map, New Block, Rotate Block, Collision Test, Move Block, Remove Row, and New Game.


bool GameInit(); // game initialization function void GameLoop(); //where the game actually takes place void GameDone(); //clean up! void DrawTile(int x, int y, int tile); //coordinates & tile type void DrawMap(); //draw the whole map.. render function, basically void NewBlock(); //create a new block! void RotateBlock(); //rotate a block.. if you can! void Move(int x, int y); //coordinates to move. int CollisionTest(int nx, int ny); //test collision of blocks void RemoveRow(int row); //remove a row.. that would be the 'x'. void NewGame(); //make a new game!

Pretty simple so far, eh? Now, let's throw in some global variables, bwahahahah! Global variables are preferred, because they are FAST. Speed is essential to a game. Thus, when you can, use them. But, be careful with them.

The first two we need are an application handle and a main window handle.


HINSTANCE hInstMain=NULL; //main app handle HWND hWndMain=NULL; //main window handle

The above are necessary for windows programming. Remember this, always.

Next, let's create an array to hold the play area map's tile types. We will add one extra row for Map to make Y-movement collision detection easier. And then, let's make a little structure for a game piece, then two variables of that structure; one for the actual piece, and one for the preview piece. Then, something we *REALLY* need is for timing. Let's call it start_time, of the DWORD type. We also need a GAMESTARTED boolean.


int Map[MAPWIDTH][MAPHEIGHT+1]; //the game map! struct Piece { int size[4][4]; int x; int y; }; Piece sPrePiece; //preview piece. Piece sPiece; //the 's' prefixes indicate this is a 'structure' DWORD start_time; //used in timing bool GAMESTARTED=false; //used by NewBlock()

NOW, let's leave main.cpp briefly, and visit bitmapobject.h:




Page: 1 2 3 4

Copyright © Triple Buffer Software 2002 - 2006.
Goto Top