Java Reference
In-Depth Information
pongAnimation.playFromStart();
pongComponents.requestFocus();
});
pongComponents = new Group(ball,
topWall,
leftWall,
rightWall,
bottomWall,
leftPaddle,
rightPaddle,
startButton);
pongComponents.setFocusTraversable(true);
pongComponents.setOnKeyPressed(k -> {
if (k.getCode() == KeyCode.SPACE
&& pongAnimation.statusProperty()
.equals(Animation.Status.STOPPED)) {
rightPaddleY.setValue(rightPaddleY.getValue() - 6);
} else if (k.getCode() == KeyCode.L
&& !rightPaddle.getBoundsInParent().intersects(topWall.getBoundsInLocal())) {
rightPaddleY.setValue(rightPaddleY.getValue() - 6);
} else if (k.getCode() == KeyCode.COMMA
&& !rightPaddle.getBoundsInParent().intersects(bottomWall.getBoundsInLocal())) {
rightPaddleY.setValue(rightPaddleY.getValue() + 6);
} else if (k.getCode() == KeyCode.A
&& !leftPaddle.getBoundsInParent().intersects(topWall.getBoundsInLocal())) {
leftPaddleY.setValue(leftPaddleY.getValue() - 6);
} else if (k.getCode() == KeyCode.Z
&& !leftPaddle.getBoundsInParent().intersects(bottomWall.getBoundsInLocal())) {
leftPaddleY.setValue(leftPaddleY.getValue() + 6);
}
});
Scene scene = new Scene(pongComponents, 500, 500);
scene.setFill(Color.GRAY);
ball.centerXProperty().bind(centerX);
ball.centerYProperty().bind(centerY);
leftPaddle.translateYProperty().bind(leftPaddleY);
rightPaddle.translateYProperty().bind(rightPaddleY);
startButton.visibleProperty().bind(startVisible);
stage.setScene(scene);
initialize();
stage.setTitle("ZenPong Example");
stage.show();
}
}
Search WWH ::




Custom Search