A sophisticated real-time traffic signal simulation system built with Python and Pygame. This project provides a realistic visualization of a four-way traffic intersection with intelligent signal control, multiple vehicle types, and dynamic traffic flow management.
- Overview
- Features
- Installation
- Usage
- How It Works
- Customization
- Screenshots
- Applications
- Technical Details
- Contributing
- License
Traffic Visualiser simulates a realistic four-way traffic intersection with automated signal control. The system manages vehicle spawning, movement, collision detection, and traffic light timing to create an authentic urban traffic scenario. This project is ideal for educational purposes, traffic engineering demonstrations, and algorithm testing.
- Multi-directional Traffic Flow: Four-way intersection with right, down, left, and up traffic directions
- Intelligent Signal Control: Automated traffic light system with configurable timing
- Multiple Vehicle Types: Support for cars, buses, trucks, bikes, and rickshaws
- Realistic Vehicle Behavior: Speed variation, stopping at red lights, and smooth acceleration
- Dynamic Vehicle Spawning: Random vehicle generation with type distribution
- Collision Detection: Vehicles maintain safe distances and queue properly
- Real-time Visualization: Smooth Pygame-based graphics with 60 FPS rendering
- 5 Vehicle Types: Each with unique dimensions and characteristics
- Cars
- Buses
- Trucks
- Bikes
- Rickshaws
- Random Spawning: Vehicles appear at configurable intervals
- Lane Discipline: Vehicles stay in designated lanes
- Turning Logic: Support for right, left, and straight movements
- Automated Timing: Configurable green, yellow, and red signal durations
- Sequential Control: Signals change in a defined order (right β down β left β up)
- Yellow Light Phase: Transition period between green and red
- Visual Indicators: Clear signal state display for each direction
- Python 3.6 or higher
- pip (Python package manager)
-
Clone the repository
git clone https://github.com/rahulkumar7189/traffic_visualiser.git cd traffic_visualiser -
Install required dependencies
pip install pygame
-
Verify installation
python --version pip show pygame
Run the simulation with default settings:
python traffic_visualiser.pyThe simulation window will open, displaying:
- A four-way intersection
- Traffic lights in each direction
- Vehicles spawning and moving according to signal states
- Real-time traffic flow visualization
- Close Window: Click the X button or press Alt+F4 to exit
- The simulation runs continuously until manually stopped
- Green Light: Vehicles in that direction can proceed
- Yellow Light: Transition phase, vehicles should prepare to stop
- Red Light: Vehicles must stop and wait
- Vehicle Colors: Different colors represent different vehicle types
- Coordinates Display: Shows signal and vehicle positions (for debugging)
The system consists of three main classes:
-
TrafficSignal: Manages traffic light states and timing
- Controls red, yellow, and green signal durations
- Handles automatic signal transitions
- Maintains signal state for each direction
-
Vehicle: Represents individual vehicles in the simulation
- Stores vehicle properties (type, position, speed, direction)
- Handles movement logic and stopping behavior
- Manages vehicle rendering
-
Main Simulation Loop: Coordinates the entire system
- Spawns vehicles at random intervals
- Updates vehicle positions
- Manages signal transitions
- Renders all elements to the screen
- Vehicles spawn at entry points for each direction
- They move at their designated speed until:
- A red or yellow light is encountered
- Another vehicle is too close ahead
- Vehicles stop and queue when necessary
- When the light turns green, vehicles accelerate and proceed
- Vehicles exit the simulation after crossing the intersection
Default Timing:
- Green Signal: 5 seconds
- Yellow Signal: 5 seconds
- Red Signal: Variable (depends on other signals)
Sequence: Right β Down β Left β Up β Repeat
You can customize various parameters by modifying the code:
# Find this section in the code
defaultGreen = {0:10, 1:10, 2:10, 3:10} # Green light duration
defaultYellow = 5 # Yellow light duration
defaultRed = 150 # Initial red duration# Adjust the spawn interval (in milliseconds)
if ts >= timeGap:
ts = 0
# Modify the vehicle generation logic here# Modify speeds dictionary
speeds = {'car':2.25, 'bus':1.8, 'truck':1.8, 'bike':2.5, 'rickshaw':2.0}screenWidth = 1400
screenHeight = 800# Adjust the vehicle type distribution in the vehicle generation logic
typeOfVehicle = random.randint(0,4) # 0:car, 1:bus, 2:truck, 3:bike, 4:rickshawAdd screenshots of your simulation here to showcase:
- Main intersection view
- Different traffic scenarios
- Various vehicle types in action
- Signal state transitions
This traffic simulation can be used for:
- Traffic Engineering: Demonstrate signal timing optimization
- Computer Science: Illustrate object-oriented programming concepts
- Game Development: Learn Pygame basics and animation techniques
- Algorithm Design: Test traffic flow algorithms
- Signal Timing Optimization: Experiment with different timing strategies
- Traffic Flow Analysis: Study vehicle queue formation and dissipation
- Congestion Studies: Model intersection capacity and bottlenecks
- Intelligent Transportation Systems: Test adaptive signal control algorithms
- Urban Planning: Visualize proposed intersection designs
- Smart City Projects: Prototype traffic management systems
- Simulation Training: Create scenarios for traffic controller training
- Python: Core programming language
- Pygame: Graphics rendering and game loop management
- Random Module: Vehicle generation and type selection
- Time Module: Signal timing and frame rate control
- Simulation FPS: 60 frames per second
- Default Screen Size: 1400x800 pixels
- Vehicle Types: 5 distinct types with unique properties
- Traffic Directions: 4 (right, down, left, up)
- Signal States: 3 per direction (red, yellow, green)
- Efficient collision detection using position-based checks
- Optimized rendering with Pygame's blit operations
- Proper memory management for spawned and despawned vehicles
- Frame-rate independent timing for consistent behavior
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch
git checkout -b feature/AmazingFeature
- Commit your changes
git commit -m 'Add some AmazingFeature' - Push to the branch
git push origin feature/AmazingFeature
- Open a Pull Request
- Add pedestrian crossing simulation
- Implement emergency vehicle priority
- Create adaptive signal timing based on traffic density
- Add sound effects for vehicle movement and signals
- Implement vehicle turning animations
- Add traffic violation detection
- Create statistics dashboard (throughput, wait times, etc.)
- Support for roundabouts or complex intersections
This project is licensed under the MIT License - see the LICENSE file for details.
Rahul Kumar
- GitHub: @rahulkumar7189
- Pygame community for excellent documentation and examples
- Traffic engineering principles for realistic signal timing
- Open source community for inspiration and support
If you have any questions, issues, or suggestions:
- Open an issue on GitHub
- Star the repository if you find it useful
- Share with others who might benefit
Note: This is a simulation for educational and demonstration purposes. Real-world traffic systems require extensive testing, safety validation, and regulatory compliance.