-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2 Assignment 4.py
101 lines (73 loc) · 2.8 KB
/
Q2 Assignment 4.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
import cv2
import mediapipe as mp
import numpy as np
# Initialize MediaPipe Hands
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.5)
# Game settings
width, height = 1280, 640
player_pos = [320, 440]
# enemy speed, size, and list initialization
enemy_speed=6
enemy_size=50
enemy_list=[]
# Initialize score
score=0
# Create random enemy
def create_enemy():
x_coordinate=np.random.randint(0,width - enemy_size)
return [x_coordinate,0]
# Move enemies down
def move_enemies(enemy_list):
for enemy in enemy_list:
enemy[1]+=enemy_speed
# Check if enemy is off-screen
def check_off_screen(enemy_list):
global score
for enemy in enemy_list[:] :
if enemy[1] > height:
score+=1
enemy_list.remove(enemy)
# Increment score for each enemy that goes off-screen
# Check for collisions
def check_collision(player_pos, enemy_list):
pos_x, pos_y = player_pos
for enemy in enemy_list:
if (pos_x < enemy[0] + enemy_size and pos_x + enemy_size > enemy[0] and pos_y < enemy[1] + enemy_size and pos_y + enemy_size > enemy[1]):
return True
return False
# Initialize webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the frame with MediaPipe
processed = hands.process(rgb_frame)
# Get coordinates of the index finger tip (landmark 8)
if processed.multi_hand_landmarks:
for hand_landmarks in processed.multi_hand_landmarks:
index_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
player_pos[0] = int(index_finger_tip.x * width)
player_pos[1] = int(index_finger_tip.y * height)
# Move player based on hand movement
cv2.rectangle(frame,(player_pos[0],player_pos[1]),(player_pos[0]+50,player_pos[1]+50),(0,255,0),-1)
# Add new enemies
if np.random.randint(0,30)==7:
enemy_list.append(create_enemy())
# Move enemies
move_enemies(enemy_list)
# Check for collision
if check_collision(player_pos,enemy_list)==True:
print("Game Over")
break
# Draw game elements
for enemy in enemy_list:
cv2.rectangle(frame,(enemy[0],enemy[1]),(enemy[0]+enemy_size,enemy[1]+enemy_size),(0,0,255),-1)
# Display score on the frame
cv2.putText(frame, f'Score: {score}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.imshow("Object Dodging Game", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()