/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package anim; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.beans.property.DoubleProperty; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.util.Duration; /** * * @author asdv */ public class BouncingBall extends Pane { public int pScore = 0; public int cScore = 0; GridPane textDisplay = new GridPane(); Rectangle racket = new Rectangle(); public final double radius = 10; // ball size 40 private double x = radius, y = radius; // coordinates of ball private double dx = 1, dy = 1; // direction of the ball private Circle circle = new Circle(x, y, radius); // her majesty, the ball private Timeline animation; // animate the ball private Label playerScore = new Label("Your Score: 0"); private Label compScore = new Label("Computer Score: 0"); public BouncingBall() { racket.setWidth(70); racket.setHeight(20); racket.relocate(0, 580); textDisplay.add(playerScore, 0, 0); textDisplay.add(compScore, 0, 1); circle.setFill(Color.BROWN); getChildren().addAll(circle, racket, textDisplay); animation = new Timeline( new KeyFrame(Duration.millis(50), (ActionEvent event) -> { moveBall(); if (y >= (getHeight() - 20)) { boolean intersected = intersection(); if (intersected && circle.getCenterX() <= racket.getX() / 2) { System.out.println("Intersected."); dx *= -1; dy *= -1; playerScored(); } else { if (intersected && circle.getCenterX() >= racket.getX() / 2) { playerScored(); dy *= -1; } else { if (y >= (getHeight() - 40)) { computerScored(); dy *= -1; } } } } })); animation.setCycleCount(Timeline.INDEFINITE); animation.setRate(animation.getRate() * 20); animation.play(); } private void moveBall() { if (x < radius || x > getWidth() - radius) { dx *= -1; } if (y < radius || y > getHeight() - radius) { dy *= -1; } x += dx; y += dy; circle.setCenterX(x); circle.setCenterY(y); } public void playerScored() { playerScore.setText("Your Score: " + (++this.pScore)); } public void computerScored() { compScore.setText("Computer Score: " + (++this.cScore)); } public boolean intersection() { boolean intersected = racket.getBoundsInParent().intersects(circle.getBoundsInParent()); if (intersected) { return true; } else { return false; } } public void moveRacket(double x) { racket.relocate(x, 580); } public void play() { animation.play(); } public void pause() { animation.pause(); } public void increaseSpeed() { animation.setRate(animation.getRate() * 1.5); } public void decreaseSpeed() { animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0); } public DoubleProperty rateProperty() { return animation.rateProperty(); } }