Play Temple Run Using Hand Gestures with Python – Guide

Have you ever imagined playing a game like Temple Run just by waving your hand in front of a camera? Well, in this step-by-step blog, you will learn how to build a hand gesture-controlled Temple Run game using Python. We’ll be using tools like OpenCV, Mediapipe, and PyAutoGUI to make it all work. This is a great beginner-friendly project if you’re getting started with computer vision and automation.


Before that, read this now – https://linkedin.openinapp.co/fsg5z

GITHUB REPO LINK – https://shorturl.at/xKeZ3

🌐 What We Are Going to Build

We will create a system where your webcam detects your hand gestures and sends keyboard controls to play Temple Run (browser/emulator version). For example:

  • πŸ‹οΈ Index + Middle Fingers Up β†’ Jump
  • πŸ¦Ήβ€β™‚οΈ Ring + Pinky Fingers Up β†’ Slide
  • πŸ– Thumb + Index β†’ Turn Right
  • πŸ–– Middle + Ring β†’ Turn Left

⚑ Tech Stack

  • Python
  • OpenCV β†’ For webcam and image processing
  • Mediapipe β†’ For detecting hand and finger positions
  • PyAutoGUI β†’ For simulating key presses

πŸ”§ Step-by-Step Tutorial

Play Temple Run Using Hand Gestures

Step 1: Install Python

Make sure Python 3.8 or later is installed.

Step 2: Setup Project Folder

Create a new folder on your desktop:

TempleRun-Gesture-Control

Open this folder in VS Code.

Step 3: Create a Python File

Create a new file named:

main.py

Step 4: Install Required Libraries

Open terminal in VS Code and install dependencies:

pip install opencv-python mediapipe pyautogui

Step 5: Write the Python Code

Paste this full code into main.py:

import cv2
import mediapipe as mp
import pyautogui

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

cap = cv2.VideoCapture(0)
tip_ids = [4, 8, 12, 16, 20]

while True:
    success, img = cap.read()
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = hands.process(img_rgb)

    if results.multi_hand_landmarks:
        for handLms in results.multi_hand_landmarks:
            lmList = []
            for id, lm in enumerate(handLms.landmark):
                h, w, _ = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                lmList.append((cx, cy))

            fingers = []
            if lmList[tip_ids[0]][0] > lmList[tip_ids[0] - 1][0]:
                fingers.append(1)
            else:
                fingers.append(0)

            for id in range(1, 5):
                if lmList[tip_ids[id]][1] < lmList[tip_ids[id] - 2][1]:
                    fingers.append(1)
                else:
                    fingers.append(0)

            if fingers == [0, 1, 1, 0, 0]:
                pyautogui.press("up")
                print("Jump")
            elif fingers == [0, 0, 0, 1, 1]:
                pyautogui.press("down")
                print("Slide")
            elif fingers == [1, 1, 0, 0, 0]:
                pyautogui.press("right")
                print("Right")
            elif fingers == [0, 0, 1, 1, 0]:
                pyautogui.press("left")
                print("Left")

            mp_draw.draw_landmarks(img, handLms, mp_hands.HAND_CONNECTIONS)

    cv2.imshow("Temple Run Controller", img)
    if cv2.waitKey(1) == ord('q'):
        break

Step 6: Run the Code

  • Make sure your webcam is turned on.
  • Press the Run button in VS Code or use the terminal:
python main.py
  • A window will open showing your webcam feed. Now show your gestures!

Step 7: Play Temple Run

  • Open Temple Run Emulator in your browser.
  • Focus the browser window.
  • Use your hand gestures:
    • Peace sign (Index + Middle) = Jump
    • Rock sign (Ring + Pinky) = Slide
    • Thumb + Index = Right
    • Middle + Ring = Left

🌟 Pro Tips

  • Use good lighting for better hand detection.
  • Keep your hand steady for accurate gesture recognition.
  • Customize gestures by editing the fingers == [...] combinations.

πŸ“Š Output Demo (Optional for YouTube/Reel)

You can screen-record and show:

  1. Hand gestures in webcam window
  2. Game reacting to those gestures

πŸ“– What You Learned

  • Basics of Computer Vision using OpenCV
  • Hand Tracking using Mediapipe
  • Keyboard Automation using PyAutoGUI
  • How to build fun Python automation projects

πŸ’Œ Stay Connected!

If you liked this project, follow me on Instagram @udaycodes for daily beginner-friendly content, Python tips, and project ideas!


Leave a Comment