Public Group

#### Archived

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

# Erm tetris question

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

## 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 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 on other sites
I hard coded mine, It was logically the easy way to do it (physically it was a bit of a pain).

You can see exactly how I did it here.

Edit: bleh, HTML.

[edited by - tiffany_smith on October 22, 2003 4:27:21 PM]

##### Share on other sites
Just transpose the matrix.

##### 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 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

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

##### 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 on other sites
edit: That was my boss--don't mind him--he's a dork.

[edited by - neurokaotix on October 22, 2003 5:10:23 PM]

Umm, oookaaaay.

##### 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.

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633657
• Total Posts
3013203
×