0
\$\begingroup\$

I am currently at the last stage of the Tetris Clone using SDL library, the only thing left is rotation. For each piece I have a separate class, and in order to move the pieces down I use a method:

void PieceZ::movePieceDown()
{
  drawBlock(x1,y1++);
  drawBlock(x2,y3++);
  drawBlock(x3,y3++);
  drawBlock(x4,y4++);
}

This method is called like that: current->movePieceDown(); where current represents a pointer to the current piece. Now to rotate the piece I have another method:

void PieceZ::rotatePiece()
{
  drawBlock(newX1,newY2++);
  drawBlock(x2,y2);
  drawBlock(newX3,newY3++);
  drawBlock(newX4,newY4++);
}

New X and Y coordinates are calculated elsewhere, with x2 and y2 taken as the origin. My question is how do I swap current->movePieceDown() with current->rotatePiece() while the piece is moving? Taking into acount that I am handling keyboard events in another method.

\$\endgroup\$
0
\$\begingroup\$

I found a solution, I am posting it just in case. Basically I removed the method rotatePiece(), instead of that I have a method that calculates the rotation independently of drawing:

void PieceZ::getRotation()
{
  newX1 = (y1-y2) + x2;
  newY1 = (x2-x1) + y2;

  newX3 = (y3-y2) + x2;
  newY3 = (x2-x3) + y2;

  newX4 = (y4-y2) + x2;
  newY4 = (x2-x4) + y2;

  x1 = newX1;
  x3 = newX3;
  x4 = newX4;

  y1 = newY1;
  y3 = newY3;
  y4 = newY4;
}

new values are set to 0 initially, and every time a user presses a button on the keyboard this method gets called, it calculates the new rotation in new and assigns it to x and y values. Which gives you a 90 degree counter clockwise rotation.

\$\endgroup\$
0
\$\begingroup\$

Alternatively, you could store four different pre-defined rotations and swap amongst them.

\$\endgroup\$
  • \$\begingroup\$ That's true. But that would envolve having hard coded arrays instead of coordinates. \$\endgroup\$ – user82301 May 12 '16 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy