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 START_TIME = 0 #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() #BG screen.fill(BG_COLOUR) #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_y + lane_speed) % 80 #ROAD pygame.draw.rect(screen, ROAD_COLOUR, (200, 0, 400, HEIGHT)) #LANE DIVIDER for i in range(-2, HEIGHT // 80 + 3): 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)) #score/timer font = pygame.font.SysFont(None, 48) current_time = pygame.time.get_ticks() elapsed_time = ((current_time - START_TIME) // 1000) * 5 timer_text = font.render(f"Score: {elapsed_time}", True, (255, 255, 255)) # Render the text screen.blit(timer_text, (50, 50)) pygame.display.flip() clock.tick(FPS)