extremely struggling to make them queue at lights

Signed-off-by: casualhonk <casualhonk@Tomoe.nya>
This commit is contained in:
casualhonk 2026-02-20 22:16:15 +11:00
parent e2ffea00e0
commit d455b8230f

32
main.py
View file

@ -52,14 +52,25 @@ class Vehicle:
self.speed = speed
self.wait = 0
def update(self, lights):
#check if light ahead
def update(self, lights, vehicles):
#check if car/light ahead
for other in vehicles:
if other is self:
continue
if abs(self.x - other.x) < 5:
if other.y > self.y:
dist = other.y - self.y
if dist < 60:
return # too close back up
for light in lights:
if abs(self.x - light.x) < 10 and self.y < light.y < self.y + 40:
if light.current != "green":
self.wait += 1
return
if abs(self.x - light.x) < 10:
if self.y < light.y < self.y + 40:
if light.current != "green":
return
self.y += self.speed
@ -108,7 +119,7 @@ while running:
if event.type == pygame.MOUSEBUTTONDOWN:
mx, my = pygame.mouse.get_pos()
if placing_mode == "remove" and event.button == 3:
if placing_mode == "remove" and event.button == 1:
DEL_RADIUS = 25
@ -163,11 +174,16 @@ while running:
light.update()
light.draw(screen)
vehicles.sort(key=lambda v: v.y)
#update vehicles
for v in vehicles:
v.update(lights)
v.update(lights, vehicles)
v.draw(screen)
#remove dem
vehicles = [v for v in vehicles if v.y < HEIGHT + 50]
#draw intersections
for x, y in intersections:
pygame.draw.rect(screen, WHITE, (x - 20, y - 20, 40, 40), 2)