• Advertisement
Sign in to follow this  

Tetris Programming?

This topic is 4039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I read through the Begginer articles. Very thouroughly, but I am still confused with makeing the tetris clone. Yes I know it is very pathetic. but does someone have a written tutorial, or somethin to teach one how to make one? that would make my day thanx!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

You'd be better off asking a specific question if the articles dont make sense to you.
What part doesnt make sense for example?

...because I sure dont have a tutorial for Tetris.

Share this post


Link to post
Share on other sites
I haven't tried creating tetris, but I'd imagine it would be fairly complicated for a beginner. I would personally start with making a game of pong.

Share this post


Link to post
Share on other sites
Hi Strass150,
Tetris is a good place to start. At least, it was my first game of any substance because it was my favorite game in high school (we're talking 13h days... :)

Anyway, here is kind of the order I did things.
1) draw one brick square, solid color.

const int BRICK_WIDTH = 9;
const int BRICK_HEIGHT = 10;
void drawBrick( int xCoordinate, int yCoordinate, int color )
{
for( int y = 0; y < BRICK_HEIGHT; y++ )
for( int x = 0; x < BRICK_WIDTH; x++ )
PlotPixel(x,y,color);
}

2) draw a darker line near the left and bottom to look more 3d.
3) draw 4 such bricks in a shape like a tetris block, positioning by hand.
4) draw 4 bricks based on a little piece of C code like

int brickPos[4][2] = { {0,0}, {1,0}, {1,0}, {1,1} }; // square block

for( brickIdx = 0; brickIdx < 4; brickIdx++ )
{
drawBrick(
brickPos[brickIdx][0] * BRICK_WIDTH,
brickPos[brickIdx][1] * BRICK_HEIGHT,
YELLOW );
}


5) rotate a block through its positions:

// L-block in all four rotations
int brickPos[4][4][2] =
{
{ {0,0}, {0,1}, {1,1}, {2,1} },
{ {1,0}, {1,1}, {0,2}, {1,2} },
{ {0,0}, {1,0}, {2,0}, {2,1} },
{ {0,0}, {1,0}, {0,1}, {0,2} },
};

int rotationIdx = 2; // rotated twice

for( brickIdx = 0; brickIdx < 4; brickIdx++ )
{
drawBrick(
brickPos[rotationIdx][brickIdx][0] * BRICK_WIDTH,
brickPos[rotationIdx][brickIdx][1] * BRICK_HEIGHT,
YELLOW );
}





6) Same for all seven blocks.
7) Position a block within the well.

int rotationIdx = 2; // rotated twice
int wellPos[2] = { 3, 5 }; // three blocks over, 5 down

for( brickIdx = 0; brickIdx < 4; brickIdx++ )
{
drawBrick(
(wellPos[0] + brickPos[rotationIdx][brickIdx][0]) * BRICK_WIDTH,
(wellPos[1] + brickPos[rotationIdx][brickIdx][1]) * BRICK_HEIGHT,
YELLOW );
}

8) Actual game is now starting... when a block lands, assign its brick positions into the well.

const int WELL_WIDTH = 10;
const int WELL_HEIGHT = 23;

int Well[WELL_HEIGHT][WELL_WIDTH] = { {0} };

void brickLanded( int brickPos[4][2], int wellPos[2], int color )
{
for( brickIdx = 0; brickIdx < 4; brickIdx++ )
{
Well[wellPos[1] + brickPos[brickIdx][1]] // y coordinate in well
[wellPos[0] + brickPos[brickIdx][0]] // x coordinate in well
= color;
}
}

9) Check to see if a block can move down.

bool canMoveToPos( int brickPos[4][2], int wellPos[2] )
{
for( brickIdx = 0; brickIdx < 4; brickIdx++ )
{
if( Well[wellPos[1] + brickPos[brickIdx][1]] // y coordinate in well
[wellPos[0] + brickPos[brickIdx][0]] // x coordinate in well
!= 0 )
return false; // this brick would collide with one already in well.
}
return true; // no collisions, so new position is okay.
}
bool canMoveDown( int brickPos[4][2], int currentWellPos[2] )
{
int newWellPos[2];
int newWellPos[0] = currentWellPos[0]; // x-coordinate the same
int newWellPos[1] = currentWellPos[1] + 1; // y-coordinate bumped down one

return canMoveToPos( brickPos, newWellPos );
}

10) Check for rotations also.

bool canRotate( int brickPos[4][4][2], int currentRotation, int currentWellPos[2] )
{
return canMoveToPos( brickPos[(currentRotation 1)%4], currentWellPos );
}

11) ..........

Share this post


Link to post
Share on other sites
That helps so much. You may have just saved my life.
I was going at that the complete wrong way. Thanks a again

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement