Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sprite.java import java.awt.*; import java.io.*; import java.net.*; import java.util.*; import javax.imageio.*; public class Sprite { private double x; //the x-coordinate of the center of the

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Sprite.java

import java.awt.*; import java.io.*; import java.net.*; import java.util.*; import javax.imageio.*; public class Sprite { private double x; //the x-coordinate of the center of the sprite private double y; //the y-coordinate of the center of the sprite private int width; //width of the sprite (for drawing) private int height; //height of the sprite (for drawing) private String image; //filename of the sprite's image public Sprite(double x, double y, int width, int height, String image) { this.x = x; this.y = y; this.width = width; this.height = height; this.setImage(image); } /** draw the sprite to the screen */ public void draw() { //draw based on sprite's size, so you don't have to manually scale images StdDraw.picture(this.getX(), this.getY(), this.getImage(), this.width, this.height); } /** * One step of the simulation - a Sprite does whatever a Sprite does (move around, etc.) * * must be supplied with a reference to the world this sprite exists in */ public void step(World world) { //TODO } //-----------------Getters------------------- public double getX() { return this.x; } public int getWidth() { return this.width; } public double getY() { return this.y; } public int getHeight() { return this.height; } public String getImage() { return this.image; } //-----------------Setters------------------- public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public void setWidth(int width) { this.width = width; } public void setHeight(int height) { this.height = height; } public void setImage(String image) { this.image = image; } } 

StdDraw.java

https://www.dropbox.com/sh/rwt6z5z2obmibsh/AAB30lliYSRCnPTOvPjhPjf1a/Lab15-Sprites%20%28abstract%20intro%29/Sprites%20starter%20files/StdDraw.java?dl=0

World.java

import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.ArrayList; import java.util.List;

public class World { private List sprites; private int width; private int height;

/** construct a world 600x600 */ public World() { this(600, 600); }

public World(int h, int w) { height = h; width = w; sprites = new ArrayList();

StdDraw.setCanvasSize(width, height); StdDraw.setXscale(0, width); StdDraw.setYscale(0, height); StdDraw.clear(Color.BLACK); }

/** add a sprite to the simulation */ public void add(Sprite sprite) { this.sprites.add(sprite); }

/** ask all sprites in simulation to update themselves */ public void stepAll() { for (int i = 0; i

/** get the width of the world */ public int getWidth() { return width; }

/** get the height of the world */ public int getHeight() { return height; }

/** get the number of sprites in the simulation currently */ public int getNumSprites() { return sprites.size(); }

/** get the sprite at the given index */ public Sprite getSprite(int index) { return sprites.get(index); }

/** run the simulation, i.e. the main game loop */ public void run() { while (true) { this.stepAll(); this.drawAll();

StdDraw.show(10); //don't worry about warning if using Eclipse StdDraw.clear(Color.BLACK); } }

/** draw all sprites in the simulation at their current positions */ public void drawAll() { for (Sprite sprite : this.sprites) sprite.draw(); }

public static void main(String[] args) { World world = new World(600, 600); world.run(); } }

Sprites This lab introduces you to the concept of abstract classes. An abstract class can contain a mixture of abstract (declared only) and concrete methods, along with instance variables and constructors. Abstract methods are quite useful when a parent class knows all sub-classes should have a method, but the implementation is not known, won't be used, or doesn't make sense for the parent. Import the starter code and images into your project. The images must be in the "root" directory, the main project folder in Blue) and Eclipse (NOT the "src" folder in Eclipse). The classes you will be using are summarized below: Std Draw The dass that provides the GUI / drawing methods. You shouldn't need to make any changes to this class. For more drawing methods, see the class' API here. World The World class represents the 2-dimensional stage where sprites appear. World has fields List sprites, a list of Sprite objects to draw, and the World's height and widt, along with a main method to begin the animations. The world / Std Draw's coordinate system is as shown to the right: Sprite This class represents a single visual entity in the world. A Sprite knows its position in the world (specifically the X-and Y-coordinates of its center position). A sprite also knows its width and height (in pixels), along with the file name of the image used to display it. Sprites will be displayed as their images, within their bounding rectangle. You will be extending the Sprite class to make on-screen objects with various behaviors. Follow these steps: 1. Compile and run World.java: you should see an empty "world" pop up (a black rectangle). 2. Sprite.java is a base class that houses code common to multiple (future) types of sprites. In this simulation, you will never add a Sprite, you'll add more specialized types of sprites (e.g. a moving sprite or a bouncing sprite). Add the abstract modifier to the Sprite class' header, and convert the existing step method to an abstract method (a method that is declared only), that sub-classes will have to override: public abstract void step (World world); /o body, declared only! The step method represents one step of the simulation / game (where a sprite does whatever it normally does) and must be supplied a reference to the World where the Sprite exists. 3. Make a new concrete class Stationary Sprite.java that extends Sprite. Page 2 of 4 a. Add an appropriate constructor, given the variables inherited from Sprite that need to be initialized when making a Stationary Sprite object. i. Don't forget a super() call to pass these values to its super-class' constructor (the inherited variables should NOT be re-dedared in the StationarySprite class). b. Override the step method. Every step of the simulation, a stationary sprite does... nothing. This shouldn't take you very long. Nice! 4. In the World class, add a method private void addSprites () that will create / add sprite objects to the world. For now, create a Stationary Sprite object, using the "square.png" file to represent this type of sprite. Don't forget to add it to the sprites list. a. At the end of the World class' two-parameter constructor, call the addsorites method. i. Run World's main method - you should now see a red square in the world. Woah! 5. Make a new concrete class MobileSprite.java that extends Sprite. The class will represent sprites that can move around, and therefore must have variables to store velocity. a. Add two private double variables called vx and vy that represent the X- and Y-components of this sprite's current velocity. is moving to the right i. For example, a vx of 2.5 and a vy of -0.5 represent a sprite th (increasing X) and slightly down (decreasing Y). b. Add an appropriate constructor, and getters and setters (for vx and vy). C. Override the step method inherited from Sprite. i. Each step, this sprite needs to move based on its velocity. Note that the x and y variables are declared private in Sprite; even though this class is a sub-class of Sprite, these variables can't be accessed directly. Use the appropriate getter methods. 6. Make a new concrete class BouncingSprite.java that extends MobileSprite. BouncingSprite objects will move around based on their velocity but will also bounce off the edges of the world. a. This type of sprite should bounce off the top, bottom, left, and right edges of the world. Add a method private void bounce () to accomplish this; call it from step. b. Using the "circle.png" image, add a couple BouncingSprite objects to the world at random positions, then run World's main method to ensure everything is working. 7. Make a new concrete class Heavy Sprite.java that extends BouncingSprite. A "heavy" sprite is very similar to a "bouncing" sprite, except that it experiences gravity. Again override the step method, this Page 2 of 4 time utilizing a call to its super-class method, followed by the code to make it fall 0.1 pixel per step. This class' step method should contain only two lines of code. a. Write the code to ensure this class works as expected. Use the "triangle.png" file to represent heavy sprites. Test and fix any mistakes you may have. 8. Write a concrete method in Sprite that takes in another Sprite and returns true if they overlap. The rectangular boundaries of two sprites A and B overlap if A's left edge is left of B's right edge, B's left is left of A's right, A's top is above B's bottom, and B's top is above A's bottom (think about it). a. Test this method thoroughly before proceeding. I mean, it's going to be inherited by a bunch of classes - it better work! Note that even though Heavy Sprite has an image of a triangle, its "hit box" is still a rectangle. 9. Add a new class ControllableSprite.java that extends MobileSprite. ControllableSprites will respond to arrow key presses (by adjusting their velocities) to move around the screen. a. Each step of the simulation, a controllable sprite should be moving left only if the left arrow is pressed, right if the right arrow is pressed, etc. The sprite should not be allowed to move if it is at the edge of the world. b. Std Draw has a static method iskeyFressed that will return true if the given key is currently being pressed. It expects an integer, a 'key code' that corresponds to a key on the keyboard. These key codes are stored in the java.awt.event.KeyEvent class as static final int constants. For example, the 'up' arrow is represented as KeyEvent.VK_UP. C. Add a (school-appropriate) picture of your choosing to the project folder and add a new controllable sprite (using this image) to the simulation. Test using World's main method. i. Sprite's draw method draws the sprite given its height and width (established at creation). You don't need to manually resize images, but it would probably work best not to use tiny or giant images. 10. Given whatever time you have left, implement the following enhancements. For those working ahead, implement as many as you like (this step is quite fun and can become a decent platform for a game). Rather than implementing some number of unrelated behaviors one after the other, you are strongly encouraged to work toward a specific simulation or game and implement the relevant behaviors below. Alive - Modify Sprite so that it keeps track of whether it is alive or dead. Modify World's stepall method to remove any dead sprites from the list. Lifespan - Create a new kind of sprite that dies at a specified time (when it reaches the edge of the screen, or after a specified number of steps). (Completing Alive first will be required for this.) Generator - Create a new kind of sprite that generates other sprites (e.g. fires projectiles) (Complete Lifespan if you want these projectiles to disappear eventually.) Page 3 of 4 Killing - Create a new kind of sprite that kills or dies when it collides with another sprite. (Complete Alive first.) Landing - Create a new kind of sprite that behaves exactly like a HeavySprite, except that it stops falling when it lands on another sprite. (Hopefully it doesn't stop inside the other sprite.) Contains - Write a method in Sprite that takes in an x and y and determines if that point lies inside the boundary of the sprite Clicking - Create a new sprite wherever you click, or (alternatively) interact with (e.g. kill) whatever sprite you clicked on. (To determine what sprite you clicked on, complete Contains first.) Jumping - Change the velocity of a specific sprite whenever a certain key is pressed so that it appears to jump. (Complete Landing first.) Bouncing - Create a new kind of sprite that bounces off other sprites it comes in contact with, by changing direction (according to some rule you devise) without changing speed. The following code segments can help you convert between vx/vy and speed/direction. 1/given speed and direction in degrees, find vx and vy double vx - speed * Math.cos(Math.toRadians (direction)); double vy - speed * Math.sin(Math.toRadians (direction)); 1/given vx and vy, find speed and direction double speed = Math.sqrt (vx * vx + vy vy); //pythagorean theorem double direction - Math.toDegrees (Math.atan2 (vy, vx)); //atan2 is in radians Adapted from the Sprites lab by Dave Feinberg https://sites.google.com/site/feinbergcompsci/home/hcsi/abs/spritelab Page 4 of 4 Sprites This lab introduces you to the concept of abstract classes. An abstract class can contain a mixture of abstract (declared only) and concrete methods, along with instance variables and constructors. Abstract methods are quite useful when a parent class knows all sub-classes should have a method, but the implementation is not known, won't be used, or doesn't make sense for the parent. Import the starter code and images into your project. The images must be in the "root" directory, the main project folder in Blue) and Eclipse (NOT the "src" folder in Eclipse). The classes you will be using are summarized below: Std Draw The dass that provides the GUI / drawing methods. You shouldn't need to make any changes to this class. For more drawing methods, see the class' API here. World The World class represents the 2-dimensional stage where sprites appear. World has fields List sprites, a list of Sprite objects to draw, and the World's height and widt, along with a main method to begin the animations. The world / Std Draw's coordinate system is as shown to the right: Sprite This class represents a single visual entity in the world. A Sprite knows its position in the world (specifically the X-and Y-coordinates of its center position). A sprite also knows its width and height (in pixels), along with the file name of the image used to display it. Sprites will be displayed as their images, within their bounding rectangle. You will be extending the Sprite class to make on-screen objects with various behaviors. Follow these steps: 1. Compile and run World.java: you should see an empty "world" pop up (a black rectangle). 2. Sprite.java is a base class that houses code common to multiple (future) types of sprites. In this simulation, you will never add a Sprite, you'll add more specialized types of sprites (e.g. a moving sprite or a bouncing sprite). Add the abstract modifier to the Sprite class' header, and convert the existing step method to an abstract method (a method that is declared only), that sub-classes will have to override: public abstract void step (World world); /o body, declared only! The step method represents one step of the simulation / game (where a sprite does whatever it normally does) and must be supplied a reference to the World where the Sprite exists. 3. Make a new concrete class Stationary Sprite.java that extends Sprite. Page 2 of 4 a. Add an appropriate constructor, given the variables inherited from Sprite that need to be initialized when making a Stationary Sprite object. i. Don't forget a super() call to pass these values to its super-class' constructor (the inherited variables should NOT be re-dedared in the StationarySprite class). b. Override the step method. Every step of the simulation, a stationary sprite does... nothing. This shouldn't take you very long. Nice! 4. In the World class, add a method private void addSprites () that will create / add sprite objects to the world. For now, create a Stationary Sprite object, using the "square.png" file to represent this type of sprite. Don't forget to add it to the sprites list. a. At the end of the World class' two-parameter constructor, call the addsorites method. i. Run World's main method - you should now see a red square in the world. Woah! 5. Make a new concrete class MobileSprite.java that extends Sprite. The class will represent sprites that can move around, and therefore must have variables to store velocity. a. Add two private double variables called vx and vy that represent the X- and Y-components of this sprite's current velocity. is moving to the right i. For example, a vx of 2.5 and a vy of -0.5 represent a sprite th (increasing X) and slightly down (decreasing Y). b. Add an appropriate constructor, and getters and setters (for vx and vy). C. Override the step method inherited from Sprite. i. Each step, this sprite needs to move based on its velocity. Note that the x and y variables are declared private in Sprite; even though this class is a sub-class of Sprite, these variables can't be accessed directly. Use the appropriate getter methods. 6. Make a new concrete class BouncingSprite.java that extends MobileSprite. BouncingSprite objects will move around based on their velocity but will also bounce off the edges of the world. a. This type of sprite should bounce off the top, bottom, left, and right edges of the world. Add a method private void bounce () to accomplish this; call it from step. b. Using the "circle.png" image, add a couple BouncingSprite objects to the world at random positions, then run World's main method to ensure everything is working. 7. Make a new concrete class Heavy Sprite.java that extends BouncingSprite. A "heavy" sprite is very similar to a "bouncing" sprite, except that it experiences gravity. Again override the step method, this Page 2 of 4 time utilizing a call to its super-class method, followed by the code to make it fall 0.1 pixel per step. This class' step method should contain only two lines of code. a. Write the code to ensure this class works as expected. Use the "triangle.png" file to represent heavy sprites. Test and fix any mistakes you may have. 8. Write a concrete method in Sprite that takes in another Sprite and returns true if they overlap. The rectangular boundaries of two sprites A and B overlap if A's left edge is left of B's right edge, B's left is left of A's right, A's top is above B's bottom, and B's top is above A's bottom (think about it). a. Test this method thoroughly before proceeding. I mean, it's going to be inherited by a bunch of classes - it better work! Note that even though Heavy Sprite has an image of a triangle, its "hit box" is still a rectangle. 9. Add a new class ControllableSprite.java that extends MobileSprite. ControllableSprites will respond to arrow key presses (by adjusting their velocities) to move around the screen. a. Each step of the simulation, a controllable sprite should be moving left only if the left arrow is pressed, right if the right arrow is pressed, etc. The sprite should not be allowed to move if it is at the edge of the world. b. Std Draw has a static method iskeyFressed that will return true if the given key is currently being pressed. It expects an integer, a 'key code' that corresponds to a key on the keyboard. These key codes are stored in the java.awt.event.KeyEvent class as static final int constants. For example, the 'up' arrow is represented as KeyEvent.VK_UP. C. Add a (school-appropriate) picture of your choosing to the project folder and add a new controllable sprite (using this image) to the simulation. Test using World's main method. i. Sprite's draw method draws the sprite given its height and width (established at creation). You don't need to manually resize images, but it would probably work best not to use tiny or giant images. 10. Given whatever time you have left, implement the following enhancements. For those working ahead, implement as many as you like (this step is quite fun and can become a decent platform for a game). Rather than implementing some number of unrelated behaviors one after the other, you are strongly encouraged to work toward a specific simulation or game and implement the relevant behaviors below. Alive - Modify Sprite so that it keeps track of whether it is alive or dead. Modify World's stepall method to remove any dead sprites from the list. Lifespan - Create a new kind of sprite that dies at a specified time (when it reaches the edge of the screen, or after a specified number of steps). (Completing Alive first will be required for this.) Generator - Create a new kind of sprite that generates other sprites (e.g. fires projectiles) (Complete Lifespan if you want these projectiles to disappear eventually.) Page 3 of 4 Killing - Create a new kind of sprite that kills or dies when it collides with another sprite. (Complete Alive first.) Landing - Create a new kind of sprite that behaves exactly like a HeavySprite, except that it stops falling when it lands on another sprite. (Hopefully it doesn't stop inside the other sprite.) Contains - Write a method in Sprite that takes in an x and y and determines if that point lies inside the boundary of the sprite Clicking - Create a new sprite wherever you click, or (alternatively) interact with (e.g. kill) whatever sprite you clicked on. (To determine what sprite you clicked on, complete Contains first.) Jumping - Change the velocity of a specific sprite whenever a certain key is pressed so that it appears to jump. (Complete Landing first.) Bouncing - Create a new kind of sprite that bounces off other sprites it comes in contact with, by changing direction (according to some rule you devise) without changing speed. The following code segments can help you convert between vx/vy and speed/direction. 1/given speed and direction in degrees, find vx and vy double vx - speed * Math.cos(Math.toRadians (direction)); double vy - speed * Math.sin(Math.toRadians (direction)); 1/given vx and vy, find speed and direction double speed = Math.sqrt (vx * vx + vy vy); //pythagorean theorem double direction - Math.toDegrees (Math.atan2 (vy, vx)); //atan2 is in radians Adapted from the Sprites lab by Dave Feinberg https://sites.google.com/site/feinbergcompsci/home/hcsi/abs/spritelab Page 4 of 4

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 An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago