Skip to main content

๐ŸŽฏ Build an AI Shooting Game Using Hand Tracking (Python + OpenCV + MediaPipe)

๐ŸŽฏ Build AI Shooting Game Using Hand Tracking

In this tutorial, we build a real-time AI Shooting Game using Python, OpenCV and MediaPipe. You control the crosshair using your index finger and shoot by pinching your thumb and index finger together.


๐Ÿ“ฆ Install Required Libraries

pip install opencv-python mediapipe

๐Ÿง  How The Game Works

  • Camera detects your hand
  • Index finger controls crosshair
  • Thumb + index pinch triggers shoot
  • Flying disc moves across screen
  • Hit disc → Score increases

๐Ÿง‘‍๐Ÿ’ป Full Python Code


import cv2
import mediapipe as mp
import random
import math
import time

mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)

cap = cv2.VideoCapture(0)

cv2.namedWindow("AI Shooting Game", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("AI Shooting Game", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

score = 0
disc_radius = 30
disc_speed = 6
disc_x = 0
disc_y = 200

last_shot_time = 0
cooldown = 0.5

def new_disc(width, height):
    x = 0
    y = random.randint(100, height - 100)
    return x, y

while True:
    success, frame = cap.read()
    if not success:
        break

    frame = cv2.flip(frame, 1)
    height, width, _ = frame.shape

    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = hands.process(rgb)

    cross_x = width // 2
    cross_y = height // 2
    shoot = False

    if result.multi_hand_landmarks:
        for hand_landmarks in result.multi_hand_landmarks:
            landmarks = hand_landmarks.landmark

            index_tip = landmarks[8]
            cross_x = int(index_tip.x * width)
            cross_y = int(index_tip.y * height)

            thumb_tip = landmarks[4]
            thumb_x = int(thumb_tip.x * width)
            thumb_y = int(thumb_tip.y * height)

            cv2.circle(frame, (cross_x, cross_y), 12, (0,255,0), -1)
            cv2.circle(frame, (thumb_x, thumb_y), 12, (255,0,0), -1)

            distance = math.sqrt((cross_x - thumb_x)**2 +
                                 (cross_y - thumb_y)**2)

            if distance < 40:
                current_time = time.time()
                if current_time - last_shot_time > cooldown:
                    shoot = True
                    last_shot_time = current_time

    disc_x += disc_speed

    if disc_x > width:
        disc_x, disc_y = new_disc(width, height)

    cv2.circle(frame, (disc_x, disc_y), disc_radius, (255,200,0), -1)

    if shoot:
        distance_to_disc = math.sqrt((cross_x - disc_x)**2 +
                                     (cross_y - disc_y)**2)

        if distance_to_disc < disc_radius:
            score += 1
            disc_x, disc_y = new_disc(width, height)

    cv2.putText(frame, "Score: " + str(score),
                (50,70),
                cv2.FONT_HERSHEY_SIMPLEX,
                1.5,
                (0,255,0),
                3)

    cv2.imshow("AI Shooting Game", frame)

    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()

๐ŸŽฎ How To Play

Move your index finger to aim.
Pinch thumb and index finger to shoot.
Hit flying discs to increase score.
Press ESC to exit.

Comments

Popular posts from this blog

TOUCH PLATE BASED DOOR BELL

Title:  Touch plate based door bell  Circuit:  Components: IC 555 Resistors: 1 M, 100k, 330 ohms Transistor: BC547  PN2222A Capacitor: 10n 1 Copper plate : as touch plate. A 6v battery An LED / Ic UM66 Description: This is the simple circuit for touch plate based security system. In this project what basically done is, circuit detects stray voltages produced by mains voltage and electrostatic built  up in the room.If sufficient static voltage is detected by the plate then chip will charge up. Transistor BC 547 or PN2222A is used basically to increase the sensitivity.In place of led just connect IC um 66(melody IC). Applications: In homes, of course. This can be specially used in places like hospitals, when patients need to call doctor by himself.

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ A load cell is a transducer that converts force (weight) into an electrical signal. The HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales. Today we’ll connect a load cell to Raspberry Pi 3 using the HX711 module. ๐Ÿงช ๐Ÿ”ง Components Required Component Quantity Raspberry Pi 3 1 Load Cell 1 HX711 Module 1 Jumper Wires 6 Breadboard (optional) 1 ๐Ÿ”Œ Pin Connections HX711 Pin Raspberry Pi Pin Pin Number VCC 5V Pin 2 GND Ground Pin 6 DT GPIO 5 Pin 29 SCK GPIO 6 Pin 31 Figure: Load Cell connected to Raspberry Pi 3 via HX711 ๐Ÿ’ป Python Code from hx711 import HX711 import RPi.GPIO as GPIO import time hx = HX711(dout_pin=5, pd_sck_pin=6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset()...

Interfacing Sound Sensor with Raspberry Pi 3

๐Ÿ”น Overview The KY-037 is a high-sensitivity sound detection sensor that can detect noise levels in the environment. It provides both analog and digital outputs. In this tutorial, we’ll interface the digital output of KY-037 with Raspberry Pi 3 Model B+ (without using an ADC like MCP3008) and detect sound events.