-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
145 lines (120 loc) · 4.7 KB
/
game.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
import cv2
import time
import random
import mediapipe as mp
import math
import numpy as np
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
# Correct initialization of Hands
hands = mp_hands.Hands(
static_image_mode=False, # Process video frames (False for real-time)
max_num_hands=2, # Max 2 hands detection
min_detection_confidence=0.7, # Detection confidence threshold
min_tracking_confidence=0.5 # Tracking confidence threshold
)
curr_Frame = 0
prev_Frame = 0
delta_time = 0
next_Time_to_Spawn = 0
Speed = [0, 5]
Fruit_Size = 30
Spawn_Rate = 1
Score = 0
Lives = 15
Difficulty_level = 1
game_Over = False
slash = np.array([[]], np.int32)
slash_Color = (255, 255, 255)
slash_length = 19
w = h = 0
Fruits = []
# Function to spawn fruits
def Spawn_Fruits():
fruit = {}
random_x = random.randint(15, 600)
random_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
fruit["Color"] = random_color
fruit["Curr_position"] = [random_x, 440]
fruit["Next_position"] = [0, 0]
Fruits.append(fruit)
# Function to move fruits
def Fruit_Movement(Fruits, speed):
global Lives
for fruit in Fruits:
if (fruit["Curr_position"][1]) < 20 or (fruit["Curr_position"][0]) > 650:
Lives = Lives - 1
Fruits.remove(fruit)
cv2.circle(img, tuple(fruit["Curr_position"]), Fruit_Size, fruit["Color"], -1)
fruit["Next_position"][0] = fruit["Curr_position"][0] + speed[0]
fruit["Next_position"][1] = fruit["Curr_position"][1] - speed[1]
fruit["Curr_position"] = fruit["Next_position"]
# Function to calculate distance
def distance(a, b):
x1, y1 = a[0], a[1]
x2, y2 = b[0], b[1]
return int(math.sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
cap = cv2.VideoCapture(0)
while (cap.isOpened()):
success, img = cap.read()
if not success:
print("skipping frame")
continue
h, w, c = img.shape
img = cv2.cvtColor(cv2.flip(img, 1), cv2.COLOR_BGR2RGB)
img.flags.writeable = False
results = hands.process(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
img,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
for id, lm in enumerate(hand_landmarks.landmark):
if id == 8: # Index finger tip
index_pos = (int(lm.x * w), int(lm.y * h))
cv2.circle(img, index_pos, 18, slash_Color, -1)
slash = np.append(slash, index_pos)
while len(slash) >= slash_length:
slash = np.delete(slash, len(slash) - slash_length, 0)
for fruit in Fruits:
d = distance(index_pos, fruit["Curr_position"])
if (d < Fruit_Size):
Score += 100
slash_Color = fruit["Color"]
Fruits.remove(fruit)
# Adjust difficulty based on score
if Score % 1000 == 0 and Score != 0:
Difficulty_level = int((Score / 1000) + 1)
Spawn_Rate = Difficulty_level * 4 / 5
Speed[0] = Speed[0] * Difficulty_level
Speed[1] = int(5 * Difficulty_level / 2)
if (Lives <= 0):
game_Over = True
slash = slash.reshape((-1, 1, 2))
cv2.polylines(img, [slash], False, slash_Color, 15, 0)
curr_Frame = time.time()
delta_Time = curr_Frame - prev_Frame
FPS = int(1 / delta_Time)
cv2.putText(img, "FPS : " + str(FPS), (int(w * 0.82), 50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 250, 0), 2)
cv2.putText(img, "Score: " + str(Score), (int(w * 0.35), 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 5)
cv2.putText(img, "Level: " + str(Difficulty_level), (int(w * 0.01), 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 150), 5)
cv2.putText(img, "Lives remaining : " + str(Lives), (200, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
prev_Frame = curr_Frame
if not game_Over:
if (time.time() > next_Time_to_Spawn):
Spawn_Fruits()
next_Time_to_Spawn = time.time() + (1 / Spawn_Rate)
Fruit_Movement(Fruits, Speed)
else:
cv2.putText(img, "GAME OVER", (int(w * 0.1), int(h * 0.6)), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 3)
Fruits.clear()
cv2.imshow("img", img)
if cv2.waitKey(5) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()