forked from krother/software-engineering-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.py
57 lines (46 loc) · 1.13 KB
/
prototype.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
"""
Proof-of-concept: move around in a 2D frame
"""
import curses
# WASD keys
KEY_COMMANDS = {97: "left", 100: "right", 119: "up", 115: "down"}
# prepare the screen
screen = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.curs_set(0)
curses.noecho()
curses.raw()
screen.keypad(False)
win = curses.newwin(20, 20, 0, 0)
win.nodelay(True)
def game_loop(screen):
"""called by curses"""
x, y = 5, 5
# draw
screen.clear()
screen.addch(y, x, "O", curses.color_pair(1))
win.refresh()
screen.refresh()
while True:
# handle moves
char = win.getch()
direction = KEY_COMMANDS.get(char)
if direction == "left":
x -= 1
elif direction == "right":
x += 1
elif direction == "up":
y -= 1
elif direction == "down":
y += 1
else:
continue
# draw
screen.clear()
screen.addch(y, x, "O", curses.color_pair(1))
win.refresh()
screen.refresh()
if __name__ == "__main__":
curses.wrapper(game_loop)
curses.endwin()