diff --git a/Games/Snake_Game/Snake_main.py b/Games/Snake_Game/Snake_main.py new file mode 100644 index 0000000..9a8390c --- /dev/null +++ b/Games/Snake_Game/Snake_main.py @@ -0,0 +1,62 @@ +import cv2 +import mediapipe as mp +import pyautogui +import time + +from Snake_utils import fingers_up, detect +from window_top import set_top + +mp_hands = mp.solutions.hands +hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7) +draw = mp.solutions.drawing_utils + +#Camera +cam = cv2.VideoCapture(0) +last = time.time() +cool = 1.5 +prev_gesture = None #Keep track of last gesture +name = "Snake_Window" +cv2.namedWindow(name) + +#Instructions +instructions = [ + "SNAKE GAME GESTURE CONTROLS:", + "Index Finger Up ->Up", + "Index + Middle Up ->Down", + "Thumb Left ->Right", + "Pinky Only ->Left", + "ESC ->Exit" +] + +while True: + ok, img = cam.read() + if not ok: + break + + img = cv2.flip(img, 1) + rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + res = hands.process(rgb) + + if res.multi_hand_landmarks: + for hand in res.multi_hand_landmarks: + draw.draw_landmarks(img, hand, mp_hands.HAND_CONNECTIONS) + f = fingers_up(hand.landmark) + g = detect(f) + if g and (g != prev_gesture or time.time() - last > cool): + pyautogui.press(g) + last = time.time() + prev_gesture = g #updates last gesture + + #instructions + for i, text in enumerate(instructions): + cv2.putText(img, text, (10, 30 + i * 25), cv2.FONT_HERSHEY_SIMPLEX, + 0.6, (255, 0, 255), 2, cv2.LINE_AA) + + cv2.imshow(name, img) + set_top(name) + + if cv2.waitKey(1) & 0xFF == 27:#exit + break + +cam.release() +cv2.destroyAllWindows() diff --git a/Games/Snake_Game/Snake_utils.py b/Games/Snake_Game/Snake_utils.py new file mode 100644 index 0000000..5cc04f4 --- /dev/null +++ b/Games/Snake_Game/Snake_utils.py @@ -0,0 +1,29 @@ +def fingers_up(landmarks): + fingers = [] + + if landmarks[4].x < landmarks[3].x: + fingers.append(1) + else: + fingers.append(0) + + tips = [8, 12, 16, 20] + pips = [6, 10, 14, 18] + + for t, p in zip(tips, pips): + if landmarks[t].y < landmarks[p].y: + fingers.append(1) + else: + fingers.append(0) + + return fingers + +def detect(f): + if f == [0, 1, 0, 0, 0]: + return 'up' + elif f == [0, 1, 1, 0, 0]: + return 'down' + elif f == [1, 0, 0, 0, 0]: + return 'right' + elif f == [0, 0, 0, 0, 1]: + return 'left' + return None diff --git a/Games/Snake_Game/__pycache__/Snake_utils.cpython-312.pyc b/Games/Snake_Game/__pycache__/Snake_utils.cpython-312.pyc new file mode 100644 index 0000000..ac34a88 Binary files /dev/null and b/Games/Snake_Game/__pycache__/Snake_utils.cpython-312.pyc differ diff --git a/Games/Snake_Game/__pycache__/window_top.cpython-312.pyc b/Games/Snake_Game/__pycache__/window_top.cpython-312.pyc new file mode 100644 index 0000000..fe25783 Binary files /dev/null and b/Games/Snake_Game/__pycache__/window_top.cpython-312.pyc differ diff --git a/Games/Snake_Game/index.html b/Games/Snake_Game/index.html index efa85cd..b9f2cf3 100644 --- a/Games/Snake_Game/index.html +++ b/Games/Snake_Game/index.html @@ -1,85 +1,190 @@
- - - -Speed increases each time you eat an apple! Good Luck!
- - - -