MP2 is pog
This commit is contained in:
@@ -17,8 +17,12 @@ import javafx.stage.Stage;
|
||||
*/
|
||||
public class BallControl extends Application {
|
||||
|
||||
private boolean mouseFocus = false;
|
||||
private int racketPosition = 0;
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
public void start(Stage primaryStage) throws Exception
|
||||
{
|
||||
|
||||
BouncingBall bouncingBall = new BouncingBall();
|
||||
// Create a scene and place it in the stage
|
||||
@@ -27,19 +31,22 @@ public class BallControl extends Application {
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
bouncingBall.requestFocus();
|
||||
bouncingBall.showInfoLabel();
|
||||
|
||||
bouncingBall.setOnMouseEntered(e
|
||||
-> {
|
||||
mouseFocus = true;
|
||||
bouncingBall.hideInfoLabel();
|
||||
bouncingBall.play();
|
||||
});
|
||||
|
||||
bouncingBall.setOnMouseExited(e -> {
|
||||
bouncingBall.showInfoLabel();
|
||||
bouncingBall.pause();
|
||||
mouseFocus = false;
|
||||
});
|
||||
bouncingBall.setOnMouseMoved(new EventHandler<MouseEvent>() {
|
||||
@Override
|
||||
public void handle(MouseEvent event) {
|
||||
public void handle(MouseEvent event)
|
||||
{
|
||||
double mouseX = event.getSceneX() - (bouncingBall.RACKET_LENGTH / 2); // set relative to center of racket
|
||||
bouncingBall.moveRacket(mouseX);
|
||||
}
|
||||
@@ -49,6 +56,20 @@ public class BallControl extends Application {
|
||||
// Increase and decrease animation
|
||||
bouncingBall.setOnKeyPressed(e
|
||||
-> {
|
||||
if (e.getCode() == KeyCode.LEFT && mouseFocus == false) {
|
||||
bouncingBall.hideInfoLabel();
|
||||
if (racketPosition > 0) {
|
||||
racketPosition = racketPosition - 30;
|
||||
}
|
||||
bouncingBall.moveRacket(racketPosition);
|
||||
}
|
||||
if (e.getCode() == KeyCode.RIGHT && mouseFocus == false) {
|
||||
bouncingBall.hideInfoLabel();
|
||||
if (racketPosition < bouncingBall.getWidth()) {
|
||||
racketPosition = racketPosition + 30;
|
||||
}
|
||||
bouncingBall.moveRacket(racketPosition);
|
||||
}
|
||||
if (e.getCode() == KeyCode.UP) {
|
||||
bouncingBall.increaseSpeed();
|
||||
} else if (e.getCode() == KeyCode.DOWN) {
|
||||
@@ -59,7 +80,8 @@ public class BallControl extends Application {
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ import javafx.util.Duration;
|
||||
public class BouncingBall extends Pane {
|
||||
|
||||
GridPane textPane = new GridPane();
|
||||
private final int COLLISION_COOLDOWN = 20;
|
||||
private long timeSinceLastCollisionEvent = 0;
|
||||
final double RACKET_LENGTH = 100;
|
||||
final double radius = 20;
|
||||
@@ -41,33 +42,43 @@ public class BouncingBall extends Pane {
|
||||
private Label playerScoreLabel = new Label("Player Score: 0");
|
||||
private Label computerScoreLabel = new Label("Computer Score: 0");
|
||||
|
||||
|
||||
|
||||
private Timeline animation;
|
||||
|
||||
private int computerScore, playerScore = 0;
|
||||
|
||||
public BouncingBall() {
|
||||
public BouncingBall()
|
||||
{
|
||||
circle.setFill(Color.RED); // Set ball color
|
||||
racket.setFill(Color.BLUE);
|
||||
textPane.add(racketTime, 0, 0);
|
||||
textPane.add(ballCords, 0, 1);
|
||||
textPane.add(playerScoreLabel, 0, 2);
|
||||
textPane.add(computerScoreLabel, 0, 3);
|
||||
getChildren().addAll(circle, racket, textPane, infoLabel); // Place a ball into this pane
|
||||
getChildren().addAll(circle, racket, textPane); // Place a ball into this pane
|
||||
racket.relocate(0, 580);
|
||||
infoLabel.relocate(getHeight() / 4.0, getWidth() / 2.0);
|
||||
// Create an animation for moving the ball
|
||||
animation = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
|
||||
@Override
|
||||
public void handle(ActionEvent t) {
|
||||
public void handle(ActionEvent t)
|
||||
{
|
||||
|
||||
timeSinceLastCollisionEvent++;
|
||||
racketTime.setText("Frames since last collision: " + timeSinceLastCollisionEvent);
|
||||
moveBall();
|
||||
if (y >= (getHeight() - 20) && (timeSinceLastCollisionEvent > 500)) {
|
||||
// Process collisions
|
||||
if (y >= (getHeight() - 20) && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) {
|
||||
timeSinceLastCollisionEvent = 0;
|
||||
playSound("collision");
|
||||
incrementComputerScore();
|
||||
}
|
||||
if ((x <= 20 && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) || ((x >= getWidth() - 20) && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN))) {
|
||||
timeSinceLastCollisionEvent = 0;
|
||||
playSound("collision");
|
||||
}
|
||||
if (y <= 20 && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) {
|
||||
timeSinceLastCollisionEvent = 0;
|
||||
playSound("collision");
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
@@ -76,20 +87,23 @@ public class BouncingBall extends Pane {
|
||||
|
||||
}
|
||||
|
||||
private boolean processRacketCollision() {
|
||||
private boolean processRacketCollision()
|
||||
{
|
||||
boolean racketCollision = racket.getBoundsInParent().intersects(circle.getBoundsInParent());
|
||||
|
||||
if (racketCollision && (timeSinceLastCollisionEvent > 500)) { // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.
|
||||
if (racketCollision && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) { // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.
|
||||
System.out.println("Racket collision detected!");
|
||||
timeSinceLastCollisionEvent = 0;
|
||||
incrementPlayerScore();
|
||||
playSound("racket");
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void moveBall() {
|
||||
private void moveBall()
|
||||
{
|
||||
|
||||
// Check boundaries
|
||||
if (x < radius || x > getWidth() - radius) {
|
||||
@@ -107,33 +121,40 @@ public class BouncingBall extends Pane {
|
||||
ballCoordsToLabel();
|
||||
}
|
||||
|
||||
public void play() {
|
||||
public void play()
|
||||
{
|
||||
animation.play();
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
public void pause()
|
||||
{
|
||||
animation.pause();
|
||||
}
|
||||
|
||||
public void increaseSpeed() {
|
||||
public void increaseSpeed()
|
||||
{
|
||||
animation.setRate(animation.getRate() * 1.5);
|
||||
System.out.println(animation.getRate());
|
||||
}
|
||||
|
||||
public void decreaseSpeed() {
|
||||
public void decreaseSpeed()
|
||||
{
|
||||
animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0);
|
||||
System.out.println(animation.getRate());
|
||||
}
|
||||
|
||||
public DoubleProperty rateProperty() {
|
||||
public DoubleProperty rateProperty()
|
||||
{
|
||||
return animation.rateProperty();
|
||||
}
|
||||
|
||||
public void moveRacket(double x) {
|
||||
public void moveRacket(double x)
|
||||
{
|
||||
racket.relocate(x, 580);
|
||||
}
|
||||
|
||||
public void showInfoLabel() {
|
||||
public void showInfoLabel()
|
||||
{
|
||||
double paneHeight = getHeight();
|
||||
double paneWidth = getWidth();
|
||||
getChildren().add(infoLabel);
|
||||
@@ -141,34 +162,58 @@ public class BouncingBall extends Pane {
|
||||
infoLabel.relocate(paneWidth / 4.0, paneHeight / 2.0);
|
||||
}
|
||||
|
||||
public void hideInfoLabel() {
|
||||
public void hideInfoLabel()
|
||||
{
|
||||
getChildren().remove(infoLabel);
|
||||
}
|
||||
|
||||
private void incrementPlayerScore() {
|
||||
private void incrementPlayerScore()
|
||||
{
|
||||
playerScoreLabel.setText("Player score: " + ++playerScore);
|
||||
playSound();
|
||||
}
|
||||
|
||||
private void incrementComputerScore() {
|
||||
private void incrementComputerScore()
|
||||
{
|
||||
computerScoreLabel.setText("Computer score: " + ++computerScore);
|
||||
//playSound();
|
||||
|
||||
}
|
||||
|
||||
private void ballCoordsToLabel() {
|
||||
private void ballCoordsToLabel()
|
||||
{
|
||||
ballCords.setText("Ball coords: " + x + ", " + y);
|
||||
}
|
||||
private void playSound() {
|
||||
|
||||
private void playSound(String sampleType)
|
||||
{
|
||||
String sample = "";
|
||||
int randInt = (int) (Math.random() * 2);
|
||||
String sample;
|
||||
if (randInt == 0) {
|
||||
sample = "5";
|
||||
} else {
|
||||
sample = "7";
|
||||
switch (sampleType) {
|
||||
case "collision":
|
||||
switch (randInt) {
|
||||
case 0:
|
||||
sample = "05";
|
||||
break;
|
||||
case 1:
|
||||
sample = "07";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "racket":
|
||||
switch (randInt) {
|
||||
case 0:
|
||||
sample = "10";
|
||||
break;
|
||||
case 1:
|
||||
sample = "12";
|
||||
break;
|
||||
}
|
||||
}
|
||||
Media sound = new Media(new File("sound/Sample_000"+sample+".wav").toURI().toString());
|
||||
|
||||
Media sound = new Media(new File("sound/Sample_00" + sample + ".wav").toURI().toString());
|
||||
MediaPlayer mediaPlayer = new MediaPlayer(sound);
|
||||
|
||||
mediaPlayer.setStartTime(Duration.ZERO);
|
||||
|
||||
mediaPlayer.play();
|
||||
//mediaPlayer.setOnEndOfMedia();
|
||||
}
|
||||
|
Reference in New Issue
Block a user