-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
293 lines (246 loc) · 7.47 KB
/
snake.py
File metadata and controls
293 lines (246 loc) · 7.47 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
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
import curses
from time import sleep
from random import randint, random
from sys import platform
class TimeBar:
def __init__(self, limit, MAX, y, delay):
self.LIMIT = 100
self._pause = False
self.unit = limit / MAX
self.head = self.MAX = MAX
self.y = y
self.count = 0
self.delay = delay
w.timeout(delay)
self.refill()
def reduce(self):
if self._pause:
return
self.count += 1
if self.count >= self.unit:
self.count -= self.unit
w.addstr(self.y, self.head, '<', curses.color_pair(2))
self.head -= 1
if self.head == -1:
death()
def refill(self):
self._pause = False
w.addstr(self.y, 0, ' ' * self.MAX, curses.color_pair(1))
self.head = self.MAX
self.delay = self.delay * 0.98
w.timeout(int(self.delay))
def pause(self):
self._pause = True
class Food:
def __init__(self, pos, s=False):
self.pos = pos
w.addstr(0, 0, str(self.pos)) # debug
w.refresh()
self.draw()
def draw(self):
draw_block(self.pos)
def tick(self):
return True
def consume(self):
timer.refill()
class PassFood(Food):
def draw(self):
draw_block(self.pos, 5)
def consume(self):
pass
class TimeLimitFood(Food):
def __init__(self, pos):
self.time = int((2-random()**3)*40)
self.count = 0
self.INTERVAL = 3
self.blink = 1
Food.__init__(self, pos)
def draw(self):
if self.blink:
draw_block(self.pos)
else:
draw_block(self.pos, 0)
def wipe(self):
draw_block(self.pos, 0)
def tick(self):
self.count += 1
if self.count > self.INTERVAL:
self.count = 0
self.blink = 1 - self.blink
self.time -= 1
if self.time == -1:
self.wipe()
return False
self.draw()
return True
class PauseFood(TimeLimitFood):
def draw(self):
if self.blink:
draw_block(self.pos, 4)
else:
draw_block(self.pos, 0)
def consume(self):
timer.pause()
class BonusFood(TimeLimitFood):
def draw(self):
if self.blink:
draw_block(self.pos, 5)
else:
draw_block(self.pos, 4)
def consume(self):
if self.blink:
for i in range(10):
foods.produce()
else:
snake.hidden = 10
timer.refill()
class FoodMgr:
DISTRIBUTION_SERIES = ((0.2, TimeLimitFood),
(0.05, PauseFood),
(0.1, PassFood),
(0.05, BonusFood))
def __init__(self):
self.foods = []
self.produce()
def update(self, head):
for food in self.foods:
if not food.tick():
self.foods.remove(food)
continue
if food.pos == head:
food.consume()
self.foods.remove(food)
ratio = 1 - 0.2 * len(self.foods) + 0.02 * snake.length()
flag = True
break
else:
ratio = 0.05 - 0.02 * len(self.foods) + 0.002 * snake.length()
flag = False
if random() < ratio:
self.produce()
return flag
def produce(self):
excludes = snake.body() + list(map(lambda f: f.pos, self.foods))
def food_pos(): return (randint(1, hei-1), randint(1, wei-1))
new_food_pos = food_pos()
while new_food_pos in excludes:
new_food_pos = food_pos()
rand_point = random()
stop_point = 0
for ratio, food_factory in self.DISTRIBUTION_SERIES:
stop_point += ratio
if rand_point < stop_point:
self.foods.append(food_factory(new_food_pos))
break
else:
self.foods.append(Food(new_food_pos))
class Snake:
def __init__(self):
y = int(hei/2)
x = int(wei/4)
self._snake = [(y, x), (y, x-1), (y, x-2)]
#self.keys = [ord('w'), ord('a'), ord('s'), ord('d')]
self.keys = [curses.KEY_UP, curses.KEY_LEFT,
curses.KEY_DOWN, curses.KEY_RIGHT]
self.key = self.keys[3]
self.hidden = 0
def body(self):
return self._snake
def length(self):
return len(self._snake)
def go(self, next_key):
if next_key in self.keys and (self.keys.index(self.key)-self.keys.index(next_key)) % 2 != 0:
self.key = next_key
w.addstr(1, 0, 'Current key= %s' % chr(self.key)) # debug
new_head = list(self._snake[0])
key = self.keys.index(self.key)
if key == 0:
new_head[0] -= 1
if key == 1:
new_head[1] -= 1
if key == 2:
new_head[0] += 1
if key == 3:
new_head[1] += 1
self._snake.insert(0, tuple(new_head))
def test_death(self):
return self._snake[0][0] in (-1, hei) or self._snake[0][1] in (-1, wei) or self._snake[0] in self._snake[1:]
def bite(self):
w.addstr(3, 0, str(self._snake[0])) # debug
if not foods.update(self._snake[0]):
draw_block(self._snake.pop(), 0)
w.addstr(2, 0, "Length:%s" % len(self._snake)) # debug
def move(self):
if self.hidden:
draw_block(self._snake[0], 2)
self.hidden -= 1
else:
draw_block(self._snake[0], 3)
def draw_block(pos, color=1, style=' '):
y, x = pos
w.addstr(y, 2*x, style, curses.color_pair(color))
def init_curses():
# initialize the screen
s = curses.initscr()
curses.curs_set(0)
curses.noecho()
curses.start_color()
# initialize the window
global hei, wei, w
if platform == 'win32':
curses.resize_term(40, 80)
hei, wei = s.getmaxyx()
wei = wei if wei % 2 == 0 else wei-1
w = curses.newwin(hei, wei, 0, 0)
w.keypad(1)
wei = wei//2
hei -= 1
# initialize color_pairs
curses.init_pair(1, curses.COLOR_MAGENTA, curses.COLOR_GREEN)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_MAGENTA)
curses.init_pair(5, curses.COLOR_BLUE, curses.COLOR_YELLOW)
def death():
for i in range(0, 4):
curses.flash()
sleep(0.08)
w.clear()
w.addstr(int(hei/2), int(wei), 'Game Over!', curses.color_pair(2))
w.refresh()
sleep(1.5)
curses.endwin()
quit()
# start
def main():
# initialize the position of snake
global snake
global timer, foods
snake = Snake()
timer = TimeBar(100, wei * 2 - 2, hei, 150)
foods = FoodMgr()
# foods.foods.append(BonusFood((hei//2, 22)))
# foods.foods.append(Food((hei//2, 24)))
while True:
next_key = w.getch()
timer.reduce()
snake.go(next_key)
# pause and resume
if next_key == ord('p') and snake.key in keys:
w.addstr(2, int(wei), "Pause")
while True:
a = w.getch()
if a == ord('r'):
w.addstr(2, int(wei), " ")
break
# death
if snake.test_death():
death()
snake.bite()
snake.move()
if __name__ == '__main__':
try:
init_curses()
main()
finally:
curses.endwin()