如何每3秒停车一次,然后继续开车

发布于 2025-01-27 17:25:35 字数 2386 浏览 2 评论 0原文

我对Java和OOP有点新鲜。我的问题是: 我有一个带有驱动方法的称为汽车的抽象类。它还有另一个子类,称为AntiqueCar。古董汽车在驾驶3秒钟时停止工作。因此,它将在3秒钟后停止,但然后继续再次开车。 (我认为我可以在Antiquecar类中覆盖驱动器方法)我尝试了很多事情,但是我只是设法停止了古董汽车,但不能再用它。

这是驱动器方法:

public void drive(Dimension2D gameBoardSize) {
        if (this.crunched) {
            return;
        }
        double maxX = gameBoardSize.getWidth();
        double maxY = gameBoardSize.getHeight();
        // calculate delta between old coordinates and new ones based on speed and
        // direction
        double deltaX = this.speed * Math.sin(Math.toRadians(this.direction));
        double deltaY = this.speed * Math.cos(Math.toRadians(this.direction));
        double newX = this.position.getX() + deltaX;
        double newY = this.position.getY() + deltaY;

        // calculate position in case the boarder of the game board has been reached
        if (newX < 0) {
            newX = -newX;
            this.direction = MAX_ANGLE - this.direction;
        } else if (newX + this.size.getWidth() > maxX) {
            newX = 2 * maxX - newX - 2 * this.size.getWidth();
            this.direction = MAX_ANGLE - this.direction;
        }

        if (newY < 0) {
            newY = -newY;
            this.direction = HALF_ANGLE - this.direction;
            if (this.direction < 0) {
                this.direction = MAX_ANGLE + this.direction;
            }
        } else if (newY + this.size.getHeight() > maxY) {
            newY = 2 * maxY - newY - 2 * this.size.getHeight();
            this.direction = HALF_ANGLE - this.direction;
            if (this.direction < 0) {
                this.direction = MAX_ANGLE + this.direction;
            }
        }
        // set coordinates
        this.position = new Point2D(newX, newY);
    }

那是古董类:

public class AntiqueCar extends Car {

    private static final String ANTIQUE_CAR_IMAGE_FILE = "antiquecar.gif";
    // this car is middle-speeded. Not so fast, not so slow.
    private static final int MIN_SPEED_FAST_CAR = 2;
    private static final int MAX_SPEED_FAST_CAR = 7;

    public AntiqueCar(Dimension2D gameBoardSize) {
        super(gameBoardSize);
        setMinSpeed(MIN_SPEED_FAST_CAR);
        setMaxSpeed(MAX_SPEED_FAST_CAR);
        setRandomSpeed();
        setIconLocation(ANTIQUE_CAR_IMAGE_FILE);

    }

感谢任何帮助,谢谢!

I am a bit new to Java and OOP. My question is:
I have an abstract class called Car with drive method. There is another subclass of it, called AntiqueCar. Antique car stops working when it drives for 3 seconds. So it will stop after 3 seconds but then continue driving again. (I think that I could override drive method in AntiqueCar class) I tried many things, but I just could manage to stop the antique car but could not make it work again.

Here is drive method:

public void drive(Dimension2D gameBoardSize) {
        if (this.crunched) {
            return;
        }
        double maxX = gameBoardSize.getWidth();
        double maxY = gameBoardSize.getHeight();
        // calculate delta between old coordinates and new ones based on speed and
        // direction
        double deltaX = this.speed * Math.sin(Math.toRadians(this.direction));
        double deltaY = this.speed * Math.cos(Math.toRadians(this.direction));
        double newX = this.position.getX() + deltaX;
        double newY = this.position.getY() + deltaY;

        // calculate position in case the boarder of the game board has been reached
        if (newX < 0) {
            newX = -newX;
            this.direction = MAX_ANGLE - this.direction;
        } else if (newX + this.size.getWidth() > maxX) {
            newX = 2 * maxX - newX - 2 * this.size.getWidth();
            this.direction = MAX_ANGLE - this.direction;
        }

        if (newY < 0) {
            newY = -newY;
            this.direction = HALF_ANGLE - this.direction;
            if (this.direction < 0) {
                this.direction = MAX_ANGLE + this.direction;
            }
        } else if (newY + this.size.getHeight() > maxY) {
            newY = 2 * maxY - newY - 2 * this.size.getHeight();
            this.direction = HALF_ANGLE - this.direction;
            if (this.direction < 0) {
                this.direction = MAX_ANGLE + this.direction;
            }
        }
        // set coordinates
        this.position = new Point2D(newX, newY);
    }

Then here is the AntiqueCar class:

public class AntiqueCar extends Car {

    private static final String ANTIQUE_CAR_IMAGE_FILE = "antiquecar.gif";
    // this car is middle-speeded. Not so fast, not so slow.
    private static final int MIN_SPEED_FAST_CAR = 2;
    private static final int MAX_SPEED_FAST_CAR = 7;

    public AntiqueCar(Dimension2D gameBoardSize) {
        super(gameBoardSize);
        setMinSpeed(MIN_SPEED_FAST_CAR);
        setMaxSpeed(MAX_SPEED_FAST_CAR);
        setRandomSpeed();
        setIconLocation(ANTIQUE_CAR_IMAGE_FILE);

    }

Appreciate any kind of help, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2025-02-03 17:25:35

派生类中的Override方法。因此,您可以从基类调用方法,然后睡三个秒

public class AntiqueCar extends Car {

    public void drive(Dimension2D gameBoardSize) {
        super.drive(gameVoardSize);
        TimeUnit.SECONDS.sleep(1);    
    }

}

It is possible to override method in derived class. So you can call method from the base class and then just sleep it for three seconds:

public class AntiqueCar extends Car {

    public void drive(Dimension2D gameBoardSize) {
        super.drive(gameVoardSize);
        TimeUnit.SECONDS.sleep(1);    
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文