Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm sorry if the codes are barely readable but chegg would tell me my question is too long if I don't make it that way.

I'm sorry if the codes are barely readable but chegg would tell me my question is too long if I don't make it that way.

image text in transcribedimage text in transcribedimage text in transcribed

import javafx.geometry.Point2D; public abstract class GameObject { protected Point2D location; public GameObject(Point2D loc) { location = loc; } public Point2D getLocation() { return location; } public void setLocation(Point2D newLocation) { location = newLocation; } public abstract void update(); public abstract char appearance();}
import javafx.geometry.Point2D; public abstract class MovableObject extends GameObject { protected Point2D previousLocation; protected int direction; protected int speed; public abstract void draw(); public void setLocation(Point2D newLocation) { previousLocation = location; location = newLocation; } public MovableObject(int d, int s, Point2D loc) { super(loc); previousLocation = location; direction = d; speed = s; } public void update() { moveForward(); draw(); } public void moveForward() { if (speed > 0) { previousLocation = location; location = location.add((int) (Math.cos(Math.toRadians(direction)) * speed), -(int) (Math.sin(Math.toRadians(direction)) * speed)); } } public Point2D getPreviousLocation() { return previousLocation; }}
import javafx.geometry.Point2D; import javafx.scene.paint.Color; public class Player extends MovableObject { private String name; private Color color; private boolean hasBall; private int score; public Player(String n, Color c, Point2D loc, int dir) { super(dir, 0, loc); name = n; color = c; hasBall = false; score = 0; } public int getDirection() { return direction; } public int getSpeed() { return speed; } public String getName() { return name; } public Color getColor() { return color; } public boolean hasBall() { return hasBall; } public int getScore() { return score; } public void setDirection(int newDirection) { direction = newDirection; } public void setSpeed(int newSpeed) { speed = newSpeed; } public void setHasBall(boolean newHasBall) { hasBall = newHasBall; } public void setScore(int newScore) { score = newScore; } public String toString() { String s = "Player" + " " + name + " at (" + (int)location.getX() + "," + (int)location.getY() + ") facing " + direction + " degrees"; if (hasBall) s += " with the ball"; return s; } public void draw() { } public char appearance() { switch(direction) { case 0: return '>'; case 90: return '^'; case 180: return ' 
import javafx.geometry.Point2D; public class Ball extends MovableObject implements Harmful{ private boolean isBeingHeld; public Ball(Point2D loc) { super(0, 0, loc); isBeingHeld = false; } public int getDirection() { return direction; } public int getSpeed() { return speed; } public boolean isBeingHeld() { return isBeingHeld; } public void setDirection(int newDirection) { direction = newDirection; } public void setSpeed(int newSpeed) { speed = newSpeed; } public void setIsBeingHeld(boolean newHoldStatus) { isBeingHeld = newHoldStatus; } public String toString() { return "Ball" + " at (" + (int)location.getX() + "," + (int)location.getY() + ") facing " + direction + " degrees going " + speed + " pixels per second"; } public void draw() { } public void update() { moveForward(); draw(); if (speed > 0) speed -= 1; } public int getDamageAmount() { return -200; } public char appearance() { return 'O';}}
import javafx.geometry.Point2D; public abstract class StationaryObject extends GameObject { public StationaryObject(Point2D loc) { super(loc); } public void update() {}}
import javafx.geometry.Point2D; public class Wall extends StationaryObject { private int width; private int height; public Wall(Point2D loc, int w, int h) { super(loc); width = w; height = h; } public Point2D getLocation() { return location; } public int getWidth() { return width; } public int getHeight() { return height; } public void setLocation(Point2D newLocation) { location = newLocation; } public String toString() { return "Wall" + " at (" + (int)location.getX() + "," + (int)location.getY() + ") with width " + width + " and height " + height; } public char appearance() { return '#'; }}
import javafx.geometry.Point2D; public class Trap extends StationaryObject implements Harmful{ public Trap(Point2D loc) { super(loc); } public String toString() { return "Trap" + " at (" + (int)location.getX() + "," + (int)location.getY() + ")"; } public int getDamageAmount() { return -50; } public char appearance() { return '@';}}
import javafx.geometry.Point2D; public class Prize extends StationaryObject { private int value; public Prize(Point2D loc, int val) { super(loc); value = val; } public int getValue() { return value; } public String toString() { return "Prize" + " at (" + (int)location.getX() + "," + (int)location.getY() + ") with value " + value; } public char appearance() { return '$';}}
import java.lang.*; import java.util.ArrayList; public class Game { public static final int MAX_GAME_OBJECTS = 1000; GameObject[] gameObjects; int objectCount; GameBoard gameBoard; public Game() { gameObjects = new GameObject[MAX_GAME_OBJECTS]; objectCount = 0; gameBoard = new GameBoard(); } public void add(GameObject obj) { if (objectCount MAX_GAME_OBJECTS) gameObjects[objectCount++] = obj; } public GameObject[] getGameObjects() { return gameObjects; } public int getObjectCount() { return objectCount; } public String toString() { return "Game with " + objectCount + " objects"; } public void displayObjects() { for (int i=0; iout.println(gameObjects[i]); } public void updateObjects() { for (int i=0; i 
import javafx.geometry.Point2D; import javafx.scene.paint.Color; public class GameTestProgram { public static void main(String args[]) { Game g; g = new Game(); g.add(new Wall(new Point2D(0,0), 10, 200)); g.add(new Wall(new Point2D(10,0), 170, 10)); g.add(new Wall(new Point2D(180,0), 10, 200)); g.add(new Wall(new Point2D(10,190), 170, 10)); g.add(new Wall(new Point2D(80,60), 100, 10)); g.add(new Wall(new Point2D(10,90), 40, 10)); g.add(new Wall(new Point2D(100,100), 10, 50)); g.add(new Prize(new Point2D(165,25), 1000)); g.add(new Prize(new Point2D(65,95), 500)); g.add(new Prize(new Point2D(145,165), 750)); g.add(new Trap(new Point2D(125,35))); g.add(new Trap(new Point2D(145, 145))); System.out.println("Here are the Game Objects:"); g.displayObjects(); System.out.println("--------------------------------------------------------"); Player player = new Player("Red Guy", Color.RED, new Point2D(100,100), 0); player.speed = 10; player.direction = 0; g.add(player); Ball ball = new Ball(new Point2D(100,100)); ball.speed = 10; ball.direction = 0; g.add(ball); for (int i=0; iout.println(" Here are the Harmful Objects:"); Harmful[] dangerousStuff = g.harmfulObjects(); for (Harmful d: dangerousStuff) System.out.println(" " + d); System.out.println(" Current Danger Assessment:"); System.out.println(g.assessDanger()); }}
public class GameBoard { public static final int WIDTH = 20; public static final int HEIGHT = 25; private char[][] grid; public GameBoard() { grid = new char[WIDTH][HEIGHT]; for (int i=0; iWIDTH; i++) for (int j=0; jHEIGHT; j++) grid[i][j] = ' '; } public void display(Game g) { for(int go=0; goWIDTH) && (m.getPreviousLocation().getX() >= 0) && (m.getPreviousLocation().getY() HEIGHT) && (m.getPreviousLocation().getY() >= 0)) grid[(int)m.getPreviousLocation().getX()][(int)m.getPreviousLocation().getY()] = ' '; } } for(int go=0; goWIDTH) && (obj.getLocation().getX() >= 0) && (obj.getLocation().getY() HEIGHT) && (obj.getLocation().getY() >= 0)) grid[(int)obj.getLocation().getX()][(int)obj.getLocation().getY()] = obj.appearance(); } } for (int r=0; rHEIGHT; r++) { for (int c=0; cWIDTH; c++) System.out.print(grid[c][r]); System.out.println(); } }}
import javafx.geometry.Point2D; import javafx.scene.paint.Color; public class GameBoardTestProgram { public static void main(String args[]) { Game g = new Game(); g.add(new Wall(new Point2D(0,0), 20, 1)); g.add(new Wall(new Point2D(0,0), 1, 15)); g.add(new Wall(new Point2D(19,0), 1, 15)); g.add(new Wall(new Point2D(0,14), 20, 1)); g.add(new Wall(new Point2D(1,6), 4, 1)); g.add(new Wall(new Point2D(9,4), 10, 1)); g.add(new Wall(new Point2D(12,7), 1, 4)); g.add(new Prize(new Point2D(17,1), 1000)); g.add(new Prize(new Point2D(6,6), 500)); g.add(new Prize(new Point2D(15,12), 750)); g.add(new Trap(new Point2D(13,2))); g.add(new Trap(new Point2D(15,10))); Player player = new Player("Red Guy", Color.RED, new Point2D(3, 12), 0); player.speed = 1; player.direction = 90; g.add(player); Ball ball = new Ball(new Point2D(3, 11)); ball.speed = 5; ball.direction = 0; g.add(ball); for (int i=0; iout.println("--------------------"); } }}
1Recall these classes GameObject MovableObject Harmful Object . Player . Ball GameObject Game StationaryObject Wall . Trap MovableObject StationaryObject Prize . Game Player Ball Wal Trap Prize GameTestProgram GameBoard GameBoardTestProgram Now we will add code that will generate a CollisionException when a Player collides with a wall. Of course, your code should attempt to prevent the Player from colliding with a wall, but in the off-chance that there is a bug in your code or that something unexpected occurs in the game, your player may end up colliding with the wall. A. Create and compile a CollisionException class as follows public class CollisionException extends Exception public CollisionException super ("Player collided with wall !")i Note that any new exceptions that you define must extend the Exception class or one of its subclasses. Once this code is saved and compiled, you are able to generate such an exception. B. Copy this unfinished method code and then complete it in the Wall class public boolean contains (Point2D p) 7return true if p.x is in the range from "this.location.x to 7(this.location.x+this.width-1)" and if p.y is in the range rom "this.location.y to (this.location.ytthis height-1) /otherwise return false C. Add the following methods to the Game class so that we can get the Players and the Walls from the Game: public ArrayList getPlayers) ArrayList result-new ArrayListO for (GameObject g: gameobjects) if (g instanceof Player) result.add ( (Player)g) return result; public ArrayList getwalls) ArrayList resultnew ArrayList for GameObject g: gameobjects) if (g instanceof Wal1) result.add( (Wall)g) return result; D. Append the following piece of code to the end of the updateObjects) method in the Game class which will compare all players with all walls to see if a collision occurs and will then throw our newly defined exception if it has indeed occurred: ArrayList getPlayers) ArrayList result-new ArrayListO for (GameObject g: gameobjects) if (g instanceof Player) result.add ( (Player)g) return result; public ArrayList getwalls) ArrayList resultnew ArrayList for GameObject g: gameobjects) if (g instanceof Wal1) result.add( (Wall)g) return result; D. Append the following piece of code to the end of the updateObjects) method in the Game class which will compare all players with all walls to see if a collision occurs and will then throw our newly defined exception if it has indeed occurred: ArrayList

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

Students also viewed these Databases questions