Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed tracking issue while hands are away #3

Merged
merged 6 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Python stuff
__pycache__/
env/

# IDE stuff
.vscode/
.idea/
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ beat your python anywhere!

## How to play
You use your left and right hand as swords, like in beat saber. Use your blue
hand to break the blur squares and the red hand to break the red squares.
hand to break the blue squares and the red hand to break the red squares.

Squares will disappear after some time. If you break a square with a wrong hand,
you get -100 points, if right - you get +(100 - t) points, where t is the time
you get `-100` points, if right - you get `+(100 - t)` points, where `t` is the time
from square spawn.

If you move away from the camera, the game will crash, lol. Also, for some reason
it flips my view sometimes on Nvidia KDE Manjaro.

[![Demo CountPages alpha](https://chameleon-lizard.ru/p/game.gif)](https://chameleon-lizard.ru/p/game.gif)
## Known issues
- For some reason it flips the view sometimes on NVIDIA KDE Manjaro.
- There are reports that the game doesn't work on ARM systems
24 changes: 18 additions & 6 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def __init__(self, gamespeed) -> None:
# Creating variables
self._gamespeed = gamespeed
self._box_size = self._imsize[0] // 10

# Hand position tracking
self._track_status = True
self._lhand = self._rhand = (0, 0), (0, 0)

def run(self) -> None:
'''
Expand All @@ -52,27 +56,32 @@ def run(self) -> None:
running = False

# Acquiring keypoints from the camera stream
lhand, rhand = self.__get_point_data()

try:
self._lhand, self._rhand = self.__get_point_data()
self._track_status = True
except AttributeError:
self._track_status = False

# Generating boxes
if fcount % self._gamespeed == 0 or (self._blue_boxes == [] and self._red_boxes == []):
self._blue_boxes = [i for i in self._blue_boxes if abs(i[1] - fcount) <= self._gamespeed]
self._red_boxes = [i for i in self._red_boxes if abs(i[1] - fcount) <= self._gamespeed]
self.__create_boxes(fcount)

self.__check_lhand_hit(lhand)
self.__check_rhand_hit(rhand)
self.__check_lhand_hit(self._lhand)
self.__check_rhand_hit(self._rhand)

# Draw stuff
self.__draw(lhand, rhand)
self.__draw(self._lhand, self._rhand, self._track_status)

# Flip the display
pg.display.flip()

# Increasing the frame counter
fcount += 1

def __draw(self, lhand, rhand) -> None:
def __draw(self, lhand, rhand, track_status) -> None:
'''
Draw the graphics for the playing field.
'''
Expand Down Expand Up @@ -100,6 +109,9 @@ def __draw(self, lhand, rhand) -> None:
score = self._font.render(f'{self._score}', False, (0, 0, 0))
self._screen.blit(score, (50, 50))

if not track_status:
tracking_error = self._font.render(f'No hands detected', False, (255, 0, 0))
self._screen.blit(tracking_error, (50, 75))

def __check_lhand_hit(self, lhand) -> None:
'''
Expand Down Expand Up @@ -165,7 +177,7 @@ def __landmark_to_game(self, landmark) -> Tuple[int, int, int]:
int(landmark.z * self._imsize[0])
)

def __get_point_data(self) -> Tuple[Tuple[int, int], Tuple[int, int]]:
def __get_point_data(self) -> Tuple[Tuple[Tuple[int, int, int], Tuple[int, int, int]], Tuple[Tuple[int, int, int], Tuple[int, int, int]]]:
'''
Functions that gets the point data of both hands.
'''
Expand Down
19 changes: 3 additions & 16 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
absl-py==1.2.0
attrs==22.1.0
cycler==0.11.0
fonttools==4.37.1
kiwisolver==1.4.4
matplotlib==3.5.3
mediapipe==0.8.10.1
numpy==1.23.2
opencv-contrib-python==4.6.0.66
packaging==21.3
Pillow==9.2.0
protobuf==3.20.1
pygame==2.1.2
pyparsing==3.0.9
python-dateutil==2.8.2
six==1.16.0
opencv-contrib-python
mediapipe
pygame