Jump to content
  • Advertisement
Sign in to follow this  
bughunter2

Tetris Clone - rotation problem

This topic is 4055 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

Hello, I'm currently writing my first game, a Tetris clone. I have followed Triple Buffer's Tetris in an Hour tutorial, and after understanding about every piece of the game, I wanted to make it work like the Tetris Guidelines SRS (Super Rotation System) standard. I'm currently still using the code from the tutorial. Can someone help me making it work like the SRS rotation system?
/* rotate a block */
void MyBlocks::RotateBlock()
{
    /* do not allow to rotate the blocks while the game is not running or paused */
    if (bGameStarted == FALSE || bGamePaused == TRUE)
    {
        return;
    }

    int x, y, temp[4][4];

    /* copy & rotate the piece to the temporary array */
    for (y = 0; y < 4; y++)
    {
        for (x = 0; x < 4; x++)
        {
            temp[3-x][y] = sPiece.size[y][x];
        }
    }

    /* check collision of the temporary array with map borders */
    for (y = 0; y < 4; y++)
    {
        for (x = 0; x < 4; x++)
        {
            if (temp[y][x] != TILE_NODRAW)
            {
                if (sPiece.x + y < 0 || sPiece.x + y > MAP_WIDTH - 1 ||
                        sPiece.y + x < 0 || sPiece.y + x > MAP_HEIGHT - 1)
                {
                    return;
                }
            }
        }
    }

    /* check collision of the temporary array with the blocks on the map */
    for (x = 0; x < MAP_WIDTH; x++)
    {
        for (y = 0; y < MAP_HEIGHT; y++)
        {
            if (x >= sPiece.x && x < sPiece.x + 4)
            {
                if (y >= sPiece.y && y < sPiece.y +4)
                {
                    if (Map[x][y] != TILE_CLR_FILL_BLACK)
                    {
                        if (temp[x - sPiece.x][y - sPiece.y] != TILE_NODRAW)
                        {
                            return;
                        }
                    }
                }
            }
        }
    }

    /* successful, now copy the rotated temporary array to the original piece */
    for (y = 0; y < 4; y++)
    {
        for (x = 0; x < 4; x++)
        {
            sPiece.size[y][x] = temp[y][x];
        }
    }

    DrawMap();

    return;
}


[Edited by - bughunter2 on May 13, 2007 7:04:36 PM]

Share this post


Link to post
Share on other sites
Advertisement
I've solved the problem by myself.

I now use a 'width' field for every block (i.e. a piece).

When generating a block, I initialize the width to 3. When a Tower (I) or Box (O) is created I set this width to 4.

And then, when picking a new block, I copy the width from the preview block to the current block.

If anyone is interested, here is my code to fill in the pieces and to do the rotating etc.


/* fills a piece structure */
void MyBlocks::FillPiece(Piece * sPiece)
{
int newblock = rand()%7;
sPiece->width = 3;

switch (newblock)
{
case 0: /* Tower */
{
sPiece->size[0][1] = TILE_CLR_RED;
sPiece->size[1][1] = TILE_CLR_RED;
sPiece->size[2][1] = TILE_CLR_RED;
sPiece->size[3][1] = TILE_CLR_RED;
sPiece->width = 4;
}
break;

case 1: /* Box */
{
sPiece->size[1][1] = TILE_CLR_BLUE;
sPiece->size[1][2] = TILE_CLR_BLUE;
sPiece->size[2][1] = TILE_CLR_BLUE;
sPiece->size[2][2] = TILE_CLR_BLUE;
sPiece->width = 4;
}
break;

case 2: /* Pyramid */
{
sPiece->size[1][0] = TILE_CLR_GREY;
sPiece->size[0][1] = TILE_CLR_GREY;
sPiece->size[1][1] = TILE_CLR_GREY;
sPiece->size[2][1] = TILE_CLR_GREY;
}
break;

case 3: /* Left Leaner */
{
sPiece->size[0][0] = TILE_CLR_YELLOW;
sPiece->size[1][0] = TILE_CLR_YELLOW;
sPiece->size[1][1] = TILE_CLR_YELLOW;
sPiece->size[2][1] = TILE_CLR_YELLOW;
}
break;

case 4: /* Right Leaner */
{
sPiece->size[1][0] = TILE_CLR_GREEN;
sPiece->size[2][0] = TILE_CLR_GREEN;
sPiece->size[0][1] = TILE_CLR_GREEN;
sPiece->size[1][1] = TILE_CLR_GREEN;
}
break;

case 5: /*Left Knight */
{
sPiece->size[0][0] = TILE_CLR_WHITE;
sPiece->size[0][1] = TILE_CLR_WHITE;
sPiece->size[1][1] = TILE_CLR_WHITE;
sPiece->size[2][1] = TILE_CLR_WHITE;
}
break;

case 6: /* Right Knight */
{
sPiece->size[0][1] = TILE_CLR_ORANGE;
sPiece->size[1][1] = TILE_CLR_ORANGE;
sPiece->size[2][1] = TILE_CLR_ORANGE;
sPiece->size[2][0] = TILE_CLR_ORANGE;
}
break;
}
}

/* create a new block */
void MyBlocks::NewBlock()
{
/* generate a block (or pick the preview one) and/or generate one for the preview */

/* initialize the piece to all blank */
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
sPiece.size[j] = TILE_NODRAW;

sPiece.x = MAP_WIDTH/2-2;
sPiece.y = 0;

/* check if the game has yet been started */
if (bGameStarted == FALSE)
{
/* it didn't */

/*
Generate a piece right off.
From now on, use previous preview block.
*/

bGameStarted = TRUE;

FillPiece(&sPiece);
}
else
{
/* use the previous preview piece as current piece */
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
sPiece.size[j] = sPrePiece.size[j];

/* copy the origin of the piece */
sPiece.width = sPrePiece.width;
}

for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
sPrePiece.size[j] = TILE_NODRAW;

sPrePiece.x = MAP_WIDTH+SIDEBAR_WIDTH/4;
sPrePiece.y = SIDEBAR_WIDTH/4;

FillPiece(&sPrePiece);

DrawMap();
}

/* rotate a block */
void MyBlocks::RotateBlock()
{
/* do not allow to rotate the blocks while the game is not running or paused */
if (bGameStarted == FALSE || bGamePaused == TRUE)
{
return;
}

int x, y, temp[4][4];
memset(temp, TILE_NODRAW, sizeof(temp));

/* copy & rotate the piece to the temporary array */
for (y = 0; y < sPiece.width; y++)
{
for (x = 0; x < sPiece.width; x++)
{
#warning remove debug info
printf("sPiece.width: %d\n", sPiece.width);

if (sPiece.width == 3)
{
temp[sPiece.width-1-y][x] = sPiece.size[x][y];
}
else
{
temp[sPiece.width-1-x][y] = sPiece.size[y][x];
}
}
}

/* check collision of the temporary array with map borders */
for (y = 0; y < 4; y++)
{
for (x = 0; x < 4; x++)
{
if (temp[y][x] != TILE_NODRAW)
{
if (sPiece.x + y < 0 || sPiece.x + y > MAP_WIDTH - 1 ||
sPiece.y + x < 0 || sPiece.y + x > MAP_HEIGHT - 1)
{
return;
}
}
}
}

/* check collision of the temporary array with the blocks on the map */
for (x = 0; x < MAP_WIDTH; x++)
{
for (y = 0; y < MAP_HEIGHT; y++)
{
if (x >= sPiece.x && x < sPiece.x + 4)
{
if (y >= sPiece.y && y < sPiece.y +4)
{
if (Map[x][y] != TILE_CLR_FILL_BLACK)
{
if (temp[x - sPiece.x][y - sPiece.y] != TILE_NODRAW)
{
return;
}
}
}
}
}
}

/* successful, now copy the rotated temporary array to the original piece */
for (y = 0; y < 4; y++)
{
for (x = 0; x < 4; x++)
{
sPiece.size[y][x] = temp[y][x];
}
}

DrawMap();

return;
}



If this has helped you, please post it here. Questions are welcome too.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!