Need help understanding code for Tetris game..

The code presented in this Utube video: https://www.youtube.com/watch?v=zH_omFPqMO4
also can be downloaded there..

My problem is with these parts,
Inside the 'Rotation', I don't understand that algorithm, what he does:
1
2
3
4
5
6
7
8
        Point p = a[1]; //center of rotation
        for (int i=0;i<4;i++)
          {
            int x = a[i].y-p.y;
            int y = a[i].x-p.x;
            a[i].x = p.x - x;
            a[i].y = p.y + y;
           }


and again with this algorithm inside the 'Tick' part:
1
2
3
4
5
6
         int n=rand()%7;
         for (int i=0;i<4;i++)
           {
            a[i].x = figures[n][i] % 2;
            a[i].y = figures[n][i] / 2;
           }


any one can help me understand this?, all the rest I figured out already..
Last edited on
Rotation: https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions

Rotating 90 degrees counter-clockwise:
x' = 0 * x - 1 * y = -y
y' = 1 * x + 0 * y = x

That rotation is around (0,0).

That code first calculates vector d = a - p,
then rotates d and finally adds p back.
1
2
3
4
5
6
7
8
9
10
11
// d = a - p
int dx = ax - px;
int dy = ay - py;

// r = rotate(d)
int rx = -dy;
int ry =  dx;

// a = r + p
ax = rx + px;
ay = ry + py;

Note that a[1] is the center of rotation. a[1] - a[1] == (0,0)
Therefore, r == d == (0,0) and the new coordinates are:
1
2
a[1].x = a[1].x - 0;
a[1].y = a[1].y + 0;

a[1] does not move. Center of rotation does not move.


The .x coordinate is k%2, so its value is either 0 or 1, depending on the k being even or odd.
The .y coordinate is half of k.
Thanks!


The .x coordinate is k%2, so its value is either 0 or 1, depending on the k being even or odd.
The .y coordinate is half of k.


this I didn't got, you can elaborate more?

why he needs it to be 0 or 1, and why the .y coordinate is half of k? how do you know?
Last edited on
If you look at the figures[7][4] array, you can see that the "S" piece (actually Z), for example, has the second index being 5.
But what point does 5 actually correspond to?
That's where your second code snippet comes into play. It takes a random tetris piece, let's say it picks the "S" piece (index 2). When i = 1, it does
1
2
a[1].x = figures[2][1] % 2;
a[1].y = figures[2][1] / 2;

which is:
1
2
a[1].x = 5 % 2;
a[1].y = 5 / 2;

which is:
1
2
a[1].x = 1;
a[1].y = 2;

i.e. it's the point (x=1, y=2) [second column, third row]

So in summary of the second code snippet: It's translating the indices in figures array into actual x, y coordinates for each square of the given tetris piece.
Last edited on
oh ok, I think I see it now, thanks @keskiverto, and @Ganado
He's coding the initial position of the pieces like this:

   0 1 y
0  0 1
1  2 3
2  4 5
3  6 7
x

The rotation code "simplifies" to this:

1
2
3
a[0].x = a[1].x - (a[0].y - a[1].y);   a[0].y = a[1].y + (a[0].x - a[1].x);
a[2].x = a[1].x - (a[2].y - a[1].y);   a[2].y = a[1].y + (a[2].x - a[1].x);
a[3].x = a[1].x - (a[3].y - a[1].y);   a[3].y = a[1].y + (a[3].x - a[1].x);

If you run that on the initial position of the piece that he calls "Z" (but he seems to have confused Z and S) it goes from this:

  0 1
0 - -
1 X -
2 X X
3 - X

To this:

  -1 0 1
0  - - -
1  - - -
2  - X X
3  X X -

Topic archived. No new replies allowed.