-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballon_pop.py
162 lines (123 loc) · 4.31 KB
/
ballon_pop.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# import
import os
import time
import datetime as dt
import pygame
import cv2
import numpy as np # control matrices
import random
from cvzone.HandTrackingModule import HandDetector
# settings
FONT_LEFT_MARGIN = 10
FONT_COLOR = (255, 50, 50)
# initialize
pygame.init()
# Create Window
width, height = 1280, 720
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("AvilPage - CV Ballon Pop Game")
# Intialize Clock for FPS
fps = 30
clock = pygame.time.Clock()
# Webcam
cap = cv2.VideoCapture(0)
cap.set(3, 1280) # height
cap.set(4, 720) # width
def get_target_image():
targets = os.listdir('targets')
target_images = [target for target in targets if target.endswith('.png')]
target_image_name = random.choice(target_images)
target_image = pygame.image.load(f'targets/{target_image_name}').convert_alpha()
target_rect = target_image.get_rect()
return target_image_name, target_image, target_rect
target_image_name, imgBallon, rectBallon = get_target_image()
rectBallon.x, rectBallon.y = 500, 300 # here we are declaring postion of ballon (W,H)
# variables
total_speed = 0
speed = 10
score = 0
missed = 0
# Hand Detector
detector = HandDetector(detectionCon=0.3, maxHands=2)
# use mirrored video
def resetBallon(audio=None):
global target_image_name, rectBallon, imgBallon
splash = pygame.image.load("splash.png").convert_alpha()
window.blit(imgBallon, splash.get_rect())
# play sound
if audio:
pygame.mixer.music.load(audio)
pygame.mixer.music.play()
target_image_name, imgBallon, rectBallon = get_target_image()
rectBallon.x = random.randint(100, img.shape[1] - 100)
rectBallon.y = img.shape[0] + 50
# main loop
start = True
start_timer = 10
is_timer_running = True
now = dt.datetime.now()
stop_time = now + dt.timedelta(seconds=start_timer)
while start:
# Get Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
start = False
pygame.quit()
# openCV
success, img = cap.read()
# flip video
img = cv2.flip(img, 1)
hands, img = detector.findHands(img, flipType=False)
img = cv2.flip(img, 1)
# show timer
if is_timer_running:
start_timer = int((stop_time - dt.datetime.now()).seconds) + 1
font = pygame.font.Font("text.ttf", 50)
textScore = font.render(f"Game Starting in {start_timer} seconds", True, FONT_COLOR)
window.blit(textScore, (300, 300))
pygame.display.update()
clock.tick(fps)
# time.sleep(0.1)
is_timer_running = dt.datetime.now() < stop_time
total_speed = speed + score // 3
rectBallon.y -= total_speed # move the ballon up r
# We are adding this because as soon as ballon reach to the top of window it should be reset
if not is_timer_running and rectBallon.y < 0:
missed += 1
if missed >= 3:
# show game over
textScore = font.render(f"GAME OVER", True, FONT_COLOR)
window.blit(textScore, (500, 500))
textScore = font.render(f"Your Score: {score}", True, FONT_COLOR)
window.blit(textScore, (500, 550))
pygame.display.update()
time.sleep(5)
start = False
print("Game Over")
resetBallon(audio="laugh.mp3")
speed += 3
if hands:
for hand in hands:
x = hand['lmList'][8][0]
y = hand['lmList'][8][1]
if rectBallon.collidepoint(x, y):
resetBallon(audio='blast.mp3')
score += 1
break
# we did this because CV use BRG convention and pygame use RGB so we converted
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
imgRGB = np.rot90(imgRGB)
frame = pygame.surfarray.make_surface(imgRGB).convert()
window.blit(frame, (0, 0))
window.blit(imgBallon, rectBallon)
font = pygame.font.Font("text.ttf", 50)
textScore = font.render(f"Score :{score}", True, FONT_COLOR)
window.blit(textScore, (FONT_LEFT_MARGIN, 0))
textScore = font.render(f"Speed :{total_speed}", True, FONT_COLOR)
window.blit(textScore, (FONT_LEFT_MARGIN, 50))
textScore = font.render(f"Missed :{missed}", True, FONT_COLOR)
window.blit(textScore, (FONT_LEFT_MARGIN, 100))
# Update Display/Window
pygame.display.update()
# set FPs
clock.tick(fps)