-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.py
More file actions
101 lines (83 loc) · 3.04 KB
/
ping.py
File metadata and controls
101 lines (83 loc) · 3.04 KB
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
import cv2
import cvzone
import numpy as np
from cvzone.HandTrackingModule import HandDetector
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
# Importing all images
img_background = cv2.imread("Background.png")
img_game_over = cv2.imread("gameOver.png")
img_ball = cv2.imread("Ball.png", cv2.IMREAD_UNCHANGED)
img_bat1 = cv2.imread("bat1.png", cv2.IMREAD_UNCHANGED)
img_bat2 = cv2.imread("bat2.png", cv2.IMREAD_UNCHANGED)
# Hand Detector
detector = HandDetector(detectionCon=0.8, maxHands=2)
# Variables
ball_pos = [100, 100]
speed_x = 15
speed_y = 15
game_over = False
score = [0, 0]
while True:
_, img = cap.read()
img = cv2.flip(img, 1)
img_raw = 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, img_background, 0.8, 0)
# Check for hands
if hands:
for hand in hands:
x, y, w, h = hand['bbox']
h1, w1, _ = img_bat1.shape
y1 = y - h1 // 2
y1 = np.clip(y1, 20, 415)
if hand['type'] == "Left":
img = cvzone.overlayPNG(img, img_bat1, (59, y1))
if 59 < ball_pos[0] < 59 + w1 and y1 < ball_pos[1] < y1 + h1:
speed_x = -speed_x
ball_pos[0] += 30
score[0] += 1
if hand['type'] == "Right":
img = cvzone.overlayPNG(img, img_bat2, (1195, y1))
if 1195 - 50 < ball_pos[0] < 1195 and y1 < ball_pos[1] < y1 + h1:
speed_x = -speed_x
ball_pos[0] -= 30
score[1] += 1
# Game Over
if ball_pos[0] < 40 or ball_pos[0] > 1200:
game_over = True
if game_over:
img = img_game_over
cv2.putText(img, str(score[1] + score[0]).zfill(2), (585, 360), cv2.FONT_HERSHEY_COMPLEX,
2.5, (200, 0, 200), 5)
# Check for spacebar press to restart
key = cv2.waitKey(1)
if key == ord(' '):
ball_pos = [100, 100]
speed_x = 15
speed_y = 15
game_over = False
score = [0, 0]
img_game_over = cv2.imread("Resources/gameOver.png")
else:
# Move the Ball
if ball_pos[1] >= 500 or ball_pos[1] <= 10:
speed_y = -speed_y
ball_pos[0] += speed_x
ball_pos[1] += speed_y
# Draw the ball
img = cvzone.overlayPNG(img, img_ball, ball_pos)
cv2.putText(img, str(score[0]), (300, 650), cv2.FONT_HERSHEY_COMPLEX, 3, (255, 255, 255), 5)
cv2.putText(img, str(score[1]), (900, 650), cv2.FONT_HERSHEY_COMPLEX, 3, (255, 255, 255), 5)
img[580:700, 20:233] = cv2.resize(img_raw, (213, 120))
cv2.imshow("Image", img)
key = cv2.waitKey(1)
if key == ord('r'):
ball_pos = [100, 100]
speed_x = 15
speed_y = 15
game_over = False
score = [0, 0]