Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Tetris - Thinking about piece rotation [closed]

Tags:

java

swing

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?

enter image description here

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?

like image 479
user3871 Avatar asked Apr 03 '13 14:04

user3871


1 Answers

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.

like image 133
COME FROM Avatar answered Oct 14 '22 03:10

COME FROM