Automated Pong! Create a JavaFX application that simulates a pong game (a ball b
ID: 3913798 • Letter: A
Question
Automated Pong! Create a JavaFX application that simulates a pong game (a ball bouncing from the left wall to the right wall and 2 'paddles' trying to hit it). 1. The balls should not bounce on the top or bottom of the frame. Use a random number to generate a Y value from 0- every time the ball hits the side. 2. Create 2 paddles that move (translate) with the ball as it is bouncing toward their side (left or right). For instance, if the ball is moving from left to right, have the right paddle move up/down toward the center location of where the ball will hit. Note: - You may want to change the X values to something like 20 - so that it looks like the balls are hitting the paddles rather than going through them.
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// PongGame.java
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class PongGame extends Application {
//defining all ui components
Circle ball;
Rectangle leftPaddle, rightPaddle;
//defining basic attributes for size, width, speed etc
int paddleHeight = 100, paddleWidth = 20;
int ballRadius = 20;
int windowWidth = 500, windowHeight = 500;
Random random;
Color ballColor=Color.RED;
int speed = 3;
boolean movingRight = true; //initially the ball is moving right
@Override
public void start(Stage primaryStage) {
/**
* initializing all components
*/
ball = new Circle(ballRadius);
ball.setFill(ballColor);
leftPaddle = new Rectangle(paddleWidth, paddleHeight);
rightPaddle = new Rectangle(paddleWidth, paddleHeight);
leftPaddle.setLayoutX(0);
leftPaddle.setLayoutY(windowHeight / 2);
rightPaddle.setLayoutX(windowWidth - paddleWidth);
rightPaddle.setLayoutY(windowHeight / 2);
ball.setLayoutX(windowWidth / 2);
ball.setLayoutY(windowHeight / 2);
/**
* Adding everything to a group and adding the group to a pane
*/
Group g = new Group(leftPaddle, ball, rightPaddle);
Pane pane = new Pane(g);
Scene scene = new Scene(pane, windowWidth, windowHeight);
primaryStage.setScene(scene);
primaryStage.setTitle("Pong Game");
primaryStage.show();
primaryStage.setResizable(false);
random = new Random();
/**
* Defining the timer for animation
*/
new AnimationTimer() {
@Override
public void handle(long now) {
/**
* Adjusting the ball's position
*/
if (movingRight) {
//moving right
ball.setLayoutX(ball.getLayoutX() + speed);
if (ball.getLayoutX() + ballRadius >= rightPaddle.getLayoutX()) {
movingRight = false;
ball.setLayoutX(ball.getLayoutX() - 2);
ball.setLayoutY(generateRandomY());
}
} else {
//moving left
ball.setLayoutX(ball.getLayoutX() - speed);
if (ball.getLayoutX() - ballRadius <= leftPaddle.getLayoutX() + paddleWidth) {
movingRight = true;
ball.setLayoutX(ball.getLayoutX() + 2);
ball.setLayoutY(generateRandomY());
}
}
/**
* Adjusting the paddle position
*/
if (movingRight) {
if (rightPaddle.getLayoutY() + paddleHeight / 2 < ball.getLayoutY()) {
if (rightPaddle.getLayoutY() + speed < windowHeight) {
rightPaddle.setLayoutY(rightPaddle.getLayoutY() + speed);
}
}
if (rightPaddle.getLayoutY() + paddleHeight / 2 > ball.getLayoutY()) {
if (rightPaddle.getLayoutY() - speed > 0) {
rightPaddle.setLayoutY(rightPaddle.getLayoutY() - speed);
}
}
} else {
if (leftPaddle.getLayoutY() + paddleHeight / 2 < ball.getLayoutY()) {
if (leftPaddle.getLayoutY() + speed < windowHeight) {
leftPaddle.setLayoutY(leftPaddle.getLayoutY() + speed);
}
}
if (leftPaddle.getLayoutY() + paddleHeight / 2 > ball.getLayoutY()) {
if (leftPaddle.getLayoutY() - speed > 0) {
leftPaddle.setLayoutY(leftPaddle.getLayoutY() - speed);
}
}
}
}
}.start(); //starting the animation
}
/**
* method to generate a random y value between 0 and windowheight when the
* ball hits either paddles
*/
public int generateRandomY() {
return random.nextInt(windowHeight);
}
public static void main(String[] args) {
launch(args);
}
}
/*OUTPUT*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.