A classic Pong game with a twist - control the paddles using hand gestures! This project combines MATLAB for the game engine with Python for hand tracking using MediaPipe.
This implementation features:
- Gesture-based controls using MediaPipe hand tracking
- Custom visual assets with Undertale-themed graphics
- Real-time UDP communication between Python and MATLAB
- Audio feedback with sound effects and background music
- Two-player support with independent hand tracking
- MATLAB R2019b or later
- Image Processing Toolbox
- Instrument Control Toolbox (for UDP communication)
- Python 3.7 or later
- Required packages:
pip install opencv-python mediapipe
- Webcam for hand tracking
- Sufficient lighting for accurate hand detection
Place the following files in your asset directory (C:\Users\hp\.vscode\dist\assets\
or modify the path in the code):
hitsound.wav
- Sound effect for paddle/wall collisionspoint.wav
- Sound effect for scoringmusic.mp3
- Background musiccustom_ball.png
- Ball sprite imagesans_paddle.png
- Paddle sprite imageundertale.png
- Background image
- Clone the repository
git clone https://github.com/yourusername/hand-controlled-pong.git
cd hand-controlled-pong
- Install Python dependencies
pip install opencv-python mediapipe
-
Set up assets
- Create your asset directory or use the default path
- Add all required audio and image files
- Update the
asset_path
variable inmatlab_pong.m
if needed
-
Configure UDP
- Ensure both scripts use the same IP (
127.0.0.1
) and port (5005
) - Check firewall settings if connection issues occur
- Ensure both scripts use the same IP (
- Launch the hand tracking script first:
python hand_tracker.py
- Allow camera access when prompted
- Position yourself so both hands are visible
- Start the MATLAB game:
matlab_pong()
- Point your index finger UP to move paddle up
- Point your index finger DOWN to move paddle down
- First hand detected controls Player 1 (left paddle)
- Second hand detected controls Player 2 (right paddle)
- Press ESC to quit the game
- Press Q in the hand tracker window to close tracking
- Score points by getting the ball past your opponent's paddle
- The ball bounces off the top and bottom walls
- Paddle collisions change the ball's direction
- First to reach the target score wins!
cfg.paddle_width = 3; % Paddle width
cfg.paddle_height = 12; % Paddle height
cfg.paddle_speed = 2; % Movement speed
cfg.ball_radius = 3; % Ball size
cfg.ball_speed = [1.0 0.8]; % Initial ball velocity [x, y]
cfg.field = [0 100 0 60]; % Playing field dimensions
min_detection_confidence=0.7 # Hand detection threshold
min_tracking_confidence=0.5 # Hand tracking threshold
cv2.resizeWindow("Hand Tracker", 480, 360) # Window size
Camera not opening:
- Check if another application is using the camera
- Try changing
cv2.VideoCapture(0)
tocv2.VideoCapture(1)
or higher
UDP connection failed:
- Ensure both scripts are using the same port (5005)
- Check firewall settings
- Verify both scripts are running on the same machine
Hand detection not working:
- Improve lighting conditions
- Ensure hands are clearly visible and not overlapping
- Adjust detection confidence thresholds
Audio not playing:
- Verify audio files exist in the asset path
- Check MATLAB audio device settings
- Ensure file formats are supported
Images not displaying:
- Verify image files exist and paths are correct
- Check image file formats (PNG recommended)
- Ensure Image Processing Toolbox is installed
hand-controlled-pong/
│
├── matlab_pong.m # Main MATLAB game engine
├── hand_tracker.py # Python hand tracking script
├── README.md # This file
│
└── assets/ # Game assets
├── hitsound.wav
├── point.wav
├── music.mp3
├── custom_ball.png
├── sans_paddle.png
└── undertale.png
- Python script captures webcam feed and processes hand gestures
- Hand positions are converted to directional commands ("up", "down", "none")
- Commands are sent via UDP to MATLAB
- MATLAB receives commands and updates paddle positions
- Game logic processes collisions, scoring, and rendering
- Audio feedback enhances gameplay experience
- Uses MediaPipe's hand landmark detection
- Compares wrist and index finger tip positions
- Index finger above wrist = "up" command
- Index finger below wrist = "down" command
- Supports up to 2 hands simultaneously
Contributions are welcome! Feel free to:
- Add new features
- Improve hand gesture recognition
- Create new themes and assets
- Optimize performance
- Fix bugs
This project is open source and available under the MIT License.
- MediaPipe for hand tracking technology
- Undertale for visual inspiration
- Classic Pong for timeless gameplay
For questions or suggestions, please open an issue on GitHub.