Repository navigation
Expand file tree
/
Copy pathsnake.py
More file actions
executable file
·86 lines (61 loc) · 1.88 KB
/
Copy pathsnake.py
File metadata and controls
executable file
·86 lines (61 loc) · 1.88 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A rough outline to a game of snake.
Steer using arrow-keys or keys "asdw".
"""
import curses
import time
def main(scr):
"""
Press any key to quit.
"""
# Don't stop the while-loop while waiting for input
scr.nodelay(1)
# Draw a border
scr.border()
# Make cursor invisible
curses.curs_set(0)
# Get dimensions
max_y, max_x = scr.getmaxyx()
# The snake
snake = {
"representation": "O",
"max-length": 5,
"position": [(max_x//2, max_y//2)],
"direction": (1, 0)
}
# Do until exit
while True:
# Get input from user and flush the input buffer
ch = scr.getch()
# Check if a key was pressed and update
if ch == curses.KEY_LEFT or ch == ord('a'):
snake["direction"] = (-1, 0)
elif ch == curses.KEY_RIGHT or ch == ord('d'):
snake["direction"] = (1, 0)
elif ch == curses.KEY_UP or ch == ord('w'):
snake["direction"] = (0, -1)
elif ch == curses.KEY_DOWN or ch == ord('s'):
snake["direction"] = (0, 1)
elif ch == ord("q"):
break
# Move one step forward
x, y = snake["position"][0]
dx, dy = snake["direction"]
snake["position"].insert(0, (x+dx, y+dy))
# Keep its size and remove tha last item from the snake & screen
if len(snake["position"]) >= snake["max-length"]:
x, y = snake["position"].pop()
scr.addstr(y, x, " ")
# Draw the snake
for x, y in snake["position"]:
scr.addstr(y, x, snake["representation"])
scr.refresh()
# Sleep until next round
time.sleep(0.1)
if __name__ == "__main__":
print(__doc__)
print(main.__doc__)
input("Press enter to begin playing...")
curses.wrapper(main)