Archived

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

ErikGroth

first-game-questions (tetris)

Recommended Posts

Hello! I''m trying to program my first game, it''s a tetris clone. My question is about rotating the blocks. I''m programming every block as four individual squares. The problem is that I don''t really know how to rotate the block. So if someone has a smart and simple idea on how to do that, I would really appreciate if you tried to explain it to me. Thanks /Erik

Share this post


Link to post
Share on other sites
Erik,

Here is how I did it. The area where the pieces fall is a matrix of numbers (ie. 1 means there is a square there and 0 means there isn't). I defined a struct as follows:

struct Piece {
int * axis;
int square1[2];
int square2[2];
int square3[2];
};

This struct keeps track of one square of each piece (called the axis). The axis points to somewhere in the matrix that represents the screen (this is the matrix that I mentioned just a moment ago). Square1, square2, and square3 are the other 3 squares of the piece. They are int arrays of length 2 that store how far in the x direction and how far in the y direction each square is from the axis. Confused yet? Here's an example. Think of this piece:

+
+++ (I call it the T)
^ This is the axis square.

So the other squares are {0,-1}, {-1,0}, and {1,0} because they are either on the same x or y line (the 0s) or are one space away (the 1s and -1s). Each square is defined like this { (this square's x location - axis's x location) , (this square's y location - axis's y location) }.

The benefit of this data structure is that when you want to rotate a piece, all you have to do is switch the x and y of each square (checking for whether it is positive or negative). For instance when we rotate the T piece shown above to the right (clockwise), the {0,-1} square becomes {1,0}, the {-1,0} square becomes {0,-1} and the {1,0} square becomes {0,1}. Then, when you want to draw the piece, you just look to wherever the axis is pointing in the matrix, draw it, then move to the location of each square relative to the axis and draw them.

I hope this wasn't too confusing.

FragLegs


Edited by - FragLegs on July 23, 2001 4:32:01 PM

Share this post


Link to post
Share on other sites