1st Place at Hackstoga 2023 | Computer Vision, Python, OpenCV, dlib
A dodge game controlled entirely by your body. No keyboard. No mouse. Your face is the spaceship.
Background subtraction turns your silhouette into a live collision boundary. Asteroids and skateboards fall from the top of the screen. You dodge them by physically moving. If you're smiling when an asteroid hits you, there's a 50% chance it doesn't count.
The game runs in three phases:
Background capture. Before you step in front of the camera, the game captures a clean frame of the empty background. This becomes the baseline.
Motion detection. Each frame, your current image is subtracted from the background. The difference (your silhouette, your body) becomes a real-time collision mask. The asteroids check whether they overlap with anything in that mask.
Face tracking. A Haar cascade detects your face on the first frame. After that, a dlib correlation tracker maintains the lock frame-by-frame. A spaceship image is overlaid on top of your face. If tracking quality drops, detection restarts automatically.
Smile detection. When a collision occurs, if the smile Haar cascade fires, you get a 50/50 chance to ignore the life loss. The mechanic is visible: the game draws a rectangle around detected smiles in real time.
| Key | Action |
|---|---|
P |
Start / enter next level (face must be detected first) |
R |
Retry after death |
Q |
Quit |
Asteroid (Ball): falls straight down at increasing speed per level.
Skateboard (CurveBall): falls diagonally, drifting left or right each frame. Harder to predict.
Levels are divided into stages. Every 5 seconds, the stage advances and new obstacles are added. After enough stages, the level increments and the game resets with harder speeds.
Level 1
Stage 2: +1 Asteroid
Stage 3: +1 Skateboard
Stage 5: +2 fast Skateboards
Stage 6: advance to Level 2
- Python 3.8+
- A webcam
- The image assets in
Images/(see below)
pip install -r requirements.txtNote on dlib: dlib requires CMake and a C++ compiler. On macOS, run
brew install cmake. On Windows, install Visual Studio Build Tools. On Linux,sudo apt install cmake build-essential.
Place the following in the Images/ folder:
| File | Source |
|---|---|
asteroid.png |
Any asteroid PNG with transparent or black background |
skate_board.jfif |
Any skateboard image |
ss.png |
Spaceship PNG (overlaid on your face) |
space.jfif |
Background image shown on the death/game-over screen |
python game.pyStep away from the camera when prompted, let it capture the background, then press Q and step back in.
- OpenCV - camera capture, image processing, Haar cascades
- dlib - correlation tracker for face lock
- cvzone - FPS reader, selfie segmentation
- NumPy - frame arithmetic and collision masking
Background subtraction for real-time collision detection is deceptively effective. The naive approach works well when the background is stable. The dlib correlation tracker is substantially more robust than running the Haar cascade every frame, and its quality score makes it easy to detect and recover from tracking loss. Smile detection with Haar cascades is noisy enough to be fun without being reliable enough to feel cheap.
- Level 2 fully implemented with new obstacle types
- Configurable background sensitivity
- High score persistence
- Multiplayer: two cameras, two players
Built by Jake Frischmann