-
Notifications
You must be signed in to change notification settings - Fork 78
/
01_keyboard_tetris.py
executable file
·63 lines (47 loc) · 1.61 KB
/
01_keyboard_tetris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
README = """
Emulate keystrokes with hand poses to play Tetris.
The game of Tetris is there: https://tetris.com/play-tetris/
The hand poses used to play are:
- FIVE (open hand) to move left or right the falling piece.
The direction of the move is given by the rotation of the hand.
- ONE to rotate the piece.
- FIST (closed hand) to accelarate the fall.
Good luck !
"""
print(README)
from HandController import HandController
# Controlling the keyboard
try:
from pynput.keyboard import Key, Controller
except ModuleNotFoundError:
print("To run this demo, you need the python package: pynput")
print("Can be installed with: pip install pynput")
import sys
sys.exit()
kbd = Controller()
def press_key(key):
kbd.press(key)
kbd.release(key)
def move(event):
event.print_line()
rotation = event.hand.rotation
if -1 < rotation < -0.2:
press_key(Key.right)
elif 0.4 < rotation < 1.5:
press_key(Key.left)
def rotate(event):
event.print_line()
press_key(Key.up)
def down(event):
event.print_line()
press_key(Key.down)
config = {
'renderer' : {'enable': True},
'pose_actions' : [
{'name': 'MOVE', 'pose':'FIVE', 'callback': 'move', "trigger":"periodic", "first_trigger_delay":0, "next_trigger_delay": 0.2},
{'name': 'ROTATE', 'pose':'ONE', 'callback': 'rotate', "trigger":"periodic", "first_trigger_delay":0, "next_trigger_delay": 0.4},
{'name': 'DOWN', 'pose':'FIST', 'callback': 'down', "trigger":"periodic", "first_trigger_delay":0.05, "next_trigger_delay": 0.05}
]
}
HandController(config).loop()