所以,我的任务是创建多个“动物”类并在空白绘图面板上运行它们。每只动物以特定的方式移动,当它们与不同的动物重叠时,“入侵”的动物获胜而另一只动物被移除。如果两只动物在同一坐标上交叉并在所有其他场合返回假,我的老师告诉我使用一个返回true(动物死亡)的布尔值。
我在Animal类上设置了一个自动返回false的布尔方法“isSameLoc()”,但是当动物在主客户端类中重叠时,我不知道如何将值切换为true。任何建议都表示赞赏。
我也提前道歉,因为我要在这里发布6个不同的课程,所以这会有点长。我不知道是否有更简单的方法来做到这一点,再次,我很抱歉。
动物类:
import java.awt.Color;
import java.awt.Point;
public abstract class Animal {
private String type;
public Animal(String type, String name, Point location, Color color) {
super();
this.type = type;
this.name = name;
this.location = location;
this.color = color;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
private String name;
private Point location;
private Color color;
private boolean sameLoc = false;
public boolean isSameLoc() {
return sameLoc;
}
public abstract String toString();
public abstract void move ();
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
}
AnimalSimulator:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
public class AnimalSimulator {
public static void main(String[] args) {
DrawingPanel forest = new DrawingPanel(720, 640);
Graphics2D pen = forest.getGraphics();
Animal [] animals = new Animal[15];
for (int i = 0; i < animals.length;i++){
int selection = (int) (Math.random()*4+1);
int randX = (int) (Math.random()*720+1);
int randY = (int) (Math.random()*640+1);
switch(selection){
case 1:
Bird b = new Bird ("Avian", "Birdie", new Point(randX, randY), Color.BLUE);
animals[i] = b;
break;
case 2:
Frog f = new Frog ("Amphibian", "Frogger", new Point(randX, randY), Color.GREEN);
animals[i] = f;
break;
case 3:
Mouse m = new Mouse ("Rodent", "Mickey", new Point(randX, randY), Color.DARK_GRAY);
animals[i] = m;
break;
case 4:
Rabbit r = new Rabbit ("Rodent", "Bugs", new Point(randX, randY), Color.ORANGE);
animals[i] = r;
break;
//case 5:
//Turtle t = new Turtle("Reptile", "Tortoise", new Point(randX, randY), Color.CYAN);
//animals[i] = t;
//break;
//case 6:
//Snake s = new Snake("Slither", "Kaa", new Point(randX, randY), Color.RED);
//animals[i] = s;
//break;
}
}
for (int time = 1; time <= 100; time++){
for (int i = 0; i < animals.length; i++){
pen.setColor(animals[i].getColor());
pen.drawString(animals[i].toString(), animals[i].getLocation().x, animals[i].getLocation().y);
animals[i].move();
for (int a = 0; a< animals.length; a++){
boolean death = animals[i].isSameLoc();
if(a!= i){
if (animals[i].equals(animals[a])){
death = true;
if(death){
animals[a] = null;
}
}
}
}
}
forest.sleep(50);
}
}
}
鸟类:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
public class Bird extends Animal{
public Bird(String type, String name, Point location, Color color) {
super(type, name, location, color);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
if (isSameLoc()){
return null;
}else{
return "B";
}
}
@Override
/*
* (non-Javadoc)
* @see Animal#move()
* this, I had no real problems with, just random directions but
* I think it was supposed to be alot more spread out.
*/
public void move() {
int newX = getLocation().x+3;
int negX = getLocation().x-3;
int newY = getLocation().y+3;
int negY = getLocation().y-3;
int direct = (int) (Math.random()*4+1);
if (direct == 1){
setLocation(new Point(getLocation().x, newY));
}else if (direct == 2){
setLocation(new Point(getLocation().x, negY));
}else if (direct == 3){
setLocation(new Point(newX, getLocation().y));
}else if (direct == 4){
setLocation(new Point(negX, getLocation().y));
}
}
}
青蛙班:
import java.awt.Color;
import java.awt.Point;
public class Frog extends Animal {
private int steps = 0;
public Frog(String type, String name, Point location, Color color) {
super(type, name, location, color);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
if (isSameLoc()){
return null;
}else{
return "F";
}
}
@Override
/*
*For this, I couldn't tell if it was moving in the
*right pattern. I knew it was moving randomly, but I
*don't think it was moving in the right increments.
*/
public void move() {
int newX = getLocation().x+3;
int negX = getLocation().x-3;
int newY = getLocation().y+3;
int negY = getLocation().y-3;
int direct = (int) (Math.random()*4+1);
if (steps < 3){
if (direct == 1){
setLocation(new Point(getLocation().x, newY));
}else if (direct == 2){
setLocation(new Point(getLocation().x, negY));
}else if (direct == 3){
setLocation(new Point(newX, getLocation().y));
}else if (direct == 4){
setLocation(new Point(negX, getLocation().y));
}
steps++;
}
if (steps >= 3){
steps = 0;
}
}
}
鼠标类:
import java.awt.Color;
import java.awt.Point;
public class Mouse extends Animal {
public Mouse(String type, String name, Point location, Color color) {
super(type, name, location, color);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
if (isSameLoc()){
return null;
}else{
return "M";
}
}
@Override
public void move() {
int newX = getLocation().x+3;
int negX = getLocation().x-3;
int newY = getLocation().y+3;
int negY = getLocation().y-3;
setLocation(new Point(negX, negY));
}
}
终于上课了:
import java.awt.Color;
import java.awt.Point;
public class Rabbit extends Animal {
private int steps = 0;
public Rabbit(String type, String name, Point location, Color color) {
super(type, name, location, color);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
if (isSameLoc()){
return null;
}else{
return "R";
}
}
@Override
public void move() {
int newX = getLocation().x+3;
int negX = getLocation().x-3;
int newY = getLocation().y+3;
int negY = getLocation().y-3;
if (steps < 2){
setLocation(new Point(getLocation().x, newY));
}else if(steps < 4){
setLocation(new Point(newX, getLocation().y));
}else if(steps < 6){
setLocation(new Point(getLocation().x, negY));
}
steps++;
if(steps >= 6){
steps = 0;
}
}
}
实际上有更多的动物类,但我认为4足以完成测试。再次,抱歉这篇长篇文章。谢谢。
答案 0 :(得分:0)
Issameloc需要两只动物来完成它的工作:
boolean death = animals[i].isSameLoc(animals[a]);