如何每3秒停车一次,然后继续开车
我对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
派生类中的Override方法。因此,您可以从基类调用方法,然后睡三个秒:
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: