Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to create the Breakout game with a ball, paddle, and bricks. Here is what I have so far. // Using a Timeline

I am trying to create the Breakout game with a ball, paddle, and bricks. Here is what I have so far.

// Using a Timeline for animation, we move a bouncing ball around the Pane

import javafx.application.Application;

import javafx.stage.*;

import javafx.event.*;

import javafx.animation.*;

import javafx.event.*;

import javafx.scene.*;

import javafx.scene.shape.*;

import javafx.scene.layout.*;

import javafx.scene.paint.*;

import java.util.*; // for Random

import javafx.util.*;

import javafx.scene.input.*; // added from last exercise to include KeyCodes

public class ch15_ex6 extends Application

{

private double bx, by, bdx, bdy; // x,y coordinate of ball and its velocity

private double px, pdx; // paddle's x coordinate and x velocity

private Circle c; // the ball

private Rectangle r;

private Pane pane; // the pane to draw on

public static void main(String[] args)

{

launch();

}

@Override

public void start(Stage primaryStage)

{

Random g = new Random();

bx = Math.abs(g.nextInt())%450 + 25; // start off at random x,y coordinate

by = Math.abs(g.nextInt())%450 + 25;

do { // start off with random velocity between [-3..3,-3..3]

bdx = Math.abs(g.nextInt())%7 - 3;

bdy = Math.abs(g.nextInt())%7 - 3;

}while(bdx==0||bdy==0); // but neither should be 0

px = 220; // start the paddle in the middle with no velocity

pdx = 0;

c = new Circle(bx,by,20); // create the ball

r = new Rectangle(px,450,60,8);

pane = new Pane();

pane.getChildren().add(c); // add initial ball to pane

pane.getChildren().add(r); // add paddle to pane

Scene scene = new Scene(pane, 500,500);

Timeline timer = new Timeline(new KeyFrame(Duration.millis(20),e -> {

pane.getChildren().remove(c); // set up our Timeline, for

pane.getChildren().remove(r); // every action, remove current ball and paddle

if(bx>450) bdx*=-1;

else if(bx

if(by>450) bdy*=-1;

else if(by

bx+=bdx; // move position of ball

by+=bdy;

c = new Circle(bx,by,20); // redraw the ball

pane.getChildren().add(c);

if(px

else if(px>390) {px = 390; pdx = 0;} // note: reset px in case its 390

px+=pdx; // using 390 because px is the left end of the paddle, not the middle

r = new Rectangle(px,450,60,8);

pane.getChildren().add(r);

}));

timer.setCycleCount(Timeline.INDEFINITE);

timer.play(); // Start animation

scene.setOnKeyPressed(e -> { // register the event handler to the scene

if(e.getCode()==KeyCode.LEFT&&pdx>-3) pdx--; // change paddle's velocity based on

else if(e.getCode()==KeyCode.RIGHT&&pdx

else if(e.getCode()==KeyCode.DOWN) pdx=0; // unless already at max velocity

});

primaryStage.setTitle("bouncing ball");

primaryStage.setScene(scene);

primaryStage.show();

}

}

Here is what the directions say:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedI really need help with the brick class. Here's info about the brick class:

There should be a separate Brick class. The Brick class contains its x,y coordinate, Color and value and whether it is currently visible or not. Aside from the constructor(s) (see the program instructions), have getters for all of the instance data and a collides method. You would use two nested for loops whenever the timer advances the ball doing

for(int i=0;i

for(int j=0;j

score += if(bricks[j][i].collides(pane, bx,by)) {}

This will cause the current Brick to see if the ball hits it and if so, set that Brick to not be visible, remove it from the pane, and return the Bricks worth to be added to the score.

For this assignment, you will create the game Breakout. If you do not know Breakout, it is a game with a bouncing ball, paddle and a series of bricks. The object is to hit the ball with the paddle so that it bounces to the other end of the game field and hits one or more bricks. As each brick is hit it disappears and the user scores points. If the user can clear the entire field of breaks, then the game resets with a new field of bricks. You may have already implemented a portion of this if you have done chapter 15 exercises 5 & 6. If you haven't look at my solution to see my code. You should start with your own code though as you will want to implement this in your own way. How the game works: 1. Have bx, by, bdx and bdy variables to represent the ball's location and velocity. The ball is a Circle 2. Have px and pdx to represent the paddle's upper-left corner and its motion in the x direction. The paddle does not move in the y direction. The paddle is a Rectangle. inner class or a class in a separate file, your choice) event handlers 3. Have any array of Bricks. Bricks are defined below as a separate class (either a nested 4. Declare the following as instance data as they will be shared among various methods and Circle ball - the ball (needed to add to and remove from the Pane as it moves) a. b. Rectangle paddle - the paddle (add and remove from Pane as it moves) int score, lives - player's current score and lives remaining c. d. Text description - output of the current score and number of lives remaining bx, by, bdx, bdy, px, pdx as noted above (used in several locations) e. f. the array of Bricks (this is a 2-D array) the Pane and the Timeline object g. h. a Random generator Your main method will call launc Your start method will instantiate or initialize all of your instance data 6. a. for the ball, set bx to the middle horizontally and by to a value beneath the Bricks but well above the paddle, give px a value in the middle horizontally, pdx should start at 0, bdx and bdy should be given random values as long as bdy is positive (so that initially, the ball is moving downward), limit bdx, bdy, pdx to a reasonable range (say -3 to +3) b. create the Text, Circle and Rectangle objects and add them to the Pane; also draw four Lines to have borders around the playing field instantiate all of the Brick objects and for each Brick object, pass it the Pane object; Brick will contact a draw method that will draw a Brick onto the Pane (described below) c. d. create a Timeline object with its own event handler (described in 7) and attach to the Scene a Key EventHandler (described in 8), use a reasonable duration (mine was 15, if you have a larger number, you will want to allow larger ranges for bdx/bdy and pdx)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

18 Impact of total rewards on recruitment and retention.

Answered: 1 week ago