From a658b56fd58004162534c4a1aaaa83e840ced209 Mon Sep 17 00:00:00 2001 From: tej Date: Tue, 29 Jul 2025 23:23:14 +0530 Subject: [PATCH 1/3] Commit Message --- Games/Snake_Game/Snake_main.py | 61 +++++++++++++++++++++++++++++++++ Games/Snake_Game/Snake_utils.py | 29 ++++++++++++++++ Games/Snake_Game/window_top.py | 7 ++++ 3 files changed, 97 insertions(+) create mode 100644 Games/Snake_Game/Snake_main.py create mode 100644 Games/Snake_Game/Snake_utils.py create mode 100644 Games/Snake_Game/window_top.py diff --git a/Games/Snake_Game/Snake_main.py b/Games/Snake_Game/Snake_main.py new file mode 100644 index 0000000..44109c3 --- /dev/null +++ b/Games/Snake_Game/Snake_main.py @@ -0,0 +1,61 @@ +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 +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 time.time() - last > cool: + pyautogui.press(g) + last = time.time() + + #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/window_top.py b/Games/Snake_Game/window_top.py new file mode 100644 index 0000000..e491857 --- /dev/null +++ b/Games/Snake_Game/window_top.py @@ -0,0 +1,7 @@ +import win32gui +import win32con + +def set_top(title): + win = win32gui.FindWindow(None, title) + if win: + win32gui.SetWindowPos(win, win32con.HWND_TOPMOST, 50, 100, 640, 480, win32con.SWP_SHOWWINDOW) From 1abcbab251f6b589692cd1c12157ff443c05c9d8 Mon Sep 17 00:00:00 2001 From: tej Date: Tue, 29 Jul 2025 23:58:33 +0530 Subject: [PATCH 2/3] Commit Message --- Games/Snake_Game/Snake_main.py | 5 +++-- .../__pycache__/Snake_utils.cpython-312.pyc | Bin 0 -> 1207 bytes .../__pycache__/window_top.cpython-312.pyc | Bin 0 -> 638 bytes 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 Games/Snake_Game/__pycache__/Snake_utils.cpython-312.pyc create mode 100644 Games/Snake_Game/__pycache__/window_top.cpython-312.pyc diff --git a/Games/Snake_Game/Snake_main.py b/Games/Snake_Game/Snake_main.py index 44109c3..9a8390c 100644 --- a/Games/Snake_Game/Snake_main.py +++ b/Games/Snake_Game/Snake_main.py @@ -14,6 +14,7 @@ cam = cv2.VideoCapture(0) last = time.time() cool = 1.5 +prev_gesture = None #Keep track of last gesture name = "Snake_Window" cv2.namedWindow(name) @@ -41,10 +42,10 @@ draw.draw_landmarks(img, hand, mp_hands.HAND_CONNECTIONS) f = fingers_up(hand.landmark) g = detect(f) - - if g and time.time() - last > cool: + 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): 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 0000000000000000000000000000000000000000..ac34a88fd9d20e12c8b21a0e1b7367d479d1bb24 GIT binary patch literal 1207 zcma)5-Amh06u&ngP1Dx4S{ZjOdW#h?($`! z4|B-D8Jh2wXzXdHDn|LiJ7TzAObTiI2`)4648W7M`EEjoaoAI+L?wydEg+{ipx#dw-Eb4qic$Aba%Fdh}a(^wmAuj>LIE$|Dr$biCoV zmA35BiF#2AL%x;ZPb8okhMw05`#Eh8T#^Dr%&B>8Q#G>|5!TcBlx|vtv(pBkK@r;o z8x~DC>C4sf^JmIy3&Ipz�ENl-HAH`h%_{bt`KZ45eTx%Vr^?ui1~^sKuPEEU25h zrL5%DtiDQlhZgO0&U#{$NT^eHwP=`QP>8;t^%bCk+-SVAa4y7agWJxRt?jM4cs$jJ z97P+^rZD3MV+YY{wD$fiIC|dYKZB#c!ef;rq3|c3s3q&u-(r`Dmh)9LQCB(l+x|3WO;deeV7k z!fAz%c?jq?_mU9aOs6*NZi?%?y@G_x>Ff4)Bw>QrEjo%xldd`mXu1tEH6MUN*UXv) zfC&k~y``ow?#2?X7}_f|#R)ey(xdl!bi@tE%yGCSON>8gKbJWL7T@gGxeCs07PMkc bpEsw$;qO8W$OVfrzGMb*qBSGpxPRdX6ANs(nMfY^K}0a;$NM_ zmKc)W9FQ}_5W_W897J5iEOCO0i3YYvR}1qV9_n>?2Ts6y7XW8S{r^^pyFxuShMMSd zSU<*1EHZEO$yhatO{~V_tYXA$6dBCM2ur?E^>;Z^Xm*(zSN9`x!w;AX9v(##4p#$O2<1@Yg+Vm2<>s@cZM%@Oo$YAKaSJ79 z%XZyw`K;|)iD*pus>UM%O{L!Q{QRrEzCw2-7m_MoDNAYxJS+U4oMySKs_>A8k`_c* z;hy@mTdvm>-7Ft)Nu8iv Date: Wed, 6 Aug 2025 04:13:49 +0530 Subject: [PATCH 3/3] Added a Component for the demo video --- Games/Snake_Game/index.html | 255 +++++++++++++++++++++++++----------- 1 file changed, 180 insertions(+), 75 deletions(-) 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 @@ - - - - Snake Game - -

Snake Game

- - -

Use the arrow keys to control the snake.

-

Speed increases each time you eat an apple! Good Luck!

- - - -