Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java help! In this assignment we'll be using object oriented design to tackle the classic 1976 Atari game Breakout, more commonly known today as Brick

Java help!

In this assignment we'll be using object oriented design to tackle the classic 1976 Atari game Breakout, more commonly known today as "Brick Breaker". In Breakout, the player moves a rectangular paddle left and right at the bottom of the screen in order to bounce a ball upward towards a wall of bricks. Bricks have varying strengths, which means that they may require multiple hits from the ball in order to break. The ball will bounce off of the left, top, and right sides of the screen in addition to bouncing off of bricks. The objective of the game is to eliminate all breakable bricks without letting the ball go off the bottom of the screen. If the user does this they have won, if not, they have lost. This game has been re-imagined many times over the years with new features, but the basic mechanics are always the same.

Many games employ a simple object-oriented model of the world to represent the various elements in the game. One key element of this is that the model gets updated incrementally over time by repeated calls to an update or move method. The game GUI will take care of repeatedly calling this method to update the model over and over as time progresses in the game. The tricky part is ensuring that the model maintains accurate information about the state of the "world" at each step.

image text in transcribed

image text in transcribed

image text in transcribed

Essentially, we need to make four new files Ball.java, Brick.java, Paddle.java and Level.java in the same src file which contains BrickBreakerGUI.java, TouchPosition.java and GameState.java. Below is the given starter codes:

BrickBreakerGUI.java

import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.Cursor; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.control.ButtonType; import javafx.scene.control.ChoiceDialog;

///////////////////////////////////////////////// //DO NOT EDIT THIS FILE. CHANGING THIS CODE //IS NOT REQUIRED BY THE ASSIGNMENT. /////////////////////////////////////////////////

/** * This class provides a GUI interface for the BrickBreaker game. It utilizes * state information provided by a Level object for the underlying game model, * while providing user interactivity through mouse input. * * THIS CLASS IS PROVIDED AND SHOULD NOT BE EDITED. * */ public class BrickBreakerGUI extends Application { /** * The fixed height of the UI screen for the game. */ private static final int GUI_HEIGHT = 440; /** * The fixed width of the UI screen for the game. */ private static final int GUI_WIDTH = 400; /** * The model object used to store all state information about the level * currently being played on this application. */ private Level theLevel; /** * A rectangle that provides the visual component for the Paddle object * inside the Level. */ private Rectangle paddleView; /** * A circle to provide the visual view of the Ball object inside the * Level. */ private Circle ballView; /** * A 2D array of rectangles which is parallel to the Brick objects * stored inside the Level. Used to visually represent the Bricks * on screen. */ private Rectangle[][] bricksView; @Override public void start(Stage theStage) throws Exception { //Ask the user which level they'd like to play ChoiceDialog d = new ChoiceDialog("Level 1", "Level 1", "Level 2", "Level 3"); d.setTitle("Select a Level"); d.setHeaderText("What level would you like to play?"); d.getDialogPane().lookupButton(ButtonType.CANCEL).setVisible(false); d.showAndWait(); //Now construct the corresponding Level object. switch (d.getSelectedItem()) { //NOTE: Possible future extension would be to read level data from external // text file rather than hard coded values here. case "Level 1": theLevel = new Level(GUI_WIDTH, GUI_HEIGHT); break; case "Level 2": String[] config = {"3300033", "2001002", "*33333*", "0022200", "1111111"}; theLevel = new Level(GUI_WIDTH, GUI_HEIGHT, config); break; case "Level 3": String[] config2 = {"***3***", "**111***", "*22222*", "1122211", "**111**"}; theLevel = new Level(GUI_WIDTH, GUI_HEIGHT, config2); break; default: //Do nothing. break; } //Create the game screen on which to place objects Pane theCanvas = new Pane(); theCanvas.setStyle("-fx-background-color: white;"); Scene scene = new Scene(theCanvas, GUI_WIDTH, GUI_HEIGHT, Color.WHITE); scene.setCursor(Cursor.NONE); //Create the paddle on the screen Paddle thePaddle = theLevel.getPaddle(); if (thePaddle != null) //Check in case a stubbed null paddle is provided { paddleView = new Rectangle(thePaddle.getLeft(), thePaddle.getTop(), thePaddle.getWidth(), thePaddle.getHeight()); theCanvas.getChildren().add(paddleView); } //Create all the bricks Brick[][] bricks = theLevel.getBricks(); if (bricks != null) { bricksView = new Rectangle[bricks.length][bricks[0].length]; for (int i = 0; i () { @Override public void handle(MouseEvent event) { int newX = (int) event.getX(); Paddle thePaddle = theLevel.getPaddle(); if (thePaddle != null) //Check in case a stubbed null paddle is provided { int paddleWidth = thePaddle.getWidth(); //Ensure that the paddle doesn't go off the right edge if (newX + paddleWidth > GUI_WIDTH) { newX = GUI_WIDTH - paddleWidth; } //Update the model and the view theLevel.getPaddle().moveTo(newX); paddleView.setX(newX); } } }); //end mouse moved handler //Game Loop Timer AnimationTimer timer = new AnimationTimer() { // This handle method will execute every time the animation timer goes off. // By default this is 60 times a second. @Override public void handle(long arg) { //Request the student code to update the model one tick of the clock theLevel.updateOneStep(); //Update the visual position of the ball based on the model state Ball theBall = theLevel.getBall(); if (theBall != null) //Check in case a stubbed null Ball is provided { ballView.setCenterX(theBall.getX()); ballView.setCenterY(theBall.getY()); } //Update the brick color for all bricks based on their state Brick[][] bricks = theLevel.getBricks(); if (bricks != null) //Check in case a stubbed null bricks array is provided { for (int i = 0; i

/** * Skeleton main method as required for launching a JavaFX application. * * @param args Not used. */ public static void main(String[] args) { launch(args); } }

TouchPosition.java

///////////////////////////////////////////////// // DO NOT EDIT THIS FILE. CHANGING THIS CODE // IS NOT REQUIRED BY THE ASSIGNMENT. /////////////////////////////////////////////////

/** * This enumerated type represents the possible locations where two objects in the * game are touching one another. * * THIS ENUM DEFINITION IS PROVIDED AND SHOULD NOT BE EDITED. * */ public enum TouchPosition { /** * Used to signal that the Ball has collided with the top of another object. */ TOP, /** * Used to signal that the Ball has collided with the bottom of another object. */ BOTTOM, /** * Used to signal that the Ball has collided with the left side of another object. */ LEFT, /** * Used to signal that the Ball has collided with the right side of another object. */ RIGHT, /** * Used to signal that the Ball has not collided with another object at this time. */ NONE }

GameState.java

///////////////////////////////////////////////// // DO NOT EDIT THIS FILE. CHANGING THIS CODE // IS NOT REQUIRED BY THE ASSIGNMENT. /////////////////////////////////////////////////

/** * A simple type to represent the three possible game states in a BrickBreaker * game. * * THIS ENUM DEFINITION IS PROVIDED AND SHOULD NOT BE EDITED. * */ public enum GameState { /** * Represents when the user has lost the game by allow the ball to go off-screen. */ LOST, /** * Represents when the user has one the game by breaking all possible bricks. */ WON, /** * Reprsents when the game is still in progress. */ PLAYING }

Brick Breaker Supplemental Instructions Computer Graphics 101 One of the important things you'll notice while working on this game is that computer graphics applications employ a 2-dimensional coordinate system that is a little different that what you may be used to in mathematics. The first thing you'll likely notice is that in computer graphics, the origin of a space starts at the top-left corner of the screen/window. Values of x get larger towards the right of the screen/window, and y- values get larger as you approach the bottom of the screen/window. Negative positions for x and y are allowed, but those objects would appear logically outside the viewable area of the window. Here's an annotated picture: (0, 0) CS2 Brick Breaker! 400, 0) (0, 440) 400, 440) Furthermore, we place many objects on the screen based on the coordinate of their "top-left" corner. For example, here, we'd say that the Paddle at the bottom of the screen lives at (320, 420) since its top- left corner is 80 pixels away from the right edge and 20 pixels up from the bottom edge. Some objects, like the circular ball are positioned based on the x and y coordinates for their center. It's important to keep in mind how these different objects are aligned, particular when creating them initially and when comparing their positions with one another to determine if they are colliding. Brick Breaker Supplemental Instructions Computer Graphics 101 One of the important things you'll notice while working on this game is that computer graphics applications employ a 2-dimensional coordinate system that is a little different that what you may be used to in mathematics. The first thing you'll likely notice is that in computer graphics, the origin of a space starts at the top-left corner of the screen/window. Values of x get larger towards the right of the screen/window, and y- values get larger as you approach the bottom of the screen/window. Negative positions for x and y are allowed, but those objects would appear logically outside the viewable area of the window. Here's an annotated picture: (0, 0) CS2 Brick Breaker! 400, 0) (0, 440) 400, 440) Furthermore, we place many objects on the screen based on the coordinate of their "top-left" corner. For example, here, we'd say that the Paddle at the bottom of the screen lives at (320, 420) since its top- left corner is 80 pixels away from the right edge and 20 pixels up from the bottom edge. Some objects, like the circular ball are positioned based on the x and y coordinates for their center. It's important to keep in mind how these different objects are aligned, particular when creating them initially and when comparing their positions with one another to determine if they are colliding

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

Step: 3

blur-text-image

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

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

=+ Outline intrinsic and extrinsic sources of employee motivation.

Answered: 1 week ago