Skip to content

A fun and interactive **Fruit Ninja** game built with **pygame** and **MediaPipe** pose estimation. Originally developed for FPT University, this version includes a gift reward system based on player performance!

Notifications You must be signed in to change notification settings

tunneee/FPTU_Ninja_Fruit_VR

Repository files navigation

Fruit Ninja VR with Gift System 🍉🎁

A fun and interactive Fruit Ninja game built with pygame and MediaPipe pose estimation. Originally developed for FPT University, this version includes a gift reward system based on player performance!

🎓 About

This project was created for FPT University as an interactive game that combines computer vision with gamification. Players use their body movements tracked by a webcam to slice falling fruits and earn rewards based on their scores.

✨ Features

  • Motion-Controlled Gameplay: Use your hands to slice fruits through webcam tracking
  • Fruits & Obstacles:
    • 🍍 Pineapple
    • 🍉 Watermelon
    • 🥝 Kiwi
    • 🍊 Orange
    • 💣 Bomb (avoid this!)
  • Gift Reward System: Earn different gift tiers based on your final score
  • High Score Tracking: Your best score is automatically saved
  • Dual Hand Tracking: Black knife trails follow both hands
  • Smart Start Menu: Raise hands to begin (prevents accidental starts)
  • Full Screen Mode: Immersive gameplay experience

🎁 Gift System

The game rewards players with different gifts based on their final score:

Score Range Gift Tier Image
80+ points 🎁 Premium Gift present/present_1.png
60-79 points 🎁 Good Gift present/present_3.png
30-59 points 🎁 Basic Gift present/present_2.png
Below 30 😢 Try Again Random sad fruit image

How to Modify Gift Images

  1. Navigate to the present/ folder
  2. Replace the gift images:
    • present_1.png - Premium gift (80+ points)
    • present_3.png - Good gift (60-79 points)
    • present_2.png - Basic gift (30-59 points)
  3. Keep the same filenames or update the paths in scripts/fruit_ninja.py

How to Modify Score Thresholds

Edit these constants in scripts/fruit_ninja.py (lines 44-46):

GIFT_1_SCORE_THRES = 80  # Premium gift threshold
GIFT_2_SCORE_THRES = 60  # Good gift threshold
GIFT_3_SCORE_THRES = 30  # Basic gift threshold

How to Reset High Score

The high score is stored in highest_score.txt. To reset it:

  • Delete the file, or
  • Edit highest_score.txt and change the number to 0

🚀 Installation

Using UV (Recommended)

UV is a fast Python package installer and resolver. Install dependencies with:

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Install project dependencies
uv sync

Using Traditional pip

pip install pygame opencv-python mediapipe

From pyproject.toml

pip install -e .

🎮 How to Play

  1. Clone the repository:

    git clone <repository-url>
    cd Fruit-Ninja-VR_With_Gift
  2. Install dependencies (see Installation section above)

  3. Run the game:

    python scripts/fruit_ninja.py
  4. Gameplay:

    • Stand in front of your webcam
    • Raise your hands to start the game
    • Move your hands to slice falling fruits
    • Avoid bombs! 💣
    • Try to get the highest score and earn the best gift!
  5. Exit: Press the Esc key to quit

🎯 Game Controls

  • Hand Movement: Slice fruits
  • Hands Up: Start game from menu
  • Esc Key: Exit game

⚙️ Configuration

Edit settings in scripts/fruit_ninja.py:

FPS = 25                # Frame rate
SHOW_MINICAM = False    # Show webcam preview in corner
GAME_WIDTH = 1280       # Window width (height auto-adjusts to 16:9)

Camera Selection

Change the camera device in line 136:

cap = cv2.VideoCapture('/dev/video2')  # Linux
# cap = cv2.VideoCapture(0)            # Default camera (Windows/Mac)

For IP cameras, use:

stream_url = "rtsp://username:password@ip:port/stream"
cap = cv2.VideoCapture(stream_url, cv2.CAP_FFMPEG)

📦 Dependencies

  • pygame >= 2.6.1 - Game framework
  • opencv-python >= 4.11.0 - Computer vision and camera handling
  • mediapipe >= 0.10.21 - Pose estimation and hand tracking
  • Python >= 3.10

📸 Gameplay Preview

fruit ninja

alt text

🛠️ Building an Executable

Create a standalone executable using PyInstaller:

# Install PyInstaller
pip install pyinstaller

# Build executable (adjust PYTHON_PATH to your Python installation)
pyinstaller -w --onefile \
  --paths="PYTHON_PATH/Python39/Lib/site-packages/cv2" \
  --collect-data mediapipe \
  --add-data "PYTHON_PATH/Python39/Lib/site-packages/mediapipe;mediapipe" \
  -i "images/icon.ico" \
  scripts/fruit_ninja.py

The executable will be created in the dist/ folder.

📝 Project Structure

Fruit-Ninja-VR_With_Gift/
├── scripts/
│   ├── fruit_ninja.py          # Main game file
│   ├── fruit.py                # Fruit class
│   └── calculations.py         # Helper functions
├── images/                     # Game images and backgrounds
├── sounds/                     # Sound effects and music
├── present/                    # Gift images
│   ├── present_1.png          # Premium gift
│   ├── present_2.png          # Basic gift
│   └── present_3.png          # Good gift
├── fonts/                      # Custom fonts
├── highest_score.txt           # Stores the highest score
├── pyproject.toml             # Project dependencies
└── README.md                   # This file

🎓 Credits

📄 License

This project is based on the original Fruit-Ninja-VR project. Please refer to the original repository for licensing information.


Enjoy the game and aim for that premium gift! 🎁

About

A fun and interactive **Fruit Ninja** game built with **pygame** and **MediaPipe** pose estimation. Originally developed for FPT University, this version includes a gift reward system based on player performance!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages