I'm building Tetris and trying to think about how I should rotate pieces.
Are they rotating on a single block? Or should they morph... aka swap positions?
The way I'm thinking of doing it is sort of hardcode... like (pseudocode):
if (rotateRight()) {
if (Piece == "T") {
if (Piece.getCurrRotation() == down (aka.. 180 degrees))
move each Tile in Piece from the down rotation to the left rotation... each coordinate would be pre-specified for a given condition... aka lot's of hardcoding
}
if (Piece == "L") { etc...
}
}
if (rotateLeft()) {
perform same checks for the Left...
}
But this would seem to be a massssssive amount of code just to figure out
firstly) which rotation the current piece is at (there's 4 possible rotations for each piece)
secondly) From there... set it to the new hardcoded coordinates based on that piece
I'd have to do that for each piece... that seems like the wrong way to think about it.
Any other thoughts?
I would probably have 1-4 Orientation objects for each PieceType object. Each Orientation would then define positions of actual blocks (relative to some pivot). For instance
PieceType "L":
Orientation 1:
#
#
##
Orientation 2:
###
#
Orientation 3:
##
#
#
Orientation 4:
#
###
PieceType "I":
Orientation 1:
#
#
#
#
Orientation 2:
####
Each PieceType could also hold information about the space needed for each possible "change of Orientation" (i.e. rotation). This is all static information, so there's really no need to move blocks around during the game. Just change the Orientation of the Piece and let the Orientation object tell you the block positions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With