Advertisement

Tetris (rotation of pieces)

Started by April 01, 2002 09:05 AM
1 comment, last by ryacketta 22 years, 7 months ago
Hello Everyone! I have a little Tetris clone coming along fairly well, nearing completed actualy. Looking back through the code I see several areas where improvement''s can be made. The most noticeable is my huge fugly array for shapes (int shapes[7][4][4][4][2]), I am sure I can narrow that down a considerable amount by only hold a basic set of 4 shapes; each with one set of coordinates. Once that has been accomplished, the array will be converted into a class and or into STL (for educational/exp purposes). My only problem is the rotation of the shape( my current array holds all possible shape coordinate combinations for all possible shape rotations). I am not sure how I would go about taking a shapes coords and doing the specific math to rotate it as desired. Could someone kindly assist me in generating a rotation function? I am n
I've never seen a five-dimensional array in C before.

Yeah...I would definitely go with a different storage structure/layout.

As the rotation:

Since you don't change the undlerying information by rotating the blocks (i.e. shape doesn't change, it just flips over) there is no need to store multiply copies of the shape (one for each possible orientation). Instead I would store each block once and then write a function that produces an x and y index into this two-dimensional array; you'd pass it the orientation of the block and the x and y indices that would refer to the cells of the NON-ROTATED block and it would output the indices necessary to reference the corresponding cells in the rotated block. For example:

  /* # #        This is the unrotated block. # ##                         #  ####    ##          ####          Other orientations.      #        #           #           #Now let's say you have a function that draws the block:*/void DrawBlock(int ScreenX, int ScreenY, Block* block){    int i, j;    for( i = 0; i < 2; i++ )    {        for( j = 0; j < 4; j++ )        {              DrawCell(block,ScreenX,ScreenY,CellXIndex(block,i,j,THREE_OCLOCK), CellYIndex(block,i,j,THREE_OCLOCK));        }    }}void CellXIndex(Block* block, int i, int j, int orientation){   switch(orientation)   {       case TWELVE_OCLOCK:           return i;       case THREE_OCLOCK:           return (BLOCK_HEIGHT-j);       case SIX_OCLOCK:           return (BLOCK_WIDTH-i);       case NINE_OCLOCK:            return j;   }}void CellYIndex(Block* block, int i, int j, int orientation){   switch(orientation)   {       case TWELVE_OCLOCK:           return j;       case THREE_OCLOCK:           return (BLOCK_WIDTH-i);       case SIX_OCLOCK:           return (BLOCK_HEIGHT-j);       case NINE_OCLOCK:            return i;   }}    


I hope this helps. If you have any questions about it just ask.


[edited by - bob_the_third on April 1, 2002 4:18:45 PM]
Advertisement
This code is buggy (windows), but the TObject class rotate function is what your looking for.

http://www.btinternet.com/~jeannie.zelos/tetriscpp.zip

,Jay

This topic is closed to new replies.

Advertisement