Exemple #1
0
 private void keyReleased(KeyEvent event) {
   if (game.getPlayer().getMovement() == Movement.UP && event.getCode() == KeyCode.UP) {
     game.getPlayer().setMovement(Movement.NONE);
   } else if (game.getPlayer().getMovement() == Movement.DOWN && event.getCode() == KeyCode.DOWN) {
     game.getPlayer().setMovement(Movement.NONE);
   }
 }
Exemple #2
0
 private void keyPressed(KeyEvent event) {
   if (event.getCode() == KeyCode.P) {
     game.pause();
   } else if (event.getCode() == KeyCode.ESCAPE) {
     game.forfeit();
   } else if (game.getPlayer().getMovement() == Paddle.Movement.NONE
       && event.getCode() == KeyCode.UP) {
     game.getPlayer().setMovement(Movement.UP);
   } else if (game.getPlayer().getMovement() == Movement.NONE && event.getCode() == KeyCode.DOWN) {
     game.getPlayer().setMovement(Movement.DOWN);
   }
 }
Exemple #3
0
  public GameScreen(Game game) {
    this.game = game;

    ball.translateXProperty().bind(game.getBall().xProperty());
    ball.translateYProperty().bind(game.getBall().yProperty());
    ball.getStyleClass().add("ball");

    player.translateXProperty().bind(game.getPlayer().xProperty());
    player.translateYProperty().bind(game.getPlayer().yProperty());
    player.getStyleClass().add("paddle");

    opponent.translateXProperty().bind(game.getOpponent().xProperty());
    opponent.translateYProperty().bind(game.getOpponent().yProperty());
    opponent.getStyleClass().add("paddle");

    playerScore.textProperty().bind(game.getPlayer().scoreProperty().asString());
    playerScore
        .boundsInLocalProperty()
        .addListener(
            observable -> {
              /*
               * When using CSS, the width and height (with CSS applied) aren't available right away.
               * Therefore, we listen for changes and update the position once the width and height
               * are available.
               */
              playerScore.setTranslateX(
                  WIDTH / 2 - SCORE_SPACING / 2 - playerScore.getBoundsInLocal().getWidth());
            });
    playerScore.setTranslateY(TEXT_MARGIN_TOP_BOTTOM);
    playerScore.getStyleClass().add("score");

    opponentScore.textProperty().bind(game.getOpponent().scoreProperty().asString());
    opponentScore.setTranslateX(WIDTH / 2 + SCORE_SPACING / 2);
    opponentScore.setTranslateY(TEXT_MARGIN_TOP_BOTTOM);
    opponentScore.getStyleClass().add("score");

    setPrefSize(WIDTH, HEIGHT);
    getChildren().addAll(ball, player, opponent, playerScore, opponentScore);
    getStyleClass().add("screen");

    setOnKeyPressed(this::keyPressed);
    setOnKeyReleased(this::keyReleased);
  }