# traffic_light **Repository Path**: babytech/traffic_light ## Basic Information - **Project Name**: traffic_light - **Description**: 红绿灯演示程序 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-11-01 - **Last Updated**: 2025-03-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Traffic Light Simulation Program A Pygame-based traffic light simulation program demonstrating vehicle and pedestrian movement under traffic light control. ## Features - 🚦 Realistic traffic light cycles (Green: 5s/Yellow: 1.5s/Red: 9.5s) - 🚗 5 different colored vehicle simulations - 🚶 Doraemon-themed pedestrian animation - 🖥️ 60FPS smooth animation - 🎮 Interactive start menu (Play/Exit buttons) ## Program Structure ```python main.py ├── Global variables (window size, colors, states) ├── MoveObject class (moving object logic) ├── light_thread function (traffic light control thread) ├── Button class (UI button component) ├── starting_screen function (start interface) └── Main program (resource loading, object initialization, main loop) ``` ## Core Logic ### Traffic Light Cycle Control ```python:main.py Lines 132-149 # Traffic light state switching logic if count < 1000: current_color = "green" elif count < 1300: current_color = "yellow" elif count < 3200: current_color = "red" else: count = 0 ``` ### Moving Object Control ```python:main.py Lines 35-100 class MoveObject: def update(self, signal_color): # Vehicle control logic (move on green, decelerate otherwise) # Pedestrian control logic (move on red, stop otherwise) ``` ## Requirements - Python 3.6+ - Pygame 2.0+ - Resource files directory `resource/` ## Usage 1. Install dependencies ```bash pip install pygame ``` 2. Run program ```bash python main.py ``` ## Interface Description - Start screen: Centered Play/Exit buttons - Main interface elements: - Dynamic road background - Real-time traffic light display - Vehicle and pedestrian animations - 60FPS frame rate control ## Control Rules | Signal | Vehicle Behavior | Pedestrian Behavior | |--------|----------------------|----------------------| | 🟢 Green | Normal passage | Wait at crosswalk | | 🟡 Yellow | Decelerate to stop line | Prepare to move | | 🔴 Red | Complete stop | Cross normally |