【问题标题】:How can I stop these cars from hitting each other?我怎样才能阻止这些汽车相互碰撞?
【发布时间】:2020-03-30 23:13:20
【问题描述】:

我正在做一个简单的交通模拟练习,因为我对编程还很陌生。到目前为止,我已经完成了我想做的所有工作,但是当一辆车停在红灯处时,后面的车就会撞上它并相互堆叠。

这里是我调用汽车的通用代码:

private ArrayList<CarsRight> carsright = new ArrayList<CarsRight>();

public IntersectionSimulation() {
    for (int i = 0; i < 4; i++) {
        carsright.add(new CarsRight(i * -250));
    }
}

public void paint(Graphics g) {
    for (CarsRight cr : carsright) {
        Graphics2D g2d = (Graphics2D) g;
        cr.paint(g2d);
        tl.paint(g2d, mkl.b);   //tl is the traffic light (either green or red)
                                //mkl.b checks whether or not the spacebar is being pressed and changes the light accordingly
    }
}

public void move() {
    for (CarsRight cr : carsright) {
        cr.move(mkl.b);     //mkl.b checks whether or not the spacebar is being pressed and makes the cars move or stop accordingly
    }
)

这里基本上是我的CarsRight.java 类:

public class CarsRight extends JPanel {
    int x;
    private int y = 330;
    private int a;
    private int speed = 20;
    int rgb = colorCode();
    private int height = 40;
    private int length = 100;

    public CarsRight(int i) {
        this.x = i;
    }

    void move(boolean b) {
        if (b == true) {
            x++;
            a = speed;
            x = x + a;
        }   //move if the light is green
        else {
            if (x > 300) {
                x++;
                a = speed;
                x = x + a;
            }
            else if (x < 250) {
                x++;
                a = speed;
                x = x + a;
            }
        }   //if the car is before the light, move up to the light, and if the car is past the light, keep going
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paint(g2d);

        for (int d = 0; d < 20; d++) {
            if (rgb == 1) {
                Color c = new Color(255, 255, 255);
                g2d.setColor(c);
            } else if (rgb == 2) {
                Color c = new Color(0, 0, 0);
                g2d.setColor(c);
            } else if (rgb == 3) {
                Color c = new Color(179, 179, 179);
                g2d.setColor(c);
            } else if (rgb == 4) {
                Color c = new Color(187, 10, 48);
                g2d.setColor(c);
            } else {
                Color c = new Color(182, 177, 169);
                g2d.setColor(c);
            }       //random color generator for car

            g2d.fillRoundRect(x, y, length, height, 10, 20);    //prints car
        }
    }
}

再一次,我不知道如何处理这个:/

【问题讨论】:

  • 我认为是 else if (x &lt; 250) 造成了这种情况。也许应该是else if (x &gt;= 100 &amp;&amp; x &lt; 250)我不太确定下限,但没有它,后面的所有汽车也会被考虑并且它们会移动。
  • 没用;它只是在 100 点而不是 250 点之前停止了汽车,它们仍然堆叠在一起。
  • 你能通过提供存储库链接来分享完整的代码吗?我可以运行它并找出问题所在。
  • 我已将其打包到 Google Drive 上的 zip 中:drive.google.com/file/d/1AawzoJxrpJp5L5_SshYGd4mMPZ73gX8Q/…

标签: java oop arraylist graphics2d


【解决方案1】:

我查看了您的代码并能够解决您的问题。您需要汽车之间的相对距离,否则它们会像现在这样堆叠起来。

我对@9​​87654321@ 流进行了更改。您可以对其他汽车进行类似的更改。

MoveForLoopRightIntersectionSimulation 中的类:

public class MoveForLoopRight extends IntersectionSimulation {
        public void run() {
            // It is important to know the relative distance between each car. If not, your cars would stack on each other.
            int distance = 0;
            for (int i = 0; i< carsright.size(); i++) {
                CarsRight car = carsright.get(i);
                if (car.getX() <= 300) { // If they are before signal, move them till the line maintaining the distance
                    car.move(mkl.b, distance); // move method needs distance between cars
                    distance = distance + car.getLength() + 50; // 50 is the gap between each car
                } else {
                    car.move(mkl.b,  0); // these cars crossed the signal. They can move freely and don't need any distance
                }
            }
        }
    }

CarsRight.java 变化:

@Override
public int getX() {
    return x;
}

public int getLength() {
    return length;
}

void move(boolean b, int distance) {
    if (b == true) {
        x++;
        a = speed;
        x = x + a;
    }
    else {
        if (x > 300) {
            x++;
            a = speed;
            x = x + a;
        }
        else if (x < (250 - distance)) {
            x++;
            a = speed;
            x = x + a;
        }
    }
}

让我知道进展如何。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 2020-08-09
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多