├── Readme.md ├── Resources ├── Background.png ├── Ball.png ├── bat1.png ├── bat2.png └── gameOver.png ├── img └── Result.gif ├── libraries.bat ├── main.py └── requirements.txt /Readme.md: -------------------------------------------------------------------------------- 1 | # Pong Game using Hand Gestures 2 | In this project, I will learn how to create a forever-alone pong game for all the lonely people out there. 3 | I will first learn how to track hands and then use some images to overlay on our game. 4 | 5 | ## Features 6 | * Can track your hand in real-time 7 | * Can play alone or with a friend 8 | 9 | ## How to install 10 | 1. Clone this repository on your computer 11 | `https://github.com/paveldat/pong_game.git` 12 | 2. Install all the requirements 13 | `run libraries.bat` or 14 | `pip install -r requirements.txt` 15 | 3. Run the program 16 | `python main.py` 17 | 18 | ## Help 19 | You might face issue with webcam not showing and you get errors. 20 | To solve it just change the value in this line (for example to `1`). 21 | `cap = cv2.VideoCapture(0)` 22 | Increment this number until you see your webcam. 23 | 24 | ## Result 25 | 26 | ![Alt Text](https://github.com/paveldat/pong_game/blob/main/img/Result.gif) 27 | -------------------------------------------------------------------------------- /Resources/Background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/pong_game/a6817fc94d1cf68c0ec1df6cd8fb32e19c938983/Resources/Background.png -------------------------------------------------------------------------------- /Resources/Ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/pong_game/a6817fc94d1cf68c0ec1df6cd8fb32e19c938983/Resources/Ball.png -------------------------------------------------------------------------------- /Resources/bat1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/pong_game/a6817fc94d1cf68c0ec1df6cd8fb32e19c938983/Resources/bat1.png -------------------------------------------------------------------------------- /Resources/bat2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/pong_game/a6817fc94d1cf68c0ec1df6cd8fb32e19c938983/Resources/bat2.png -------------------------------------------------------------------------------- /Resources/gameOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/pong_game/a6817fc94d1cf68c0ec1df6cd8fb32e19c938983/Resources/gameOver.png -------------------------------------------------------------------------------- /img/Result.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/pong_game/a6817fc94d1cf68c0ec1df6cd8fb32e19c938983/img/Result.gif -------------------------------------------------------------------------------- /libraries.bat: -------------------------------------------------------------------------------- 1 | pip install cvzone 2 | pip install opencv-python 3 | pip install numpy -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import cvzone 3 | from cvzone.HandTrackingModule import HandDetector 4 | import numpy as np 5 | 6 | cap = cv2.VideoCapture(0) 7 | cap.set(3, 1280) 8 | cap.set(4, 720) 9 | 10 | # Importing all resources 11 | imgBackground = cv2.imread("Resources/Background.png") 12 | imgGameOver = cv2.imread("Resources/gameOver.png") 13 | imgBall = cv2.imread("Resources/Ball.png", cv2.IMREAD_UNCHANGED) 14 | imgBat1 = cv2.imread("Resources/bat1.png", cv2.IMREAD_UNCHANGED) 15 | imgBat2 = cv2.imread("Resources/bat2.png", cv2.IMREAD_UNCHANGED) 16 | 17 | # Hand Detector 18 | detector = HandDetector(detectionCon=0.8, maxHands=2) 19 | 20 | # Variables 21 | ballPos = [100, 100] 22 | speedX = 15 23 | speedY = 15 24 | gameOver = False 25 | score = [0, 0] 26 | 27 | 28 | while True: 29 | _, img = cap.read() 30 | img = cv2.flip(img, 1) 31 | imgRaw = img.copy() 32 | 33 | # Find the hand and its landmarks 34 | hands, img = detector.findHands(img, flipType=False) 35 | 36 | # Overlaying the background image 37 | img = cv2.addWeighted(img, 0.2, imgBackground, 0.8, 0) 38 | 39 | # Check for hands 40 | if hands: 41 | for hand in hands: 42 | x, y, w, h = hand['bbox'] 43 | h1, w1, _ = imgBat1.shape 44 | y1 = y - h1//2 45 | y1 = np.clip(y1, 20, 415) 46 | 47 | if hand['type'] == "Left": 48 | img = cvzone.overlayPNG(img, imgBat1, (59, y1)) 49 | if 59 < ballPos[0] < 59 + w1 and y1 < ballPos[1] < y1 + h1: 50 | speedX = -speedX # Change OX direction 51 | ballPos[0] += 30 52 | score[0] += 1 53 | 54 | if hand['type'] == "Right": 55 | img = cvzone.overlayPNG(img, imgBat2, (1195, y1)) 56 | if 1195 - 50 < ballPos[0] < 1195 + w1 and y1 < ballPos[1] < y1 + h1: 57 | speedX = -speedX # Change OX direction 58 | ballPos[0] -= 30 59 | score[1] += 1 60 | 61 | # Game Over 62 | if ballPos[0] < 40 or ballPos[0] > 1200: 63 | gameOver = True 64 | 65 | if gameOver: 66 | img = imgGameOver 67 | cv2.putText(img, str(score[1] + score[1]).zfill(2), (585, 360), cv2.FONT_HERSHEY_COMPLEX, 2.5, (200, 0, 200), 5) 68 | 69 | # If game not over move the ball 70 | else: 71 | 72 | # Move the ball 73 | # Change OY direction 74 | if ballPos[1] >= 500 or ballPos[1] <= 10: 75 | speedY = -speedY 76 | 77 | ballPos[0] += speedX 78 | ballPos[1] += speedY 79 | 80 | # Draw the ball 81 | img = cvzone.overlayPNG(img, imgBall, ballPos) 82 | 83 | # Display score on the image 84 | # Left player (hand) 85 | cv2.putText(img, str(score[0]), (300, 650), cv2.FONT_HERSHEY_COMPLEX, 3, (255, 255, 255), 5) 86 | # Rigth player (hand) 87 | cv2.putText(img, str(score[1]), (900, 650), cv2.FONT_HERSHEY_COMPLEX, 3, (255, 255, 255), 5) 88 | 89 | # Cam show 90 | img[580:700, 20:233] = cv2.resize(imgRaw, (213, 120)) 91 | 92 | cv2.imshow("Image", img) 93 | key = cv2.waitKey(1) 94 | 95 | # Reload the game by pressing "r" 96 | if key == ord("r"): 97 | ballPos = [100, 100] 98 | speedX = 15 99 | speedY = 15 100 | gameOver = False 101 | score = [0, 0] 102 | imgGameOver = cv2.imread("Resources/gameOver.png") -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | cvzone 2 | opencv-python 3 | numpy --------------------------------------------------------------------------------