import cv2
import cvzone
from cvzone.HandTrackingModule import HandDetector
import numpy as np
cap = cv2.VideoCapture(1)
cap.set(3, 1280)
cap.set(4, 720)
# Importing all images
bg = cv2.imread("res/main_frame.png")
sc = cv2.imread("res/slesai_frame.png")
ijo = cv2.imread("res/ijo.png", cv2.IMREAD_UNCHANGED)
abang = cv2.imread("res/abang.png", cv2.IMREAD_UNCHANGED)
ba = cv2.imread("res/baa.png", cv2.IMREAD_UNCHANGED)
# Hand Detector
detector = HandDetector(detectionCon=0.8, maxHands=2)
# Variables
ballPos = [100, 100]
speedX = 15
speedY = 15
gameOver = False
score = [0, 0]
while True:
_, img = cap.read()
img = cv2.flip(img, 1)
imgRaw = img.copy()
# Find the hand and its landmarks
hands, img = detector.findHands(img, flipType=False) # with draw
# Overlaying the background image
img = cv2.addWeighted(img, 0.2, bg, 0.8, 0)
# Check for hands
if hands:
for hand in hands:
x, y, w, h = hand['bbox']
h1, w1, _ = ijo.shape
y1 = y - h1 // 2
y1 = np.clip(y1, 20, 415)
if hand['type'] == "Left":
img = cvzone.overlayPNG(img, ijo, (40, y1))
if 59 < ballPos[0] < 59 + w1 and y1 < ballPos[1] < y1 + h1:
speedX = -speedX
ballPos[0] += 30
score[0] += 1
if hand['type'] == "Right":
img = cvzone.overlayPNG(img, abang, (1220, y1))
if 1195 - 50 < ballPos[0] < 1195 and y1 < ballPos[1] < y1 + h1:
speedX = -speedX
ballPos[0] -= 30
score[1] += 1
# Game Over
if ballPos[0] < 40 or ballPos[0] > 1200:
gameOver = True
if gameOver:
img = sc
cv2.putText(img, str(score[1]).zfill(2), (585, 360), cv2.FONT_HERSHEY_COMPLEX,
2.5, (200, 0, 200), 5)
# If game not over move the ball
else:
# Move the Ball
if ballPos[1] >= 500 or ballPos[1] <= 10:
speedY = -speedY
ballPos[0] += speedX
ballPos[1] += speedY
# Draw the ball
img = cvzone.overlayPNG(img, ba, ballPos)
cv2.putText(img, str(score[0]), (210, 680), cv2.FONT_HERSHEY_COMPLEX, 3, (255, 255, 255), 5)
cv2.putText(img, str(score[1]), (1000, 680), cv2.FONT_HERSHEY_COMPLEX, 3, (255, 255, 255), 5)
#img[580:700, 20:233] = cv2.resize(imgRaw, (213, 120))
cv2.imshow("Image", img)
key = cv2.waitKey(1)
if key == ord('r'):
ballPos = [100, 100]
speedX = 15
speedY = 15
gameOver = False
score = [0, 0]
sc = cv2.imread("res/slesai_frame.png")