Original Post
🔒 Locked
Tetris Programming?
Advertisement
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.
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
5) rotate a block through its positions:
6) Same for all seven blocks.
7) Position a block within the well.
8) Actual game is now starting... when a block lands, assign its brick positions into the well.
9) Check to see if a block can move down.
10) Check for rotations also.
11) ..........
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 blockfor( 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 rotationsint 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 twicefor( 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 twiceint wellPos[2] = { 3, 5 }; // three blocks over, 5 downfor( 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) ..........
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement