|
| 1 | +import javafx.animation.TranslateTransition; |
| 2 | +import javafx.application.Application; |
| 3 | +import javafx.scene.Scene; |
| 4 | +import javafx.scene.control.Label; |
| 5 | +import javafx.scene.layout.Pane; |
| 6 | +import javafx.scene.paint.Color; |
| 7 | +import javafx.scene.shape.Circle; |
| 8 | +import javafx.stage.Stage; |
| 9 | +import javafx.util.Duration; |
| 10 | + |
| 11 | +import java.util.Random; |
| 12 | + |
| 13 | +public class EscapeTheCursorGame extends Application { |
| 14 | + private Circle ball; |
| 15 | + private Random random = new Random(); |
| 16 | + private final int screenWidth = 800; |
| 17 | + private final int screenHeight = 600; |
| 18 | + private boolean isMoving = false; |
| 19 | + private int score = 0; |
| 20 | + private Label scoreLabel; |
| 21 | + private Label message; |
| 22 | + |
| 23 | + @Override |
| 24 | + public void start(Stage primaryStage) { |
| 25 | + Pane root = new Pane(); |
| 26 | + |
| 27 | + message = new Label("You'll never catch me! Try to click on me once LOL"); |
| 28 | + message.setStyle("-fx-font-size: 24; -fx-text-fill: red;"); |
| 29 | + message.setLayoutX((screenWidth - message.getWidth()) / 4 - 55); |
| 30 | + message.setLayoutY(screenHeight - message.getHeight() - 30); |
| 31 | + root.getChildren().add(message); |
| 32 | + |
| 33 | + ball = createBall(); |
| 34 | + root.getChildren().add(ball); |
| 35 | + |
| 36 | + scoreLabel = new Label("Score: 0"); |
| 37 | + scoreLabel.setStyle("-fx-font-size: 16; -fx-text-fill: red;"); |
| 38 | + root.getChildren().add(scoreLabel); |
| 39 | + |
| 40 | + Scene scene = new Scene(root, screenWidth, screenHeight); |
| 41 | + scene.setOnMouseMoved(event -> moveBall(event.getSceneX(), event.getSceneY())); |
| 42 | + scene.setOnMouseClicked(event -> handleClick(event.getSceneX(), event.getSceneY())); |
| 43 | + |
| 44 | + primaryStage.setTitle("Escape the Cursor Game"); |
| 45 | + primaryStage.setScene(scene); |
| 46 | + primaryStage.show(); |
| 47 | + } |
| 48 | + |
| 49 | + private Circle createBall() { |
| 50 | + Circle circle = new Circle(20, Color.BLUE); |
| 51 | + circle.relocate(random.nextInt(screenWidth - (int) circle.getRadius() * 2) + circle.getRadius(), |
| 52 | + random.nextInt(screenHeight - (int) circle.getRadius() * 2) + circle.getRadius()); |
| 53 | + return circle; |
| 54 | + } |
| 55 | + |
| 56 | + private void moveBall(double mouseX, double mouseY) { |
| 57 | + if (isMoving) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + double distanceX = Math.abs(ball.getLayoutX() + ball.getRadius() - mouseX); |
| 62 | + double distanceY = Math.abs(ball.getLayoutY() + ball.getRadius() - mouseY); |
| 63 | + |
| 64 | + if (distanceX < 50 && distanceY < 50) { |
| 65 | + isMoving = true; |
| 66 | + double newX = random.nextInt(screenWidth - (int) ball.getRadius() * 2) + ball.getRadius(); |
| 67 | + double newY = random.nextInt(screenHeight - (int) ball.getRadius() * 2) + ball.getRadius(); |
| 68 | + |
| 69 | + TranslateTransition transition = new TranslateTransition(Duration.millis(200), ball); |
| 70 | + transition.setFromX(ball.getTranslateX()); |
| 71 | + transition.setFromY(ball.getTranslateY()); |
| 72 | + transition.setToX(newX - ball.getLayoutX()); |
| 73 | + transition.setToY(newY - ball.getLayoutY()); |
| 74 | + transition.setOnFinished(event -> { |
| 75 | + ball.setLayoutX(newX); |
| 76 | + ball.setLayoutY(newY); |
| 77 | + ball.setTranslateX(0); |
| 78 | + ball.setTranslateY(0); |
| 79 | + isMoving = false; |
| 80 | + }); |
| 81 | + transition.play(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void handleClick(double mouseX, double mouseY) { |
| 86 | + double distanceX = Math.abs(ball.getLayoutX() + ball.getRadius() - mouseX); |
| 87 | + double distanceY = Math.abs(ball.getLayoutY() + ball.getRadius() - mouseY); |
| 88 | + |
| 89 | + if (distanceX < 20 && distanceY < 20) { |
| 90 | + score++; |
| 91 | + scoreLabel.setText("Score: " + score); |
| 92 | + ball.setLayoutX(random.nextInt(screenWidth - (int) ball.getRadius() * 2) + ball.getRadius()); |
| 93 | + ball.setLayoutY(random.nextInt(screenHeight - (int) ball.getRadius() * 2) + ball.getRadius()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static void main(String[] args) { |
| 98 | + launch(args); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments