-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathFlappyBird.py
More file actions
48 lines (36 loc) · 1.28 KB
/
FlappyBird.py
File metadata and controls
48 lines (36 loc) · 1.28 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
# pip install opencv-python cvzone pyautogui mediapipe
import cv2
from cvzone.HandTrackingModule import HandDetector
import pyautogui
import time
# NOTE: This program can also be used to play chrome Dino!!
# Initialize Hand Detector
detector = HandDetector(detectionCon=0.85, maxHands=1)
# Initialize webcam
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320) # Lower resolution for faster processing - NO LAG!!!!!!!!!!!!
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
last_press_time = 0
press_cooldown = 0.5 # seconds
while True:
success, frame = cap.read()
if not success:
break
frame = cv2.flip(frame, 1)
hands, img = detector.findHands(frame)
if hands:
hand = hands[0]
fingers = detector.fingersUp(hand)
totalFingers = fingers.count(1)
cv2.putText(img, f'Fingers: {totalFingers}', (10, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Space key press with cooldown
current_time = time.time()
if totalFingers == 5 and current_time - last_press_time > press_cooldown:
pyautogui.press('space')
last_press_time = current_time
cv2.imshow("Hand Controller", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()