import pygame import sys pygame.init() pygame.font.init() #SETUP WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Traffic Controller") #CONSTANTS FPS = 60 #CLOCK clock = pygame.time.Clock() #COLOURS ROAD_COLOUR = (50, 50, 50) LINE_COLOUR = (255, 255, 0) CAR_COLOUR = (200, 0, 0) BG_COLOUR = (30, 150, 30) #CAR car_width = 40 car_height = 70 car_x = WIDTH // 2 - car_width // 2 car_y = HEIGHT - car_height - 20 car_speed = 4 lane_speed = 5 #LANE lane_y = 0 #LOOP running = True while running: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() #score/timer font = pygame.font.SysFont(None, 48) start_time = 0 current_time = pygame.time.get_ticks() elapsed_time = (current_time - start_time) // 1000 timer_text = font.render(f"Time: {elapsed_time}s", True, (255, 255, 255)) # Render the text screen.blit(timer_text, (100, 100)) #INPUT keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] or keys[pygame.K_a]: car_x -= car_speed if keys[pygame.K_RIGHT] or keys[pygame.K_d]: car_x += car_speed if keys[pygame.K_UP] or keys[pygame.K_w]: car_y -= car_speed if keys[pygame.K_DOWN] or keys[pygame.K_s]: car_y += car_speed #LANE SPEED lane_y += lane_speed if lane_y > HEIGHT: lane_y = 0 #BG screen.fill(BG_COLOUR) #ROAD pygame.draw.rect(screen, ROAD_COLOUR, (200, 0, 400, HEIGHT)) #LANE DIVIDER for i in range(-1, 10): pygame.draw.rect(screen, LINE_COLOUR, (WIDTH//2 - 5, lane_y + i * 80, 10, 40)) #CAR pygame.draw.rect(screen, CAR_COLOUR, (car_x, car_y, car_width, car_height)) pygame.display.flip() clock.tick(FPS)