Archived

This topic is now archived and is closed to further replies.

NullPointer33

Erm tetris question

Recommended Posts

Hello, I''m making a tetris game that is almost complete mostly everything works collisions, game logic etc, the problem I have is I don''t know how to rotate the pieces I have them in arrays like for example
int L[5][5] =
{
	-1, -1, 0, -1, -1,
	-1, -1, 0, -1, -1,
	-1, -1, 0, 0, -1,
	-1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1,
};
 
...then I have another BIG array that represents the screen to where the pieces are drawn (I copy the information of the pieces there also) which looks like this int board[MAX_W][MAX_H]; but I can''t figure out how to rotate the pieces, thanks in advance for the help.

Share this post


Link to post
Share on other sites
I used a big 2d array to represent everything. Each piece had a number, such as:

No Block - 0
Wall - -1
LBlock - 1
ReverseLBlock - 2
SquareBlock - 3
NBlock - 4
ReverseNBlock - 5
IBlock - 6
TBlock - 7

Whenever a block takes up positions on the board, I just set those positions to the appropriate number. When the block moves and isn''t in that position anymore, I set it back to the no block state. When the block comes to a permanent stand still, the position is set that way permanently (at least until the row disappears). It makes it easy to check for collision, and to check if a row is complete. To render, all you have to do is loop through the 2d array every frame, and draw the correct color corresponding to the correct block.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Just transpose the matrix.


I believe that would leave the object upside down and backwards (giving only two states of rotation BTW).

xgOblin - you forgot to explain rotation


int M[5][5];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
M[i][j] = L[i][j]; // Copy the array

}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
L[j][4-i] = M[i][j];
}
}


That's more of a guess than anything, but I think it's right. If it rotates the wrong way swap the "4-" part between dimensions.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________


[edited by - Thunder_Hawk on October 22, 2003 4:39:33 PM]

Share this post


Link to post
Share on other sites
Thanks for the replies, ok what you guys suggested is pretty much what I did and it's working correctly I can move the piece left and right, I'm also checking collisions using the array like xg0blin said and that's working too. What I can't figure out is how to determine the position when the piece is rotated and then replace the values in the big array with the appropiate values.

quote:

I believe that would leave the object upside down and backwards



Yes I had that problem before too already fixed it

[edited by - NullPointer33 on October 22, 2003 4:48:14 PM]

Share this post


Link to post
Share on other sites
Record the position of say, the upper left hand corner of the current block in some external variables and use that position (two variables) as an offset into the giant array.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
I define each block with a center, and a state. Some blocks have four states, some two, and some only one. The state of course specifies the direction that the block is facing (UP, DOWN, LEFT, RIGHT). Based upon the state and the position is how you check your movement and collision. For instance, If the center of my I block is at index [17][12], and the state is sideways (it only has VERTICAL, and HORIZONTAL), then I set my array indicies based on that information.

Share this post


Link to post
Share on other sites
If you go element by element, it's pretty easy to figure out how to make a rotated version of an array, especially if you write out what you need to do on paper.

If I were writing Tetris right now, I would have a current block object, that had randomly been assigned a block configuration from the 7? "base type" blocks. To rotate, call CurrentBlock.rotate(DIRECTION). No real need to hardcode all the block type ROTATIONS. Makes it easier if you want to put some spin on your game and make new block shapes too.

I don't know how relevant this is, but whatever

[edited by - Peon on October 22, 2003 5:07:16 PM]

Share this post


Link to post
Share on other sites
quote:

If you go element by element, it''s pretty easy to figure out how to make a rotated version of an array, especially if you write out what you need to do on paper.

If I were writing Tetris right now, I would have a current block object, that had randomly been assigned a block configuration from the 7? "base type" blocks. To rotate, call CurrentBlock.rotate(DIRECTION). No real need to hardcode all the block type ROTATIONS. Makes it easier if you want to put some spin on your game and make new block shapes too.

I don''t know how relevant this is, but whatever



I did this approach on the first version I did. Of course, every time I wanted to do a rotation, I had to switch from my board coordinates, to the block coordinates, do the rotation, and then switch back to the board coordinates. I thought it was a whole lot cleaner to just do it all with the one big array, with indexes moving about.

Share this post


Link to post
Share on other sites