Class TetrisGame


  • public class TetrisGame
    extends java.lang.Object
    Stores and controls a game's state, manages its components.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Board board
      The game's board which holds the tetromino parts.
      protected Tetromino fallingTetromino
      The game's falling tetromino, the player can control it, but it'll move down automatically too.
      private GameFrame gameFrame
      The frame of this game, manages its layout, user events.
      private PeriodicTask gravity
      A timer, in each period the tetromino will be moved down by one.
      protected boolean isRunning
      Game's state: true when the tetromino is falling, false if game is over.
    • Constructor Summary

      Constructors 
      Constructor Description
      TetrisGame​(Dimension size, GameFrame gameFrame, int fallingSpeed)
      Initalizes a new Tetris game.
    • Method Summary

      Modifier and Type Method Description
      private boolean generateNextTetromino()
      Creates a new tetromino that will fall down next time.
      protected int getNextTetrominoType()
      Generates a number (type) for the next tetromino.
      void handleCommand​(Command command)
      Handles an user command when the player pressed an input button.
      private boolean moveTetrominoDown()  
      private boolean moveTetrominoLeft()  
      private boolean moveTetrominoRight()  
      private boolean rotateTetromino()  
      void start()
      Starts the game: a new tetromino is created, and it starts falling.
      private void stop()
      Called when game is over, disables everything.
      private void tetrominoLanded()
      Called when the tetromino can't be moved down any more (ei. it fell down).
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • isRunning

        protected boolean isRunning
        Game's state: true when the tetromino is falling, false if game is over.
      • board

        protected final Board board
        The game's board which holds the tetromino parts.
      • fallingTetromino

        protected Tetromino fallingTetromino
        The game's falling tetromino, the player can control it, but it'll move down automatically too.
      • gravity

        private final PeriodicTask gravity
        A timer, in each period the tetromino will be moved down by one.
      • gameFrame

        private final GameFrame gameFrame
        The frame of this game, manages its layout, user events.
    • Constructor Detail

      • TetrisGame

        public TetrisGame​(Dimension size,
                          GameFrame gameFrame,
                          int fallingSpeed)
        Initalizes a new Tetris game.
        Parameters:
        size - The board's size (rows/columns).
        gameFrame - This game's display area.
        fallingSpeed - After this much time, the tetromino will automatically move down.
    • Method Detail

      • start

        public void start()
        Starts the game: a new tetromino is created, and it starts falling.
      • stop

        private void stop()
        Called when game is over, disables everything.
      • handleCommand

        public void handleCommand​(Command command)
        Handles an user command when the player pressed an input button. It controls the falling tetromino.
        Parameters:
        command - The command that the player caused.
      • tetrominoLanded

        private void tetrominoLanded()
        Called when the tetromino can't be moved down any more (ei. it fell down). Adds the tetromino's parts to the board, removes full rows, generates next tetromino.
      • generateNextTetromino

        private boolean generateNextTetromino()
        Creates a new tetromino that will fall down next time.
        Returns:
        True if it could be moved to its initial position, false otherwise.
      • getNextTetrominoType

        protected int getNextTetrominoType()
        Generates a number (type) for the next tetromino.
        Returns:
        A randomly choosed tetromino type.
      • moveTetrominoLeft

        private boolean moveTetrominoLeft()
      • moveTetrominoDown

        private boolean moveTetrominoDown()
      • moveTetrominoRight

        private boolean moveTetrominoRight()
      • rotateTetromino

        private boolean rotateTetromino()