Processing十字路口交通灯时序控制功能实现求助
Processing十字路口交通灯时序逻辑修正求助
我正在做一个Processing十字路口项目,需要实现4个交通灯的联动控制:
- 纵向街道的2个交通灯:红灯亮4秒,绿灯、黄灯各亮2秒
- 横向街道的2个交通灯:状态与纵向相反,即纵向红灯/黄灯亮时,横向绿灯亮
我计划通过调整灯的透明度模拟亮灭效果,用变量tipo区分交通灯类型(1为纵向,0为横向),但当前update函数的逻辑无法实现预期时序,附上项目代码,请求帮助修正。
主程序代码
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); // 应用弧度角度 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; // 交通灯初始颜色 float tipo; // 1为纵向,0为横向 float transparencia; int startTime; float alphav = 50; // 灯暗状态的透明度 float alphaa = 50; float alphaver = 50; int activeLight; // 修正构造函数参数顺序,匹配实例化时的参数传递 lane(float positionx, float positiony, float angleS, color c, float x, float y, float velocidade_max, float velocidade_atual, float angle, float tipo, float transparencia) { 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; // 初始红灯亮 this.startTime = millis(); // 记录初始时间 } void update() { int elapsedTime = millis() - this.startTime; int currentSecond = elapsedTime / 1000; int cyclePhase = currentSecond % 8; // 总周期为8秒(4红+2绿+2黄) // 先将所有灯重置为暗状态 this.alphav = 50; this.alphaa = 50; this.alphaver = 50; if (this.tipo == 1) { // 纵向交通灯逻辑 if (cyclePhase < 4) { // 0-4秒:红灯亮 this.alphav = 255; } else if (cyclePhase < 6) { // 4-6秒:绿灯亮 this.alphaver = 255; } else { // 6-8秒:黄灯亮 this.alphaa = 255; } } else if (this.tipo == 0) { // 横向交通灯逻辑:纵向红/黄灯亮时横向绿灯亮,纵向绿灯亮时横向红灯亮 if (cyclePhase < 4 || cyclePhase >= 6) { // 0-4秒(纵向红)、6-8秒(纵向黄):横向绿灯亮 this.alphaver = 255; } else { // 4-6秒(纵向绿):横向红灯亮 this.alphav = 255; } } } 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(); } }
关键修正点说明
- 周期调整:将总周期从6秒改为8秒,完全匹配需求中4秒红灯、2秒绿灯、2秒黄灯的时间设置
- 透明度范围修正:Processing中alpha值合法范围是0-255,用255表示灯完全亮起,50表示灯暗,替换原代码中超出范围的500值
- 构造函数参数修正:调整
lane类构造函数的参数顺序,匹配实例化时的参数传递,避免tipo变量赋值错误 - 时序逻辑修正:
- 纵向灯:严格按照0-4秒红灯、4-6秒绿灯、6-8秒黄灯的周期循环
- 横向灯:完全遵循“纵向红/黄灯亮时横向绿灯亮”的规则,仅在纵向绿灯亮起的2秒内切换为红灯
- 状态重置优化:每次更新时先将所有灯设为暗状态,避免出现多灯同时亮的错误
内容的提问来源于stack exchange,提问作者Alexandre Santos
相关产品推荐
相关产品推荐

