A Formula 1 starting lights reaction game built with p5.js. Test and improve your reaction time just like real F1 drivers!
Live Demo: https://michelschep.github.io/f1-reaction-game/
- Press SPACE or TAB to start a round
- Watch the 5 red lights turn on one by one (1 second each) with a beep sound
- Wait for all lights to turn green (random delay between 1-4 seconds)
- React as fast as possible by pressing SPACE or TAB when the lights turn green
- Your reaction time is recorded and ranked
- ✅ Good reaction: Short success beep
- 🏆 New record: Triple beep celebration sound
- ❌ False start: Harsh buzzer sound (pressed too early)
- 📊 Leaderboard: Top 5 best times displayed (stores up to 10 times)
- No sound plays when lights turn green - you must watch carefully!
- Reaction times under 200ms are exceptional
- Times are saved in your browser's localStorage
- p5.js - Canvas rendering and game loop
- Web Audio API - Sound generation using oscillators
- localStorage - Persistent score storage
f1-reaction-game/
├── index.html # HTML structure and p5.js imports
├── sketch.js # Main game logic and rendering
└── README.md # Documentation
The game uses a finite state machine with the following states:
ready- Waiting for player to startlighting- Red lights turning on sequentiallywaiting- Random delay before green lightsgreen- Lights are green, waiting for player reactionreacted- Player reacted successfullyfalse_start- Player pressed too early
Setup & Initialization
setup()- Initializes canvas and loads saved timesinitAudio()- Creates Web Audio API context (triggered on first user interaction)
Game Loop
draw()- Main game loop (60fps)- Renders lights, status text, and leaderboard
- Manages state transitions
- Handles timing logic
Rendering
drawLights()- Draws 5 light indicators with current statedrawStatus()- Displays game instructions and reaction timedrawLeaderboard()- Shows top 5 times with medal icons
Input Handling
keyPressed()- Detects SPACE (32) or TAB (9) keyhandleInput()- Processes input based on current game state
Game Logic
startRound()- Resets state and begins light sequencesaveTimes()/loadTimes()- Persist scores to localStorage
Audio System
playLightBeep()- Beep sound for each red light (400 Hz)playSuccessSound()- Success feedback (400-600 Hz sweep)playRecordSound()- Triple beep for new records (600-800 Hz)playFalseStartSound()- Error sound for false starts (150-80 Hz)
All sounds are generated programmatically using Web Audio API oscillators:
- Red lights: Sine wave at 400 Hz (150ms duration)
- Success: Sine wave sweep from 400-600 Hz
- Record: Three ascending beeps (600-800 Hz)
- False start: Sawtooth wave descending from 150-80 Hz
- Background: Dark theme (#1a1a1a) for reduced eye strain
- Lights: 80x120px rounded rectangles with circular indicators
- Colors:
- Red (#FF0000) for active lights
- Green (#00FF00) for go signal
- Gray (#3C3C3C) for inactive lights
Times are stored as a JSON array in localStorage:
localStorage.setItem('f1ReactionTimes', JSON.stringify(times));- Maximum 10 times stored
- Automatically sorted ascending (fastest first)
- Persists across browser sessions
- Clone the repository:
git clone https://github.com/michelschep/f1-reaction-game.git
cd f1-reaction-game- Open
index.htmlin a web browser:
# Windows
start index.html
# macOS
open index.html
# Linux
xdg-open index.html- Or use a local server (recommended):
# Python 3
python -m http.server 8000
# Node.js
npx serve
# Then open http://localhost:8000- Each red light: 1 second interval
- Total light-up phase: 5 seconds
- Random delay before green: 1-4 seconds (simulates real F1 variance)
- Total round time: 6-9 seconds
- < 200ms: Professional level
- 200-300ms: Very good
- 300-400ms: Good
- 400-500ms: Average
- > 500ms: Room for improvement
- Modern browsers with Web Audio API support
- Tested on Chrome, Firefox, Edge, Safari
- Requires JavaScript enabled
- localStorage for score persistence
Possible improvements:
- Add difficulty modes (shorter random delays)
- Multiplayer support
- Statistics (average, median, standard deviation)
- Visual reaction time graph
- Mobile touch support
- Social sharing of best times
MIT License - Feel free to use and modify!
Created by Michel Schep