当前位置: 首页 > 知识库问答 >
问题:

在多个类之间更改布尔值

关宏毅
2023-03-14

因此,我的任务是制作多个“动物”类,并在空白绘图面板上运行它们。每只动物都以特定的方式移动,当它们与不同的动物重叠时,“入侵”的动物获胜,而另一只动物被移走。我的老师告诉我使用一个布尔值,如果两个动物在相同的坐标上交叉,则返回true(动物死亡),而在所有其他情况下返回false。

我在动物类上设置了一个自动返回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;
}


}

动物模拟器:

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个就足够完成一次测试了。再一次,很抱歉发了这么长的帖子。非常感谢。

共有1个答案

郎经纶
2023-03-14

Issameloc需要两种动物来完成它的工作:

boolean death = animals[i].isSameLoc(animals[a]);
 类似资料:
  • 我有一个学校作业,要做一个程序,结果要么正确,要么错误。这关系到一年是否是闰年。目前的问题是,我使用的是公共静态布尔值,而不是公共布尔值。这是我的代码: 现在int年是2000年,但是规则是这样的:闰年是一年,可以除以4,除非这一年是一个新世纪的开始(1700, 1800, 1900.....)。所以即使你可以把1900除以4,你也不能把它除以400,所以这是错误的。所以再一次问一个问题:我需要做

  • 问题内容: 我有一个MPClient和MultiplayerMatch类。MultiplayerMatch在其构造函数中创建一个MPClient可运行线程。 为了避免数据溢出,我在MultiplayerMatch中有一个名为“ moved”的布尔值,当播放器移动时,它会变为true。 在updateMatch方法中,如果有任何播放器移动,则“ moved”变为true,这允许MPClient输入i

  • 问题内容: 我有一个线程,它等待布尔值更改,如下所示: 这不是我首选的方法,因为这会导致大量CPU消耗。 有什么方法可以阻止线程,直到布尔值更改其状态? 问题答案: 这不是我首选的方法,因为这会导致大量CPU消耗。 如果这实际上是您的工作代码,则只需保留该代码即可。每秒检查一次布尔值不会导致可测量的CPU负载。没有任何。 真正的问题是检查该值的线程可能由于缓存而没有看到任意长时间的更改。为了确保该

  • 可以在多个类之间传递数据吗? 我试图实现的是“一个类设置某个类的值,另一个类访问它”(java初学者)

  • 我在flutter中创建了一个应用程序,在其中我把raisedButton的东西放在任何地方。我想在NeuMorphicButton中全部更改它们。有没有一种方法可以将所有raisedbutton改为NeummorphicButton而不需要一个接一个地编辑?谢谢你。

  • 您好,我正在开发一个游戏和碰撞检测工作。目前,我的方法不允许动态添加元素,正如您所看到的,我正在手动添加冲突节点()。这是我的密码: 我想根据列表中冲突元素的数量动态构建一个。我想以一种方式重新构建它,使其符合