【发布时间】:2021-04-12 16:56:21
【问题描述】:
对于我们的学校作业,我们的任务是
更改教程中的两车对象程序,以便每次 汽车相互通过,汽车减速到其速度的 33% 在两辆车的中心之间画一条橙色的垂直线 表示驾驶员正在进行眼神交流。
但是,每当我尝试使用 if-else 条件或其他方法更改速度时,速度更改就会变成永久性的。其他时候,速度根本没有变化。我只做了橙色的垂直线。
这是我目前拥有的图片: Screenshot of the program
这是它应该做的: Video
代码如下:
// Example: Two Car objects
Car myCar1;
Car myCar2; // Two objects!
void setup() {
size(200,200);
// Parameters go inside the parentheses when the object is constructed.
myCar1 = new Car(color(255,0,0),0,100,2);
myCar2 = new Car(color(0,0,255),0,10,1);
}
void draw() {
background(255);
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
stroke(255,128,0);
line(myCar1.xpos,myCar1.ypos,myCar2.xpos,myCar2.ypos);
}
// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
float xspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
}
非常感谢任何帮助。提前致谢。
[1]: https://i.stack.imgur.com/q0hAX.png
[2]: https://youtu.be/dIGr9RprfoE
【问题讨论】:
标签: java performance animation processing