-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.py
40 lines (33 loc) · 863 Bytes
/
terminal.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
import curses
from snake_game import snake_game
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)
stdscr.addstr(0, 2, "Hit arrow keys to play!")
stdscr.addstr(1, 2, "Hit 'q' to quit")
stdscr.refresh()
key_actions = {
curses.KEY_UP: (0, -1),
curses.KEY_DOWN: (0, 1),
curses.KEY_LEFT: (-1, 0),
curses.KEY_RIGHT: (1, 0)
}
game = snake_game()
game.next()
while True:
try:
key = stdscr.getch()
if key == ord('q'):
break
screen, reward = game.send(key_actions[key])
stdscr.addstr(2, 4, "Reward: "+str(reward))
screen_y = 5
for row in screen:
row_str = " ".join("F" if val == .5 else str(int(val)) for val in row)
stdscr.addstr(screen_y, 0, row_str)
screen_y += 1
except:
break
curses.echo()
curses.nocbreak()
curses.endwin()