-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolormatch.py
221 lines (175 loc) · 5.95 KB
/
colormatch.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import cv2
import mediapipe as mp
import time
import math
import pygame
import random
# Determines whether or not the distance of pixels between @prev and @current is greater than @tolerance
def hasMoved(prev, current, tolerance):
if abs(prev.x - current.x) > tolerance or abs(prev.y - current.y) > tolerance:
return True
else:
return False
# Create a new 2D point at the average of @one and @two
def pointAverage(one, two):
return Point((one.x + two.x) / 2, (one.y + two.y) / 2)
# Calculate a points coordinate relative to the screen (pixel coordinates)
def calculateRelativePoint(point, image):
return Point(point.x * image.shape[1], point.y * image.shape[0])
# Return the direction of movement from prev -> current
def getDirection(prev, curr, tolerance):
left = None
up = None
# Moving left:
if curr.x - prev.x > tolerance:
left = True
# Moving right:
if curr.x - prev.x < -1 * tolerance:
left = False
# Moving up:
if curr.y - prev.y > tolerance:
up = True
# Moving down:
if curr.y - prev.y < -1 * tolerance:
up = False
return (left, up)
# Initialize MediaPipe Hand solution
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
# Initialize webcam
cap = cv2.VideoCapture(0)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
prev_center = Point(-1, -1)
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 400
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Simple Dino Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
ORANGE = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 229, 255)
BLUE = (0, 0, 255)
PURPLE = (89, 0, 255)
PINK = (255, 0, 234)
# Game variables
block_size = 30
block_y = SCREEN_HEIGHT - block_size
block_x = 50
block_y_change = 0
gravity = 1
jump_speed = -15
line_width = 20
line_height = random.randint(20, 30)
line_x = SCREEN_WIDTH
line_speed = 10
score = 0
font = pygame.font.Font(None, 36)
# lower = more frequent
line_frequency = 400
# Clock
clock = pygame.time.Clock()
# Initialize font
pygame.font.init()
number_font = pygame.font.Font(None, 28)
def getColor(finger_count):
match finger_count:
case 0:
return RED
case 1:
return ORANGE
case 2:
return BLUE
case 3:
return PURPLE
case 4:
return PINK
case 5:
return GREEN
return RED
# Lines list
lines = []
# Game loop
running = True
while running:
direction = (None, None)
screen.fill((74, 74, 74))
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
success, image = cap.read()
if not success:
continue
# Flip the image horizontally for a later selfie-view display
image = cv2.flip(image, 1)
# Check if 5 seconds have passed since the last update
current_time = time.time()
# Update the finger count
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
finger_count = 0
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
finger_tips = [8, 12, 16, 20]
middle_joints = [5, 9, 13, 17]
for tip, joint in zip(finger_tips, middle_joints):
if hand_landmarks.landmark[tip].y < hand_landmarks.landmark[joint].y:
finger_count += 1
# Handle thumb for right hand forwards
if hand_landmarks.landmark[4].x < hand_landmarks.landmark[3].x:
finger_count += 1
# Draw the hand landmarks on the image
mp.solutions.drawing_utils.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Display the count of raised fingers
cv2.putText(image, f'Fingers: {finger_count}', (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# Display the image
cv2.imshow('Finger Count', image)
if direction[1] == True and block_y == SCREEN_HEIGHT - block_size:
block_y_change = jump_speed
# Update block
block_y_change += gravity
block_y += block_y_change
if block_y > SCREEN_HEIGHT - block_size:
block_y = SCREEN_HEIGHT - block_size
block_y_change = 0
# Draw block
pygame.draw.rect(screen, getColor(finger_count), (block_x, block_y, block_size, block_size))
# Update and draw lines
if not lines or lines[-1]['x'] < SCREEN_WIDTH - line_frequency:
line_height = random.randint(20, 30)
line_number = random.randint(0, 5)
lines.append({'x': SCREEN_WIDTH, 'height': line_height, 'number': line_number})
for line in lines[:]:
line['x'] -= line_speed
pygame.draw.rect(screen, getColor(line['number']), (line['x'], SCREEN_HEIGHT - line['height'], line_width, line['height']))
# Render and draw number
number_surface = number_font.render(str(line['number']), True, WHITE)
screen.blit(number_surface, (line['x'] + line_width // 2 - number_surface.get_width() // 2, SCREEN_HEIGHT - line['height'] // 2 - number_surface.get_height() // 2))
if line['x'] < -line_width:
lines.remove(line)
score += 1
line_speed += 1
# Collision detection
for line in lines:
if line['x'] < block_x + block_size and line['x'] + line_width > block_x and block_y + block_size > SCREEN_HEIGHT - line['height']:
if line['number'] != finger_count:
running = False
# Display score
score_text = font.render(f'Score: {score}', True, WHITE)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(30)
# Close the game window
pygame.quit()
print(f"You scored {score}. Better luck next time!")
# Release the webcam and close OpenCV window
cap.release()
cv2.destroyAllWindows()