Compare commits
2 Commits
40b0e585e6
...
4abb6cc7d5
| Author | SHA1 | Date |
|---|---|---|
|
|
4abb6cc7d5 | |
|
|
ba24bbdf9a |
Binary file not shown.
Binary file not shown.
28
game.py
28
game.py
|
|
@ -70,7 +70,12 @@ def add_directional_triangle(
|
||||||
]).astype(np.float64)
|
]).astype(np.float64)
|
||||||
|
|
||||||
# normalize
|
# normalize
|
||||||
dir_vector /= np.linalg.norm(dir_vector)
|
norm = np.linalg.norm(dir_vector)
|
||||||
|
dir_vector /= norm
|
||||||
|
|
||||||
|
# TODO: Fix type issue
|
||||||
|
side_len *= norm / 15
|
||||||
|
# stretch /= (norm/30)
|
||||||
|
|
||||||
triangle_height: float = side_len * (3**0.5) / 2
|
triangle_height: float = side_len * (3**0.5) / 2
|
||||||
half_base: float = side_len / 2
|
half_base: float = side_len / 2
|
||||||
|
|
@ -96,7 +101,7 @@ def show_frame(frame: np.ndarray, to_stdout: bool=False) -> None:
|
||||||
cv2.waitKey(1)
|
cv2.waitKey(1)
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
start_game_sfx()
|
music = start_game_sfx()
|
||||||
|
|
||||||
capture: VideoCapture = cv2.VideoCapture(0)
|
capture: VideoCapture = cv2.VideoCapture(0)
|
||||||
hands: mp.solutions.hands.Hands = mp_hands.Hands(max_num_hands=1)
|
hands: mp.solutions.hands.Hands = mp_hands.Hands(max_num_hands=1)
|
||||||
|
|
@ -106,6 +111,10 @@ def main() -> int:
|
||||||
img42_y: int = -img42_side_len - 1 - noise_42img
|
img42_y: int = -img42_side_len - 1 - noise_42img
|
||||||
no_fingers: int = 0
|
no_fingers: int = 0
|
||||||
score: int = 0
|
score: int = 0
|
||||||
|
finger_x: int = -1
|
||||||
|
finger_y: int = -1
|
||||||
|
no_collect_ratio = 0
|
||||||
|
no_finger_ratio = 0
|
||||||
|
|
||||||
i: int = 0
|
i: int = 0
|
||||||
while True:
|
while True:
|
||||||
|
|
@ -119,23 +128,32 @@ def main() -> int:
|
||||||
if collected_42:
|
if collected_42:
|
||||||
collected_42 = False
|
collected_42 = False
|
||||||
frame_height, frame_width = frame.shape[:2]
|
frame_height, frame_width = frame.shape[:2]
|
||||||
img42_x = random.randint(0, frame_width - img42_side_len - 1)
|
img42_x = random.randint(0, frame_width - img42_side_len - 1 - noise_42img)
|
||||||
img42_y = random.randint(0, frame_height - img42_side_len - 1)
|
img42_y = random.randint(0, frame_height - img42_side_len - 1 - noise_42img)
|
||||||
|
while ((finger_x - img42_x) ** 2 + (finger_y - img42_y) ** 2) ** .5 < 200:
|
||||||
|
img42_x = random.randint(0, frame_width - img42_side_len - 1 - noise_42img)
|
||||||
|
img42_y = random.randint(0, frame_height - img42_side_len - 1 - noise_42img)
|
||||||
rand_noise_y = random.randint(0, noise_42img)
|
rand_noise_y = random.randint(0, noise_42img)
|
||||||
rand_noise_x = random.randint(0, noise_42img)
|
rand_noise_x = random.randint(0, noise_42img)
|
||||||
frame[
|
frame[
|
||||||
img42_y + rand_noise_y : img42_y + img42_side_len + rand_noise_y,
|
img42_y + rand_noise_y : img42_y + img42_side_len + rand_noise_y,
|
||||||
img42_x + rand_noise_x : img42_x + img42_side_len + rand_noise_x,
|
img42_x + rand_noise_x : img42_x + img42_side_len + rand_noise_x,
|
||||||
] = img42
|
] = img42
|
||||||
|
no_collect_ratio = min(i, 200) / 200
|
||||||
|
|
||||||
finger_positions = list(get_finger_positions(frame, hands, add_landmarks=True))
|
finger_positions = list(get_finger_positions(frame, hands, add_landmarks=True))
|
||||||
if finger_positions == []:
|
if finger_positions == []:
|
||||||
no_fingers += 1
|
no_fingers += 1
|
||||||
|
no_finger_ratio = min(no_fingers, 255) / 255
|
||||||
else:
|
else:
|
||||||
no_fingers = 0
|
no_fingers = 0
|
||||||
if no_fingers > 200:
|
if no_fingers > 255:
|
||||||
|
music.kill()
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
ratio = max(no_finger_ratio, no_collect_ratio)
|
||||||
|
frame = cv2.addWeighted(frame, 1 - ratio, np.ones(frame.shape, dtype=frame.dtype), ratio, 0)
|
||||||
|
|
||||||
for positions in finger_positions:
|
for positions in finger_positions:
|
||||||
index_knuckle_1_pos: tuple[int, int] = (-1, -1)
|
index_knuckle_1_pos: tuple[int, int] = (-1, -1)
|
||||||
for finger_id, finger_x, finger_y in positions:
|
for finger_id, finger_x, finger_y in positions:
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,15 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import os
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from typing import NoReturn
|
from typing import NoReturn
|
||||||
|
|
||||||
from utils import *
|
from utils import *
|
||||||
|
|
||||||
|
abspath = os.path.abspath(__file__)
|
||||||
|
dname = os.path.dirname(abspath)
|
||||||
|
os.chdir(dname)
|
||||||
|
|
||||||
def start_game() -> None:
|
def start_game() -> None:
|
||||||
proc = Popen(['./start_game.sh'])
|
proc = Popen(['./start_game.sh'])
|
||||||
proc.communicate()
|
proc.communicate()
|
||||||
|
|
@ -15,8 +20,8 @@ def show_highscore() -> None:
|
||||||
'display-popup',
|
'display-popup',
|
||||||
'-E',
|
'-E',
|
||||||
'watch',
|
'watch',
|
||||||
'-tcn.5',
|
'-tcn.6',
|
||||||
r"""sh -c 'for _ in $(seq $(($(tput lines) / 3 - 1))); do printf "\n\033[31m"; done; printf "%$(($(tput cols) / 2 + 5))s\n" "Highscore:"; figlet -w $(tput cols) -c $(cat "/home/tosuman/42/hackthelobby/.score"); printf "\n%$(($(tput cols) / 2 + 8))s\n" "Show your hands!";'""",
|
r"""bash -c 'for _ in $(seq $(($(tput lines) / 3 - 1))); do printf "\n\033[31m"; done; printf "%$(($(tput cols) / 2 + 5))s\n" "Highscore:"; figlet -w $(tput cols) -c $(cat "/home/tosuman/42/hackthelobby/.score"); printf "\n\033[3$((RANDOM % 7 + 1))m%$(($(tput cols) / 2 + 4 + RANDOM % 8))s\n" "Show your hands!";'""",
|
||||||
])
|
])
|
||||||
|
|
||||||
def main() -> NoReturn:
|
def main() -> NoReturn:
|
||||||
|
|
|
||||||
4
utils.py
4
utils.py
|
|
@ -39,10 +39,10 @@ def save_score(score: int) -> None:
|
||||||
with open('./.score', 'w') as score_file:
|
with open('./.score', 'w') as score_file:
|
||||||
score_file.write(str(score))
|
score_file.write(str(score))
|
||||||
|
|
||||||
def start_game_sfx() -> None:
|
def start_game_sfx() -> Popen:
|
||||||
Popen(['paplay', './assets/sfx/start.mp3']).communicate()
|
Popen(['paplay', './assets/sfx/start.mp3']).communicate()
|
||||||
sleep(.5)
|
sleep(.5)
|
||||||
Popen(['paplay', './assets/sfx/background_music.mp3'])
|
return Popen(['paplay', './assets/sfx/background_music.mp3'])
|
||||||
|
|
||||||
def collect_sfx() -> None:
|
def collect_sfx() -> None:
|
||||||
Popen(['paplay', './assets/sfx/collect.mp3'])
|
Popen(['paplay', './assets/sfx/collect.mp3'])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue