In: Computer Science
Create the following class to represent the sprite objects in the game.
Sprite |
|
+ Sprite (String); + Sprite (String, int, int); + setName (String): void + setX(int): void + setY(int): void + getName (): String + getX (): int + getY (): int + collide (Sprite): boolean + toString (): String |
Create ten (10) game objects as Sprite type and stored in an array. All the games objects location (x & y) must be set in range 1 to 800 randomly.
The collide method is a method to test the collision between the current sprite with another sprite (from parameter). This method will check the distance in between two sprites based on the current x and y location using Pythagoras theorem. If the distance of 2 objects is less than 10, then the method will return true. Otherwise false will be returned.
Create a driver class called TestSprite which will:
Here are the 2 classes - Sprite.java and TestSprite.java ================================================================= public class Sprite { private String name; private int x; private int y; public Sprite(String name) { this.name = name; x = y = 0; } public Sprite(String name, int x, int y) { this.name = name; this.x = x; this.y = y; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public String getName() { return name; } public int getX() { return x; } public int getY() { return y; } public boolean collide(Sprite aSprite) { int distance = (x - aSprite.getX()) * (x - aSprite.getX()) + (y - aSprite.getY()) * (x - aSprite.getY()); return distance <= 100; } public String toString() { return "Sprite Name: " + name + ", Location (" + x + "," + y + ")"; } }
======================================================================
import java.util.Random;
public class TestSprite { public static void main(String[] args) { Sprite[] sprites = new Sprite[10]; Random r = new Random(); sprites[0] = new Sprite("Mercury", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[1] = new Sprite("Venus", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[2] = new Sprite("Earth", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[3] = new Sprite("Mars", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[4] = new Sprite("Jupiter", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[5] = new Sprite("Saturn", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[6] = new Sprite("Uranus", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[7] = new Sprite("Neptune", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[8] = new Sprite("Pluto", r.nextInt(800) + 1, r.nextInt(800) + 1); sprites[9] = new Sprite("Moon", r.nextInt(800) + 1, r.nextInt(800) + 1); System.out.println("Sprite Details: "); for (int i = 0; i < sprites.length; i++) { System.out.println("Name: " + sprites[i].getName() + " X: " + sprites[i].getX() + ", Y: " + sprites[i].getY()); } System.out.println("\nCollision Status:"); for (int i = 0; i < sprites.length - 1; i++) { for (int j = i + 1; j < sprites.length; j++) { if (sprites[i].collide(sprites[j])) { System.out.println(sprites[i] + " collides with " + sprites[j]); } } } } }
=======================================================================