I am doing the Java Fx program. There are some requirements that I have trouble
ID: 3833230 • Letter: I
Question
I am doing the Java Fx program. There are some requirements that I have trouble with it. First, the bottom of the paddle should be about 1/2 the diameter of the ball off the bottom. If the ball connects with the paddle, then it bounces at a 90-degree angle back into the pane space. I also need a label that displays the score and If the ball misses the paddle, then the score is decremented by 1. For every (2?) paddle misses in a row, the paddle grows in length. Finally, The game ends when 20 points are lost.
Here is my code in JavaFx I am working on:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class PongforOne extends Application
{
int sceneWidth = 500;
int sceneHeight = 500;
@Override
public void start(Stage primaryStage)
{
BallPane ballPane = new BallPane();
Scene scene = new Scene(ballPane, 500, 500);
primaryStage.setTitle("Pong for One"); // Set the stage title
primaryStage.setScene(scene);
primaryStage.show();
ballPane.requestFocus();
}
public class BallPane extends Pane
{
public final double radius = 20;
private double x = radius, y = radius;
private double dx = 1, dy = 1;
private final Circle circle = new Circle(x, y, radius);
private final Timeline animation;
// Pane pane = new Pane();
public int lossScore = 20;
public int hitStreak = 0;
public int lossStreak = 0;
private double length = 140;
private final double width = 30;
public double paddleX = sceneWidth / 2 - length / 2;
public final double paddleY = sceneHeight - width;
public Rectangle paddle = new Rectangle(paddleX, paddleY, length, width);
public BallPane()
{
circle.setFill(Color.GREEN); // Set ball color
getChildren().add(circle); // Place a ball into this pane
paddle.setFill(Color.RED);
paddle.setStroke(Color.RED);
paddle.setOnMouseDragged(e ->
{
paddle.setX(e.getX());
}
);
getChildren().add(paddle);
// Create an animation for moving the ball
animation = new Timeline(
new KeyFrame(Duration.millis(5), e -> moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); // Start animation
}
public void play()
{
animation.play();
}
public void pause()
{
animation.pause();
}
public void increaseSpeed()
{
animation.setRate(animation.getRate() + 0.1);
}
public void decreaseSpeed() {
animation.setRate(
animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
}
public DoubleProperty rateProperty()
{
return animation.rateProperty();
}
public void increasePaddleLength()
{
paddle.setWidth(100 * 2);
}
protected void moveBall()
{
double circX = circle.getCenterX();
double circY = circle.getCenterY() + radius;
double padY = paddle.getY();
double padX = paddle.getX();
Text text = new Text();
if (x < radius || x > sceneWidth - radius)
{
dx *= -1;
}
if (y < radius || y > sceneHeight - radius)
{
dy *= -1;
}
if (y > sceneHeight - radius)
{
--lossScore;
System.out.println(lossScore);
x = (int) (radius + Math.random() * 300);
y = radius;
circle.setCenterX(x);
circle.setCenterY(y);
++lossStreak;
hitStreak = 0;
setDifficulty(hitStreak, lossStreak);
}
if (circY == padY && (circX >= padX && circX <= (padX + length)))
{
lossStreak = 0;
++hitStreak;
setDifficulty(hitStreak, lossStreak);
dy *= -1;
if (circX > sceneWidth / 2)
{
x -= sceneWidth / 4;
}
else
{
x += sceneWidth / 4;
}
}
x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
}
public void setDifficulty(int hitStreak, int lossStreak)
{
switch (hitStreak)
{
case 10:
circle.setFill(Color.ORANGE);
increaseSpeed();
break;
case 20:
circle.setFill(Color.BLUE);
increaseSpeed();
break;
case 30:
circle.setFill(Color.YELLOW);
increaseSpeed();
}
switch (lossStreak)
{
case 5:
paddle.setWidth(length += 40);
decreaseSpeed();
break;
case 7:
paddle.setWidth(length += 40);
decreaseSpeed();
break;
}
}
public void dragMouse()
{
paddle.setOnMouseDragged(e ->
{
paddle.setX(e.getX());
if (paddle.getX() <= 0)
{
paddle.setX(0);
}
if (paddle.getX() + length >= sceneWidth)
{
paddle.setX(sceneWidth - length);
}
});
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Explanation / Answer
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class PongforOne extends Application
{
int sceneWidth = 500;
int sceneHeight = 500;
@Override
public void start(Stage primaryStage)
{
BallPane ballPane = new BallPane();
Scene scene = new Scene(ballPane, 500, 500);
primaryStage.setTitle("Pong for One"); // Set the stage title
primaryStage.setScene(scene);
primaryStage.show();
ballPane.requestFocus();
}
public class BallPane extends Pane
{
public final double radius = 20;
private double x = radius, y = radius;
private double dx = 1, dy = 1;
private final Circle circle = new Circle(x, y, radius);
private final Timeline animation;
// Pane pane = new Pane();
public int lossScore = 20;
public int hitStreak = 0;
public int lossStreak = 0;
private double length = 140;
private final double width = 30;
public double paddleX = sceneWidth / 2 - length / 2;
public final double paddleY = sceneHeight - width;
public Rectangle paddle = new Rectangle(paddleX, paddleY, length, width);
public BallPane()
{
circle.setFill(Color.GREEN); // Set ball color
getChildren().add(circle); // Place a ball into this pane
paddle.setFill(Color.RED);
paddle.setStroke(Color.RED);
paddle.setOnMouseDragged(e ->
{
paddle.setX(e.getX());
}
);
getChildren().add(paddle);
// Create an animation for moving the ball
animation = new Timeline(
new KeyFrame(Duration.millis(5), e -> moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); // Start animation
}
public void play()
{
animation.play();
}
public void pause()
{
animation.pause();
}
public void increaseSpeed()
{
animation.setRate(animation.getRate() + 0.1);
}
public void decreaseSpeed() {
animation.setRate(
animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
}
public DoubleProperty rateProperty()
{
return animation.rateProperty();
}
public void increasePaddleLength()
{
paddle.setWidth(100 * 2);
}
protected void moveBall()
{
double circX = circle.getCenterX();
double circY = circle.getCenterY() + radius;
double padY = paddle.getY();
double padX = paddle.getX();
Text text = new Text();
if (x < radius || x > sceneWidth - radius)
{
dx *= -1;
}
if (y < radius || y > sceneHeight - radius)
{
dy *= -1;
}
if (y > sceneHeight - radius)
{
--lossScore;
System.out.println(lossScore);
x = (int) (radius + Math.random() * 300);
y = radius;
circle.setCenterX(x);
circle.setCenterY(y);
++lossStreak;
hitStreak = 0;
setDifficulty(hitStreak, lossStreak);
}
if (circY == padY && (circX >= padX && circX <= (padX + length)))
{
lossStreak = 0;
++hitStreak;
setDifficulty(hitStreak, lossStreak);
dy *= -1;
if (circX > sceneWidth / 2)
{
x -= sceneWidth / 4;
}
else
{
x += sceneWidth / 4;
}
}
x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
}
public void setDifficulty(int hitStreak, int lossStreak)
{
switch (hitStreak)
{
case 10:
circle.setFill(Color.ORANGE);
increaseSpeed();
break;
case 20:
circle.setFill(Color.BLUE);
increaseSpeed();
break;
case 30:
circle.setFill(Color.YELLOW);
increaseSpeed();
}
switch (lossStreak)
{
case 5:
paddle.setWidth(length += 40);
decreaseSpeed();
break;
case 7:
paddle.setWidth(length += 40);
decreaseSpeed();
break;
}
}
public void dragMouse()
{
paddle.setOnMouseDragged(e ->
{
paddle.setX(e.getX());
if (paddle.getX() <= 0)
{
paddle.setX(0);
}
if (paddle.getX() + length >= sceneWidth)
{
paddle.setX(sceneWidth - length);
}
});
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.