如何在给定处理中的某个属性的情况下在 x 秒内执行操作

问题描述 投票:0回答:2

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();
  }
}

抱歉,如果代码不遵循约定,但我必须尝试使用我的老师决定的“结构”来完成……抱歉,如果这太简单了,我上周就开始了,只是做了一个处理练习...有人可以帮助我吗?

java processing
2个回答
0
投票

您可以使用 java.util 中的 Timer 类,但它涉及的任务可能有点过头了。
为每个红绿灯阶段延迟几秒钟的最简单方法可能是使用 Thread.sleep() 这不是最干净的解决方案,但我认为它会满足您想要实现的目标。


0
投票

我把所有的东西都按照它计算时间的方式保留了下来,为了让交通灯的灯不同,我添加了一个变量,告诉你当你启动程序时交通灯开始的颜色是什么,如果不是这样我可以做什么我想了。以下是更改的代码:

    void update()
{
  float elapsedTime = millis() - this.startTime; // calcula o tempo decorrido
  float currentSecond = elapsedTime / 1000; // converte para segundos

  float cycleTime = timev + timea + timever; // tempo total do ciclo
  float lightIndex = currentSecond % cycleTime; // calcula o índice da luz acesa

  float distanciax = W_WIDTH/2 - 120;
  float distanciay = W_HEIGHT/2 + 300;
  if (cor_inicio == GREEN) {
    if (lightIndex < this.timever) {
      // vermelho
      this.alphav = 150;
      this.alphaa = 150;
      this.alphaver = 500;
      this.activeLight = 0;
      carros[0].x += velocidade_atual; // move o primeiro carro da esquerda para a direita
      carros[0].wrapAroundScreenx();
      carros[3].x -= velocidade_atual;
      carros[3].wrapAroundScreenx();
    } else if (lightIndex < this.timever + this.timea) {
      // amarelo
      this.alphav = 150;
      this.alphaa = 500;
      this.alphaver = 150;
      this.activeLight = 1;
  if (abs(carros[0].x - distanciax) > 10) {
    carros[0].x += velocidade_atual;
    carros[0].wrapAroundScreenx();
    carros[3].x -= velocidade_atual;
    carros[3].wrapAroundScreenx();
  }
    } else {
      // verde
      this.alphav = 500;
      this.alphaa = 150;
      this.alphaver = 150;
      this.activeLight = 2;
      if(abs(carros[3].x - distanciax) < 10 && carros[3].x < distanciax)
      {
      carros[0].x += velocidade_atual; // move o primeiro carro da esquerda para a direita
      carros[0].wrapAroundScreenx();
      carros[3].x += velocidade_atual;
      carros[3].wrapAroundScreenx();
      }
    }
  } else if (cor_inicio == RED) {
    if (lightIndex < this.timev) {
      // vermelho
      this.alphav = 500;
      this.alphaa = 150;
      this.alphaver = 150;
      this.activeLight = 0;
      if (abs(carros[1].y - distanciay) > 10 && carros[1].y < distanciay) {
       carros[1].y -= velocidade_atual;
       carros[1].wrapAroundScreenx();
       carros[2].y += velocidade_atual;
       carros[2].wrapAroundScreenx();
      }

    } else if (lightIndex < this.timev + this.timea) {
      // amarelo
      this.alphav = 150;
      this.alphaa = 150;
      this.alphaver = 500;
      this.activeLight = 1;
      carros[1].y -= velocidade_atual;
      carros[1].wrapAroundScreenx();
      carros[2].y += velocidade_atual;
      carros[2].wrapAroundScreenx();
    } else {
      // verde
      this.alphav = 150;
      this.alphaa = 500;
      this.alphaver = 150;
      this.activeLight = 2;
  if (abs(carros[1].y - distanciay) > 10 && carros[1].y < distanciay) {
  carros[1].y -= velocidade_atual;
  carros[1].wrapAroundScreenx();
  carros[2].y += velocidade_atual;
  carros[2].wrapAroundScreenx();
}
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.