Post

Interactive Snake Game Using OpenCV & Hand Tracking

Interactive Snake Game Using OpenCV & Hand Tracking

๐Ÿš€ Interactive Snake Game Using OpenCV & Hand Tracking in Python

Introduction

Are you looking for a fun, interactive project using Python, OpenCV, and Hand Tracking? In this guide, youโ€™ll learn how to build an interactive Snake Game controlled with your fingers using MediaPipe Hand Tracking and OpenCV.

This project is perfect for beginners and intermediate programmers interested in computer vision, game development, and artificial intelligence. Letโ€™s dive into the code and explore how to create a gesture-controlled Snake Game from scratch!

Features of the Interactive Snake Game

โœ… Hand-Tracking Control โ€“ Move the snake using your index finger. โœ… Real-time Interaction โ€“ The snake follows your hand movements dynamically. โœ… Food Collection & Score System โ€“ Eat food to grow the snake and increase the score. โœ… Collision Detection โ€“ Game over when the snake touches the border or itself. โœ… Restart & Quit Functionality โ€“ Restart with the โ€˜Rโ€™ key and quit with โ€˜Qโ€™.

Technologies Used

  • Python โ€“ Programming language for game logic and computer vision.
  • OpenCV โ€“ For capturing video from the webcam and rendering graphics.
  • MediaPipe โ€“ For real-time hand-tracking detection.
  • NumPy โ€“ For handling mathematical operations efficiently.

Prerequisites

Before you start, ensure you have the following installed:

1
pip install opencv-python mediapipe numpy

Code for Hand-Controlled Snake Game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import cv2
import mediapipe as mp
import numpy as np
import random

# Initialize MediaPipe Hand Tracking
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)

# Game Window Size
width, height = 800, 600

# Game Variables
snake_size = 10
speed = 5

def reset_game():
    global snake_pos, snake_length, food_pos, score, game_over
    snake_pos = [(width // 2, height // 2)]  # Snake starting position
    snake_length = 5
    food_pos = (random.randint(50, width - 50), random.randint(50, height - 50))
    score = 0
    game_over = False

# Initialize game
reset_game()

# Open Webcam
cap = cv2.VideoCapture(0)

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Flip the frame
    frame = cv2.flip(frame, 1)
    frame = cv2.resize(frame, (width, height))

    # Convert to RGB for MediaPipe
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = hands.process(rgb_frame)

    finger_position = None

    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)

            # Get the tip of the index finger
            index_finger = hand_landmarks.landmark[8]
            finger_position = (int(index_finger.x * width), int(index_finger.y * height))

    # Move Snake Towards Finger
    if not game_over and finger_position:
        snake_x, snake_y = snake_pos[-1]  # Get last position (snake head)
        finger_x, finger_y = finger_position

        # Calculate movement direction
        dx, dy = finger_x - snake_x, finger_y - snake_y
        dist = np.sqrt(dx ** 2 + dy ** 2)

        if dist > 0:
            move_x = int((dx / dist) * speed)
            move_y = int((dy / dist) * speed)
        else:
            move_x, move_y = 0, 0

        new_head = (snake_x + move_x, snake_y + move_y)
        snake_pos.append(new_head)

        # Keep snake length fixed
        if len(snake_pos) > snake_length:
            snake_pos.pop(0)

        # Check if Snake Eats Food
        head_x, head_y = snake_pos[-1]
        food_x, food_y = food_pos

        if abs(head_x - food_x) < 15 and abs(head_y - food_y) < 15:  # Collision check
            snake_length += 5  # Increase size
            score += 10  # Increase score
            food_pos = (random.randint(50, width - 50), random.randint(50, height - 50))  # New food position

        # Check for Border Collision
        if head_x <= 0 or head_x >= width or head_y <= 0 or head_y >= height:
            game_over = True

        # Check for Self Collision
        if new_head in snake_pos[:-1]:  # If the head touches its body
            game_over = True

    # Draw Snake
    for i, pos in enumerate(snake_pos):
        color = (0, 255 - i * 5, 0)  # Gradual green effect
        cv2.circle(frame, pos, snake_size, color, -1)

    # Draw Food
    cv2.circle(frame, food_pos, 8, (0, 0, 255), -1)  # Red food

    # Display Score
    cv2.putText(frame, f"Score: {score}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

    # Show Game Over Message
    if game_over:
        cv2.putText(frame, "GAME OVER", (width // 3, height // 2), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 4)
        cv2.putText(frame, "Press 'R' to Restart", (width // 3, height // 2 + 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

    # Show Game Window
    cv2.imshow("Snake Game - Finger Control", frame)

    # Check for Quit ('q') or Restart ('r')
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break
    elif key == ord('r') and game_over:
        reset_game()  # Restart the game

cap.release()
cv2.destroyAllWindows()

How the Code Works

  • ๐ŸŽฏ Step 1: Initialize Hand Tracking Uses MediaPipe Hands to detect and track hand movements. Captures the index fingerโ€™s position to control the snake.
  • ๐ŸŽฏ Step 2: Control Snake Movement The snake follows the detected fingerโ€™s position dynamically. Moves in the direction of the fingerโ€™s location on the screen.
  • ๐ŸŽฏ Step 3: Detect Food & Increase Score The snake eats food when its head touches the food position. The score increases by 10, and the snake grows in length.
  • ๐ŸŽฏ Step 4: Collision Detection If the snake touches the border or its own body, the game ends. Displays a GAME OVER message with an option to restart.

Final Thoughts

This interactive Snake Game using OpenCV and Hand Tracking is a great project to learn real-time hand gesture recognition. It integrates computer vision with game development, making it an exciting project for Python enthusiasts.

๐Ÿ”ฅ Whatโ€™s Next?

  • โœ… Add more gestures โ€“ Use different fingers to control speed or change direction.
  • โœ… Enhance the UI โ€“ Add animations and a graphical interface.
  • โœ… Implement AI โ€“ Use AI models to predict better movement patterns.

Start coding today and create your own gesture-controlled Snake Game! ๐ŸŽฎ๐Ÿ

This post is licensed under CC BY 4.0 by the author.