47 lines
1 KiB
Python
47 lines
1 KiB
Python
import random
|
|
import pygame
|
|
import sys
|
|
|
|
pygame.init()
|
|
|
|
# Window Setup
|
|
WIDTH, HEIGHT = 1000, 700
|
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
pygame.display.set_caption("Traffic Simulation")
|
|
|
|
FPS = 60
|
|
clock = pygame.time.Clock()
|
|
|
|
# Colours
|
|
ROAD = (60, 60, 60)
|
|
CAR = (200, 40, 40)
|
|
GREEN = (0, 200, 0)
|
|
YELLOW= (255, 180, 0)
|
|
RED = (200, 0, 0)
|
|
WHITE = (255, 255, 255)
|
|
UI_BG = (30, 30, 30)
|
|
|
|
#Classes
|
|
|
|
#Traffic Light Class
|
|
class TrafficLight:
|
|
def __init__(self, x, y):
|
|
self.x = x
|
|
self.y = y
|
|
self.states = ["green", "yellow", "red"]
|
|
self.current = "green"
|
|
self.timer = 0
|
|
self.cycle_time = 120
|
|
|
|
def update(self):
|
|
self.timer += 1
|
|
if self.timer > self.cycle_time:
|
|
self.timer = 0
|
|
idx = self.states.index(self.current)
|
|
self.current = self.states[(idx +1) % 3]
|
|
|
|
def draw(self, surf):
|
|
colour = GREEN if self.current == "green" else YELLOW if self.current == "yellow" else RED
|
|
pygame.draw.circle(surf, colour, (self.x, self.y), 12)
|
|
|
|
|