AI & MLBeginnerPRJ-102

Rock Paper Scissors AI Game

Play rock-paper-scissors against the computer using real hand gestures.

Built and tested by Shahan Fathima K · Industrial Robotics Institute

Laptop / PC with webcam

Python 3.11, OpenCV, MediaPipe

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

What is the Rock Paper Scissors AI Game?

The classic game played with your hand in front of a webcam. MediaPipe reads your fingers to recognise Rock (fist), Paper (open hand) or Scissors (two fingers). After a 3-2-1 countdown the computer picks at random, the winner is shown, and scores are kept over 10 rounds.

How it works

  1. A 3-2-1 countdown appears on screen, then "SHOW!".
  2. MediaPipe tracks your hand and checks which fingers are up.
  3. 0 fingers = Rock, 5 = Paper, index + middle = Scissors.
  4. The computer picks randomly and the winner is decided.
  5. Scores update; after 10 rounds the game shows GAME OVER. Press q to quit.

Parts list

ComponentQty
Laptop / PC1
Webcam1

Software

  • Python 3.11
  • OpenCV
  • MediaPipe

How to build the Rock Paper Scissors AI Game

  1. Install Python 3.11 and tick "Add Python to PATH".
  2. Open a terminal and run: pip install opencv-python mediapipe
  3. Paste the code into a new Python file and run it.
  4. Hold your hand up to the camera and show your move when it says SHOW!
  5. Play 10 rounds; press q to quit.

Rock Paper Scissors AI 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 random
import time

cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mpDraw = mp.solutions.drawing_utils

choices = ["Rock", "Paper", "Scissors"]
score_user = 0
score_computer = 0
rounds = 10

game_state = "COUNTDOWN"
start_time = time.time()
computer_choice = ""
user_choice = ""
result_text = ""
gesture_locked = False
display_text = ""


def get_gesture(lm):
    fingers = [1 if lm[4].x < lm[3].x else 0]   # thumb
    for tip, pip in [(8, 6), (12, 10), (16, 14), (20, 18)]:
        fingers.append(1 if lm[tip].y < lm[pip].y else 0)

    if sum(fingers) == 0:
        return "Rock"
    elif sum(fingers) == 5:
        return "Paper"
    elif fingers[1] == 1 and fingers[2] == 1 and fingers[3] == 0:
        return "Scissors"
    return None


while True:
    ret, frame = cap.read()
    if not ret:
        break
    frame = cv2.flip(frame, 1)
    result = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

    detected_choice = None
    if result.multi_hand_landmarks:
        for handLms in result.multi_hand_landmarks:
            mpDraw.draw_landmarks(frame, handLms, mpHands.HAND_CONNECTIONS)
            detected_choice = get_gesture(handLms.landmark)

    now = time.time()
    if rounds > 0:
        if game_state == "COUNTDOWN":
            elapsed = now - start_time
            if elapsed < 1:
                display_text = "3"
            elif elapsed < 2:
                display_text = "2"
            elif elapsed < 3:
                display_text = "1"
            else:
                display_text = "SHOW!"
                if detected_choice and not gesture_locked:
                    user_choice = detected_choice
                    computer_choice = random.choice(choices)
                    if user_choice == computer_choice:
                        result_text = "Draw"
                    elif (user_choice, computer_choice) in [("Rock", "Scissors"), ("Paper", "Rock"), ("Scissors", "Paper")]:
                        result_text = "You Win"
                        score_user += 1
                    else:
                        result_text = "Computer Wins"
                        score_computer += 1
                    rounds -= 1
                    game_state = "RESULT"
                    start_time = now
                    gesture_locked = True
        elif game_state == "RESULT":
            display_text = f"Result: {result_text}"
            if now - start_time > 2:
                game_state = "COUNTDOWN"
                start_time = now
                gesture_locked = False
    else:
        display_text = "GAME OVER"

    cv2.putText(frame, f"Rounds Left: {rounds}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
    cv2.putText(frame, f"You: {score_user} Computer: {score_computer}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 255), 2)
    cv2.putText(frame, display_text, (20, 150), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3)
    if game_state == "RESULT":
        cv2.putText(frame, f"You: {user_choice}", (20, 220), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.putText(frame, f"Computer: {computer_choice}", (20, 270), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

    cv2.imshow("Rock Paper Scissors AI", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Real-world uses

  • Fun AI demos at events
  • Learning gesture recognition
  • School exhibitions
  • Game development practice

Frequently asked questions

What is a Rock Paper Scissors AI Game?

The classic game played with your hand in front of a webcam. MediaPipe reads your fingers to recognise Rock (fist), Paper (open hand) or Scissors (two fingers).

What components are needed for the Rock Paper Scissors AI Game?

You need: Laptop / PC (1), Webcam (1). Software: Python 3.11, OpenCV, MediaPipe.

Is the Rock Paper Scissors AI 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 Rock Paper Scissors AI 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 Rock Paper Scissors AI 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