Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bouncingball2;
- import java.util.Random;
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Label;
- import javafx.scene.input.MouseButton;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- public class BouncingBall2 extends Application {
- private Random rand = new Random();
- private Rectangle rect;
- private Circle circle;
- private Label label;
- private Label count;
- private int eats;
- private int incX = rand.nextInt(3) + 1, incY = rand.nextInt(2) + 1;
- @Override
- public void start(Stage stage) throws Exception {
- Group root = new Group();
- circle = new Circle(200, 200, 25);
- circle.setFill(Color.RED);
- rect = new Rectangle(rand.nextInt(395), rand.nextInt(395), 70, 70);
- label = new Label("Eats: ");
- count = new Label("0");
- count.setTranslateX(32);
- rect.setFill(Color.BLUE);
- root.getChildren().add(label);
- root.getChildren().add(count);
- root.getChildren().add(rect);
- root.getChildren().add(circle);
- Scene scene = new Scene(root, 400, 400);
- stage.setScene(scene);
- stage.show();
- EventHandler<ActionEvent> eventHandler = e -> {
- if (circle.getCenterX() > 400 || circle.getCenterX() < 0) {
- incX *= -1;
- }
- if (circle.getCenterY() > 400 || circle.getCenterY() < 0) {
- incY *= -1;
- }
- circle.setCenterX(circle.getCenterX() + incX);
- circle.setCenterY(circle.getCenterY() + incY);
- if (circle.getCenterX() >= rect.getX()-21.5 && circle.getCenterX() <= rect.getX() + 91.5 &&
- circle.getCenterY() >= rect.getY()-21.5 && circle.getCenterY() <= rect.getY() + 91.5){
- eats++;
- count.setText(eats + "");
- circle.setCenterX(rand.nextInt(390));
- circle.setCenterY(rand.nextInt(390));
- }
- };
- rect.setOnKeyPressed(e -> {
- switch (e.getCode()) {
- case DOWN:
- if (rect.getY() < 300) {
- rect.setY(rect.getY() + 10);
- }
- break;
- case UP:
- if (rect.getY() > 0) {
- rect.setY(rect.getY() - 10);
- }
- break;
- case LEFT:
- if (rect.getX() > 0) {
- rect.setX(rect.getX() - 10);
- }
- break;
- case RIGHT:
- if (rect.getX() < 250) {
- rect.setX(rect.getX() + 10);
- }
- break;
- }
- });
- rect.requestFocus();
- Timeline animation = new Timeline(new KeyFrame(Duration.millis(19), eventHandler));
- animation.setCycleCount(Timeline.INDEFINITE);
- animation.play();
- circle.setOnMousePressed(e->{
- if (e.getButton().equals(MouseButton.PRIMARY)){
- if (animation.getStatus().equals(animation.getStatus().RUNNING))
- animation.pause();
- else
- animation.play();
- }
- else
- animation.setRate(animation.getRate() + 1);
- });
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement