Esempio n. 1
0
  /** Controls movements of the falling shapes depending on the key pressed */
  @Override
  public void keyPressed(KeyEvent e) {
    // assign variable to the source
    int whichKey = e.getKeyCode();
    // switch statement
    switch (whichKey) {
        // if right arrow
      case KeyEvent.VK_RIGHT:
        // if shape can move right
        if (game.canMoveRight(grid, game.getCurrentShape()) == true) {
          // move shape right
          game.getCurrentShape()
              .updateShape(
                  game.getCurrentShape().getShape(),
                  game.getCurrentShape().getxCentre() + b_Width,
                  game.getCurrentShape().getyCentre());
        }
        break;
        /// if left arrow
      case KeyEvent.VK_LEFT:
        // if shape can move left
        if (game.canMoveLeft(grid, game.getCurrentShape()) == true) {
          // move shape left
          game.getCurrentShape()
              .updateShape(
                  game.getCurrentShape().getShape(),
                  game.getCurrentShape().getxCentre() - b_Width,
                  game.getCurrentShape().getyCentre());
        }

        break;
        // down arrow
      case KeyEvent.VK_DOWN:
        // while shape can move down
        while (game.canMoveBellow(grid, game.getCurrentShape()) == true) {
          // move shape down
          game.getCurrentShape()
              .updateShape(
                  game.getCurrentShape().getShape(),
                  game.getCurrentShape().getxCentre(),
                  game.getCurrentShape().getyCentre() + b_Width);
        }
        break;

      case KeyEvent.VK_P:
        game.pauseGame(game.getTimer());
        break;
    }

    repaint();
  }