AI & MLBeginnerPRJ-078

AI Gesture Paddle Game

Move the paddle with your hand in front of the webcam to keep the ball alive.

Built and tested by Aboobacker sidheeq K P · Industrial Robotics Institute

Laptop / PC with webcam

Python 3.11, OpenCV (opencv-python), MediaPipe, PyGame, PyCharm (recommended)

LevelBeginner
Build time1–2 hours
BoardLaptop / PC with webcam
Parts2 components

What is the AI Gesture Paddle Game?

A paddle-and-ball game you play with hand gestures instead of a keyboard. The webcam tracks your wrist with MediaPipe and moves the paddle left and right as you move your hand. Hit the ball to score; it speeds up every 5 points, and you have 3 lives. Press R to restart.

How it works

  1. The webcam captures your hand and MediaPipe finds the wrist position.
  2. The wrist’s left-right position sets where the paddle is.
  3. The ball bounces off the walls and the paddle; each hit scores a point.
  4. Every 5 points the ball gets faster.
  5. Missing the ball costs a life; after 3 misses it is Game Over. Press R to play again.

Parts list

ComponentQty
Laptop / Computer1
Webcam (built-in or external)1

Software

  • Python 3.11
  • OpenCV (opencv-python)
  • MediaPipe
  • PyGame
  • PyCharm (recommended)

How to build the AI Gesture Paddle Game

  1. Install Python 3.11 and tick "Add Python to PATH".
  2. Open a terminal and run: pip install opencv-python mediapipe pygame
  3. Paste the code into a new Python file and run it.
  4. Stand in front of the webcam and move your hand left and right.
  5. Press R to restart after Game Over, or close the window to quit.

AI Gesture Paddle Game code

Complete, tested Python code for this project. Copy it into Python 3.11 and upload it and run it.

import cv2
import mediapipe as mp
import pygame
import sys
import random

# ---------- Initialize PyGame ----------
pygame.init()
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gesture Paddle Game")
clock = pygame.time.Clock()
FONT = pygame.font.SysFont(None, 40)

# ---------- Game Variables ----------
paddle_w, paddle_h = 100, 20
paddle_x = WIDTH // 2 - paddle_w // 2
paddle_y = HEIGHT - 50

ball_radius = 15
ball_x = WIDTH // 2
ball_y = HEIGHT // 2

base_speed = 5
ball_dx = random.choice([-base_speed, base_speed])
ball_dy = -base_speed

score = 0
lives = 3
game_over = False

# ---------- MediaPipe Hand Tracking ----------
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
cap = cv2.VideoCapture(0)


def draw_objects():
    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (0, 255, 0), (paddle_x, paddle_y, paddle_w, paddle_h))
    pygame.draw.circle(screen, (255, 0, 0), (int(ball_x), int(ball_y)), ball_radius)
    screen.blit(FONT.render(f"Score: {score}", True, (255, 255, 255)), (10, 10))
    screen.blit(FONT.render(f"Lives: {lives}", True, (255, 255, 255)), (WIDTH - 150, 10))


def get_current_speed():
    # Increase speed every 5 points
    return base_speed + score // 5


def reset_ball():
    global ball_x, ball_y, ball_dx, ball_dy
    ball_x, ball_y = WIDTH // 2, HEIGHT // 2
    speed = get_current_speed()
    ball_dx = random.choice([-speed, speed])
    ball_dy = -speed


# ---------- Main Game Loop ----------
while True:
    clock.tick(60)

    ret, frame = cap.read()
    if not ret:
        continue
    frame = cv2.flip(frame, 1)
    results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            cap.release()
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
            score, lives, game_over = 0, 3, False
            reset_ball()

    if not game_over:
        # Paddle follows the wrist
        if results.multi_hand_landmarks:
            hand = results.multi_hand_landmarks[0]
            x = hand.landmark[mp_hands.HandLandmark.WRIST].x
            paddle_x = int(x * WIDTH) - paddle_w // 2
            paddle_x = max(0, min(WIDTH - paddle_w, paddle_x))

        # Ball movement
        ball_x += ball_dx
        ball_y += ball_dy

        if ball_x <= ball_radius or ball_x >= WIDTH - ball_radius:
            ball_dx *= -1
        if ball_y <= ball_radius:
            ball_dy *= -1

        # Paddle collision
        if (paddle_y < ball_y + ball_radius < paddle_y + paddle_h and
                paddle_x < ball_x < paddle_x + paddle_w and ball_dy > 0):
            score += 1
            speed = get_current_speed()
            ball_dy = -speed
            ball_dx = speed if ball_dx > 0 else -speed

        # Missed the ball
        if ball_y > HEIGHT:
            lives -= 1
            reset_ball()
            if lives <= 0:
                game_over = True

    draw_objects()
    if game_over:
        over_text = FONT.render("GAME OVER! Press R to Restart", True, (255, 0, 0))
        screen.blit(over_text, (WIDTH // 2 - over_text.get_width() // 2, HEIGHT // 2))
    pygame.display.update()

Real-world uses

  • Gesture-controlled games
  • Fun computer vision demos
  • Exhibitions and open days
  • Learning PyGame

Frequently asked questions

What is a AI Gesture Paddle Game?

A paddle-and-ball game you play with hand gestures instead of a keyboard. The webcam tracks your wrist with MediaPipe and moves the paddle left and right as you move your hand.

What components are needed for the AI Gesture Paddle Game?

You need: Laptop / Computer (1), Webcam (built-in or external) (1). Software: Python 3.11, OpenCV (opencv-python), MediaPipe, PyGame, PyCharm (recommended).

Is the AI Gesture Paddle Game project good for beginners?

Yes. It is rated beginner and takes about 1–2 hours to build, using the Laptop / PC with webcam. The guide on this page explains every connection and step.

Where can I get the code for the AI Gesture Paddle Game?

The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.

Do I need any hardware for the AI Gesture Paddle Game?

No kit is needed. This project runs on a laptop or PC with webcam. For help with the setup, message IRI on WhatsApp at +91 86060 82634.

Industrial Robotics Institute
Logo