Repository navigation
Expand file tree
/
Copy pathsnake.py
More file actions
executable file
·127 lines (97 loc) · 2.92 KB
/
Copy pathsnake.py
File metadata and controls
executable file
·127 lines (97 loc) · 2.92 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
"""
Testing out the curses lib.
"""
#from curses import wrapper
import curses
import random
def main(screen):
"""
Snake
Press 'q' to quit the game.
"""
screen.clear()
curses.curs_set(0)
screen.nodelay(1)
curses.start_color()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
# Get the y,x of the window
dims = screen.getmaxyx()
# Get the center of the window
center = (dims[0]//2), (dims[1]//2)
screen.border()
# Main loop
x = center[1]
y = center[0]
dirs = {"x": 1, "y": 0}
#screen.move(0, 0);
screen.refresh()
snake = [[y, x], [y, x-1], [y, x-2]]
apple = [random.randint(1, dims[0]-2), random.randint(1, dims[1]-2)]
appleCh = "ö"
screen.addstr(apple[0], apple[1], appleCh, curses.color_pair(1))
ch = "O"
screen.addstr(snake[0][0], snake[0][1], ch, curses.color_pair(2))
score = 0
while True:
screen.border()
screen.addstr(0, 2, "Score : " + str(score) + " ")
key = screen.getch()
if key == ord("q"):
#screen.clear()
text = "GAME OVER!"
screen.addstr(dims[0]//2, dims[1]//2-len(text)//2, text)
screen.refresh()
curses.napms(2500)
break
elif key == curses.KEY_UP:
dirs["y"] = -1
dirs["x"] = 0
elif key == curses.KEY_DOWN:
dirs["y"] = 1
dirs["x"] = 0
elif key == curses.KEY_LEFT:
dirs["x"] = -1
dirs["y"] = 0
elif key == curses.KEY_RIGHT:
dirs["x"] = 1
dirs["y"] = 0
x += dirs["x"]
y += dirs["y"]
# head of snake
if y <= 0:
y = 1
elif y >= dims[0]-1:
y = dims[0] - 2
if x <= 0:
x = 1
elif x >= dims[1]-1:
x = dims[1] - 2
snake.insert(0, [y, x])
if snake[0] in snake[1:]:
#screen.clear()
text = "GAME OVER!"
screen.addstr(dims[0]//2, dims[1]//2-len(text)//2, text)
screen.refresh()
curses.napms(2500)
break
if snake[0] == apple:
score += 1
while apple in snake:
apple = [random.randint(2, dims[0]-3), random.randint(2, dims[1]-3)]
screen.addstr(apple[0], apple[1], appleCh, curses.color_pair(1))
else:
last = snake.pop()
screen.addch(last[0], last[1], " ", curses.color_pair(2))
screen.addch(snake[0][0], snake[0][1], ch, curses.color_pair(2))
#time.sleep(0.1)
nap_time = 100 - len(snake)//2
curses.napms(nap_time)
curses.endwin()
print("Final score:", score)
if __name__ == "__main__":
print(__doc__)
print(main.__doc__)
input("Press enter to begin playing...")
curses.wrapper(main)