enter image description here我正在做一个处理练习,其中我需要在两条街道之间创建一个交叉口,每个交叉口都有一个红绿灯(总共有4个红绿灯),我将有两种类型的红绿灯:水平街道上的红绿灯和垂直街道上的红绿灯。当垂直街道上的交通灯是黄色或红色时,水平街道上的交通灯将是绿色的,等等。我的问题是,我不能创建这样做的函数,我的想法是创建交通灯,使“灯”不透明,然后通过一个函数降低不透明度,有感觉,他们正在打开和关闭。为了识别哪些交通灯在水平街道上,哪些在垂直街道上,我创建了一个名为type的变量,其中1表示它们在垂直街道上,0表示它们在水平街道上。此时此刻,在我的功能中,我做的好像每个“灯”保持2秒,但我的意图是使红灯保持4秒,绿色和黄灯保持2秒,以便红灯可以保持亮着,而其他灯(黄灯和绿灯)在其他交通灯上亮着,然后它们继续打开和关闭。这是我的代码:第一个选项卡:
final color WHITE = color(255,255,255);
final color GREEN = color(0,255,0);
final color DARK_GREEN = color(0,55,0);
final color RED = color(255,0,0);
final color YELLOW = color(255,255,0);
final color DARK_GREY = color(100,100,100);
final color GREY = color(200,200,200);
final color BLACK = color(0,0,0);
final int W_WIDTH = 1000;
final int W_HEIGHT = 700;
carro c1 = new carro(W_WIDTH/2 - 150,W_HEIGHT/2,150,100,0);
carro c2 = new carro(W_WIDTH/2 + 300,W_HEIGHT/2 + 150,150,100,3.15);
carro c3 = new carro(W_WIDTH/2 + 75,W_HEIGHT/2 + 300,150,100,4.7);
carro c4 = new carro(W_WIDTH/2 + 75, W_HEIGHT/2 - 150,150,100,1.58);
carro carros[] = {c1,c2,c3,c4};
lane s1 = new lane(350, W_HEIGHT/2 + 150,0, RED,W_WIDTH/2 - 150,W_HEIGHT/2,150,100,0,1,150);
lane s2 = new lane(500,W_HEIGHT/2 - 150,-300,RED,W_WIDTH/2 + 300,W_HEIGHT/2 + 150,150,100,3.15,0,150);
lane s3 = new lane(650,W_HEIGHT/2 + 300,300,GREEN,W_WIDTH/2 + 75,W_HEIGHT/2 + 300,150,100,4.7,0,150);
lane s4 = new lane(800,W_HEIGHT/2,3.15,GREEN,W_WIDTH/2 + 75, W_HEIGHT/2 - 150,150,100,1.58,1,150);
lane semaforos[] = {s1,s2,s3,s4};
void settings()
{
size(W_WIDTH, W_HEIGHT);
}
void setup()
{
}
void update()
{
for(int i = 0;i < semaforos.length;i++)
{
semaforos[i].update();
}
}
void draw()
{
update();
noStroke();
background(DARK_GREEN);
fill(DARK_GREY);
rect(0, W_HEIGHT/2, 1000, 150);
fill(DARK_GREY);
rect(W_WIDTH/2,0,150,700);
for(int i = 0; i<carros.length; i++)
{
carros[i].draw();
}
for(int i = 0; i < semaforos.length;i++)
{
semaforos[i].draw();
}
}
我的第二个账单
class carro
{
float x;
float y;
float velocidade_max;
float velocidade_atual;
float angle;
carro(float x,float y, float velocidade_max,float velocidade_atual,float angle)
{
this.x = x;
this.y = y;
this.velocidade_max = velocidade_max;
this.velocidade_atual = velocidade_atual;
this.angle = angle;
}
void draw() {
pushMatrix();
translate(x, y);
rotate(angle); // aplicar o ângulo em radianos
fill(RED);
rect(0, 10, 100, 50);
fill(BLACK);
fill(RED);
rect(101,15,25,40);
fill(BLACK);
rect(70,60,20,5);
fill(BLACK);
rect(15,60,20,5);
fill(BLACK);
rect(70,5,20,5);
fill(BLACK);
rect(15,5,20,5);
popMatrix();
}
}
我的第三个选项卡,我试图打开灯:
class lane extends carro {
float positionx;
float positiony;
float angleS;
color c; // cor inicial do semáforo
float tipo; //vert ou hor, 1 horizontal o vertical
float transparencia;
int startTime;
float alphav = 150; //transparencia vermelho
float alphaa = 150; //transparencia amarelo
float alphaver = 100; //transparencia verde
int activeLight;
lane(float positionx, float positiony, float angleS, color c, float tipo, float transparencia, float x, float y, float velocidade_max, float velocidade_atual, float angle) {
super(x, y, velocidade_max, velocidade_atual, angle);
this.positionx = positionx;
this.positiony = positiony;
this.angleS = angleS;
this.c = c;
this.tipo = tipo;
this.transparencia = transparencia;
this.activeLight = 0; // começa com o vermelho aceso
this.startTime = millis(); // armazena o tempo de início
}
void update() {
int elapsedTime = millis() - this.startTime; // calcula o tempo decorrido
int currentSecond = elapsedTime / 1000; // converte para segundos
float lightIndex = currentSecond % 6; // tempo total do ciclo é 6 segundos
if (this.tipo == 1) {
if (lightIndex < 2) {
this.alphav = 500;
this.alphaa = 150;
this.alphaver = 150;
}
else if (lightIndex < 4 && lightIndex > 2) {
this.alphav = 500;
this.alphaa = 150;
this.alphaver = 150;
}
else if (lightIndex <= 6 && lightIndex > 4) {
this.alphav = 150;
this.alphaa = 150;
this.alphaver = 500;
}
}
else if (this.tipo == 2) {
if (lightIndex < 2) {
this.alphav = 500;
this.alphaa = 150;
this.alphaver = 150;
}
else if (lightIndex < 4 && lightIndex > 2) {
this.alphav = 150;
this.alphaa = 500;
this.alphaver = 150;
}
else if (lightIndex < 6 && lightIndex > 4) {
this.alphav = 150;
this.alphaa = 150;
this.alphaver = 500;
}
}
}
void draw() {
pushMatrix();
translate(positionx, positiony);
rotate(angleS);
fill(BLACK);
rect(0, 0, 150, 50);
fill(RED, alphav);
circle(120, 25, 40);
fill(GREEN, alphaver);
circle(75, 25, 40);
fill(YELLOW, alphaa);
circle(30, 25, 40);
fill(BLACK);
rect(-40, 14, 40, 20);
popMatrix();
}
}
对不起,如果代码不遵循惯例,但我必须尝试用我的老师决定的“结构”来做。..如果这太简单了,我很抱歉,我上周开始做这个,只是做了一个处理练习。有人能帮我吗
2条答案
按热度按时间uxh89sit1#
你可以使用Java中的Timer类。util,但它涉及的任务,这可能有点过头了。
最简单的方法有一些延迟几秒钟的每个交通灯阶段可能是to use Thread.sleep()这不是最干净的解决方案,但我认为这将是令人满意的你试图实现。
bvn4nwqk2#
我让所有的东西都保持计算时间的方式,为了让交通灯的灯光不同,我添加了一个变量,告诉你当你启动程序时,交通灯开始的颜色,然后用一个简单的if else,我就可以做我想做的事情。下面是更改的代码: