/
snake_d.py
338 lines (243 loc) · 7.93 KB
/
snake_d.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import unicurses as curses
import time
import os
from random import randint, choice
os.environ['ESCDELAY'] = '25'
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.keypad(stdscr, 1)
curses.wborder(stdscr)
curses.curs_set(0)
middley, middlex = map(lambda x: x//2, curses.getmaxyx(stdscr)) # get middle coordinates depending on the size of the terminal
UP = 259 # ascii key codes
DOWN = 258
LEFT = 261
RIGHT = 260
asci ='''
_____ _ _ _ ________
/ ____| \ | | /\ | |/ / ____|
| (___ | \| | / \ | ' /| |__
\___ \| . ` | / /\ \ | < | __|
____) | |\ |/ ____ \| . \| |____
|_____/|_| \_/_/ \_\_|\_\______|'''
def reset_game():
'''
>>> reset_game()
False
'''
curses.clear()
curses.refresh()
return False
def get_key(current_direction):
# utility function to control snake with users input
# doctest for main functions
#'''
#>>> get_key(DOWN)
#258
#>>> get_key(UP)
#259
#>>> get_key(LEFT)
#261
#>>> get_key(RIGHT)
#260
#>>> get_key(27)
#27
#'''
KEY = curses.getch()
if KEY == UP:
if current_direction == DOWN:
return DOWN
else:
return UP
elif KEY == DOWN:
if current_direction == UP:
return UP
else:
return DOWN
elif KEY == RIGHT:
if current_direction == LEFT:
return LEFT
else:
return RIGHT
elif KEY == LEFT:
if current_direction == RIGHT:
return RIGHT
else:
return LEFT
elif KEY == 27:
return 27
else:
return current_direction
class Menu:
def speed_menu(self):
# draws the speed menu
curses.mvwaddstr(stdscr, 8, middlex-7, 'Choose speed')
curses.mvwaddstr(stdscr, 9, middlex-7, '<---------->')
curses.wborder(stdscr)
curses.refresh
def draw_snake(self):
# draws the snake ascii art
for y, line in enumerate(asci.splitlines()):
curses.mvwaddstr(stdscr, y, middlex-20, line)
curses.wborder(stdscr)
curses.refresh()
def main_menu(self):
# draws the main menu
curses.wborder(stdscr)
curses.mvwaddstr(stdscr, 8, middlex-5, 'New Game')
curses.mvwaddstr(stdscr, 9, middlex-5 ,'Set Speed')
curses.mvwaddstr(stdscr, 10, middlex-5 ,'Quit')
curses.refresh()
def main_cursor(self, location):
# draws the cursor in main menu
curses.mvwaddstr(stdscr, 8, middlex-7, ' ')
curses.mvwaddstr(stdscr, 9, middlex-7, ' ')
curses.mvwaddstr(stdscr, 10, middlex-7, ' ')
if location == 0:
curses.mvwaddstr(stdscr, 8, middlex-7, '#')
elif location == 1:
curses.mvwaddstr(stdscr, 9, middlex-7, '#')
else:
curses.mvwaddstr(stdscr, 10, middlex-7, '#')
curses.refresh()
def speed_cursor(self, location):
# draws the cursor in speed menu
for i in range(10):
curses.mvwaddstr(stdscr, 9, middlex-i+3, '-')
curses.mvwaddstr(stdscr, 9, middlex-7+location, '#')
curses.refresh()
class Snake:
def __init__(self):
self.y = 20
self.x = 20
self.lenght = 3
self.count = 0
self.reg = {}
self.sy, self.sx = curses.getmaxyx(stdscr)
self.starting_lenght = self.lenght
def food(self):
#return location of a new food and grow the snake by one
while True:
self.dot = (randint(2,self.sy-2), randint(2, self.sx-2))
if self.dot not in self.reg.values():
break
self.lenght+=1
return self.dot
def get_status(self):
if self.y == self.sy-1 or self.y == 0:
return 'dead'
elif self.x == self.sx-1 or self.x == 0:
return 'dead'
elif len([i for i in self.reg.values() if i == (self.y, self.x)]) == 2:
return 'dead'
elif (self.y, self.x) == self.dot:
return 'grow'
else:
return False
def draw(self, direction):
# draw the pixel
curses.mvwaddstr(stdscr, *self.dot, 'o')
if direction == UP:
self.y -= 1
if direction == DOWN:
self.y += 1
if direction == RIGHT:
self.x -= 1
if direction == LEFT:
self.x += 1
curses.mvwaddstr(stdscr, self.y ,self.x, '#')
curses.refresh()
self.reg[self.count] = (self.y,self.x) # a register of previous coords
self.count +=1
def delete(self, lenght):
# delete the pixel from snake's tail
try:
curses.mvwaddstr(stdscr, *self.reg[self.count-self.lenght], ' ')
del self.reg[self.count-self.lenght]
except KeyError:
pass
def speed_menu():
# main loop for speed menu
global SPEED
while True:
reset_game()
menu.draw_snake()
menu.speed_menu()
menu.speed_cursor(SPEED)
KEY = curses.getch()
if KEY == 27 or KEY == 10 or KEY == 459:
break
if KEY == RIGHT and SPEED > 1:
SPEED -=1
menu.speed_menu()
menu.speed_cursor(SPEED)
if KEY == LEFT and SPEED < 10:
SPEED +=1
menu.speed_menu()
menu.speed_cursor(SPEED)
def main_menu():
# main loop for main menu
global CURSOR
while True:
curses.nodelay(stdscr, False)
menu.draw_snake()
menu.main_menu()
menu.main_cursor(CURSOR)
KEY = curses.getch()
if KEY == UP and CURSOR > 0:
CURSOR-=1
elif KEY == DOWN and CURSOR < 2:
CURSOR+=1
elif KEY == 27:
reset_game()
break
elif KEY == 10 or KEY == 459:
if CURSOR == 0:
reset_game()
curses.wborder(stdscr)
game(SPEED)
elif CURSOR == 1:
reset_game()
speed_menu()
reset_game()
elif CURSOR == 2:
reset_game()
break
def game(speed):
snake.food()
# main loop for the game
global KEY
curses.nodelay(stdscr, True)
while True:
KEY = get_key(KEY)
if snake.get_status() == 'dead' or KEY == 27:
# game over
curses.clear()
curses.wborder(stdscr)
curses.mvwaddstr(stdscr, middley-1, middlex-6, '--GAME OVER--')
curses.mvwaddstr(stdscr, middley, middlex-8,'Your score is -')
curses.mvwaddstr(stdscr, middley, middlex+8, snake.lenght - snake.starting_lenght-1)
curses.mvwaddstr(stdscr, middley+1, middlex-8,'Press Esc to quit')
KEY = choice([RIGHT, LEFT])
snake.food()
curses.refresh()
curses.nodelay(stdscr, False)
while get_key(KEY) != 27:
pass
reset_game()
snake.__init__()
break
if snake.get_status() == 'grow':
# eaten food
snake.food()
snake.draw(KEY)
snake.delete(snake.lenght)
time.sleep(1/speed/2)
if __name__ == "__main__":
snake = Snake()
menu = Menu()
KEY = choice([RIGHT, LEFT]) # choose random direction for snake to go at start
CURSOR = 0
SPEED = 5
main_menu()