Create a Java fx for a single player who wants to play a game called Pong. The r
ID: 3812772 • Letter: C
Question
Create a Java fx for a single player who wants to play a game called Pong. The requirements are a rectangular paddle moves back and forth via mouse drag along the bottom of the pane; 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. If the ball misses the paddle, then the score is decremented by 1. The game ends when 20 points are lost. This game also needs to have a label that displays the score (you can start at 20 and go to zero if you want...) For every 10 paddle connections in a row, the ball moves faster. For every 10 paddle connections in a row, the ball changes color. For every (2?) paddle misses in a row, the paddle grows in length. Thanks!
Explanation / Answer
Answer:
Java fx for single player game called Pong:
package ballpane;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class BallPane extended Pane{
public final double radius=20;
private double x=radius,y=radius;
private double dx=1,dy=1;
privateTimeline animation;
private Circle circle=new Circle(x,y,radius);
public BallPane(){
circle.setFill(color.GREEN); //set ball color
getChildren().add(circle); //place a ball into this pane
//Create an animation for moving the ball
animation=new Timeline(new KeyFrame(Duration.millis(50),e->moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); //start animation
}
public void play(){
animation.play();
}
public void pause(){
animaion.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();
}
protected void moveBall(){
//check boundaries
if(x<radius|| x>getwidth()-radius){
dx*=-1;//Change ball move direction
}
if(y<radius||y>getHeight()-radius){
dy*=-1;//Change ball move direction
}
//adjust ball position
x+=dx;
y+=dy;
circle.setCenterX(x);
circle.setCenterY(y);
}
}
BounceBallControl:
package ballpane;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.scene;
import javafx.scene.input.KeyCode;
public class BounceBallContrl extends Application{
@Override//Override the start method in the Application class
public void start(Stage primaryStage){
BallPane ballPane=new BallPane();//Create a ball pane
//Pause and resume animation
ballPane.setOnMousePressed(e->ballPane.pause());
ballPane.setOnMouseReleased(e->ballPane.play());
//Increase and decrease animation
ballPane.setOnKeyPressed(e->{
if(e.getCode()==KeyCode.UP){
ballPane.decreaseSpeed();
}
});
//create a scene and place it in the stage
Scene scene=new Scene(ballPane,250,150);
primaryStage.setTitle("BounceBallControl");//set the stage title
primaryStage.setScene(scene);//Place the scene in the stage
primaryStage.show();//display the stage
//Must request focus after the primary stage is displayed
ballPane.requestFocus();
}