-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuman_snake.py
55 lines (46 loc) · 1.59 KB
/
human_snake.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
import sys
import matplotlib
matplotlib.use('tkAgg')
from matplotlib import pyplot as plt # noqa: E402
from snake import Snake # noqa: E402
from snake_ui import SnakeUI # noqa: E402
class HumanSnake:
"""
This class can be used to control the snake via keyboard.
"""
actions = {
'UP': 0b00, # 00
'DOWN': 0b01, # 01
'LEFT': 0b10, # 10
'RIGHT': 0b11, # 11
}
def __init__(self, snake, ui):
self.snake = snake
ui.fig.canvas.mpl_connect('key_press_event', self.keypress)
plt.show()
def keypress(self, event):
"""Handles keypresses."""
key = (event.key or '').upper()
if key == 'ESCAPE':
sys.exit()
if key in self.actions.keys():
ret = self.snake.step(self.actions[key])
if ret != 0:
print('You {}!'.format('win' if ret > 0 else 'lose'))
try:
if '--store' in sys.argv:
# writes the score into the output file
try:
fname = '{}.csv'.format(
sys.argv[sys.argv.index('--store') + 1])
except IndexError:
fname = 'human_unknown.csv'
with open(fname, 'a') as f:
print(self.snake.highscore, self.snake.steps,
sep=',', file=f)
finally:
sys.exit()
if __name__ == '__main__':
snake = Snake()
ui = SnakeUI(snake)
HumanSnake(snake, ui)