This commit is contained in:
cubernetes 2023-08-11 05:55:36 +02:00
parent 4abb6cc7d5
commit d9261a9c99
18 changed files with 48 additions and 13 deletions

Binary file not shown.

BIN
assets/sound/lost.mp3 Normal file

Binary file not shown.

BIN
assets/sound/rick.mp3 Normal file

Binary file not shown.

BIN
assets/video/rick.mp4 Normal file

Binary file not shown.

BIN
assets/video/rick2.mp4 Normal file

Binary file not shown.

18
game.py
View File

@ -2,6 +2,7 @@
import sys
import random
import psutil
import numpy as np
import cv2
@ -102,6 +103,7 @@ def show_frame(frame: np.ndarray, to_stdout: bool=False) -> None:
def main() -> int:
music = start_game_sfx()
music_p = psutil.Process(pid=music.pid)
capture: VideoCapture = cv2.VideoCapture(0)
hands: mp.solutions.hands.Hands = mp_hands.Hands(max_num_hands=1)
@ -115,6 +117,7 @@ def main() -> int:
finger_y: int = -1
no_collect_ratio = 0
no_finger_ratio = 0
timer = 100
i: int = 0
while True:
@ -124,6 +127,8 @@ def main() -> int:
if not success:
continue
ratio = max(no_finger_ratio, no_collect_ratio)
frame = cv2.addWeighted(frame, 1 - ratio, np.ones(frame.shape, dtype=frame.dtype), ratio, 0)
if i > 30:
if collected_42:
collected_42 = False
@ -139,7 +144,7 @@ def main() -> int:
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
no_collect_ratio = min(i, 200) / 200
no_collect_ratio = min(i, timer) / timer
finger_positions = list(get_finger_positions(frame, hands, add_landmarks=True))
if finger_positions == []:
@ -147,12 +152,12 @@ def main() -> int:
no_finger_ratio = min(no_fingers, 255) / 255
else:
no_fingers = 0
if no_fingers > 255:
if ratio > 0.99:
music.kill()
lost_sfx()
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:
index_knuckle_1_pos: tuple[int, int] = (-1, -1)
@ -176,6 +181,11 @@ def main() -> int:
collected_42 = True
i = 0
score += 42
if score == 4200 / 4: # that's 25 collects
music_p.suspend()
initiate_rick()
music_p.resume()
timer = 60 + (timer - 60) * .9
collect_sfx()
show_frame(frame, to_stdout=(not sys.stdout.isatty()))
i += 1

View File

@ -15,6 +15,8 @@ def start_game() -> None:
proc.communicate()
def show_highscore() -> None:
with open('./.score', 'w') as f:
f.write('0')
Popen([
'tmux',
'display-popup',
@ -30,7 +32,7 @@ def main() -> NoReturn:
while True:
if found_hands():
start_game()
sleep(1)
sleep(2)
if __name__ == '__main__':
main()

BIN
mpv-shot0001.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
mpv-shot0002.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
mpv-shot0003.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
mpv-shot0004.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

BIN
mpv-shot0005.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -14,6 +14,7 @@ opencv-python==4.8.0.76
packaging==23.1
Pillow==10.0.0
protobuf==3.20.3
psutil==5.9.5
pycparser==2.21
pyparsing==3.0.9
python-dateutil==2.8.2

View File

@ -5,7 +5,7 @@
TERM_FONT='SauceCodePro Nerd Font Mono'
TERM_FONT_SIZE='10'
OUT_TERM='xterm-mono'
OUT_TERM='vt100'
TERM_FULLSCREEN='-fullscreen'
xterm \
-bg black \

View File

@ -1,4 +1,5 @@
from time import sleep
import sys
import time
from enum import Enum
from subprocess import Popen
from typing import Generator
@ -40,16 +41,37 @@ def save_score(score: int) -> None:
score_file.write(str(score))
def start_game_sfx() -> Popen:
Popen(['paplay', './assets/sfx/start.mp3']).communicate()
sleep(.5)
return Popen(['paplay', './assets/sfx/background_music.mp3'])
Popen(['paplay', './assets/sound/start.mp3']).communicate()
time.sleep(.5)
return Popen(['paplay', './assets/sound/background_music.mp3'])
def collect_sfx() -> None:
Popen(['paplay', './assets/sfx/collect.mp3'])
Popen(['paplay', './assets/sound/collect.mp3'])
def lost_sfx() -> None:
Popen(['paplay', './assets/sound/lost.mp3']).communicate()
def show_matrix() -> None:
Popen(['tmatrix'])
def initiate_rick() -> None:
Popen(['paplay', './assets/sound/rick.mp3'])
cap = cv2.VideoCapture('./assets/video/rick2.mp4')
fps: int = int(cap.get(cv2.CAP_PROP_FPS))
desired_delay: float = 1 / fps
while True:
start_time = time.time()
ret, frame = cap.read()
if not ret:
break
sys.stdout.buffer.write(frame.tobytes())
elapsed_time = time.time() - start_time
remaining_delay = max(desired_delay - elapsed_time, 0)
time.sleep(remaining_delay)
cap.release()
def found_hands() -> bool:
capture: VideoCapture = cv2.VideoCapture(0)
hands = mp_hands.Hands(max_num_hands=1)