7
7
MEDIUM_SCORE_THRESHOLD = 25
8
8
FAST_SCORE_THRESHOLD = 70
9
9
10
-
11
10
class Direction (object ):
12
11
"""
13
12
Enumerates all possible directions
@@ -185,6 +184,10 @@ class SnakeGameState(object):
185
184
:ivar apple_position: Position object representing the current position of \
186
185
the apple, or None if there is no apple.
187
186
:ivar bool dead: True if the snake has died
187
+ :ivar bool apples_disappear: If True, apples will disappear after a fixed period \
188
+ of time if not collected. If False, apples will stay forever until collected.
189
+ :ivar int apple_disappear_ticks: specifies number of frames before apple disappears. \
190
+ If set to None, a sane default will be used.
188
191
"""
189
192
area_width : int = 120
190
193
area_height : int = 120
@@ -195,7 +198,8 @@ class SnakeGameState(object):
195
198
score : int = 0
196
199
apple_position : Position = None
197
200
dead : bool = False
198
-
201
+ apples_disappear : bool = True
202
+ apple_disappear_ticks : int = None
199
203
200
204
def serialize (self ):
201
205
"""
@@ -212,7 +216,9 @@ def serialize(self):
212
216
'score' : self .score ,
213
217
'apple_position' : self .apple_position .serialize (),
214
218
'snake_speed' : self .snake_speed ,
215
- 'fixed_speed' : self .fixed_speed
219
+ 'fixed_speed' : self .fixed_speed ,
220
+ 'apples_disappear' : self .apples_disappear ,
221
+ 'apple_disappear_ticks' : self .apple_disappear_ticks
216
222
}
217
223
218
224
def deserialize (self , attrs ):
@@ -229,6 +235,8 @@ def deserialize(self, attrs):
229
235
self .apple_position = Position ().deserialize (attrs ['apple_position' ])
230
236
self .snake_speed = attrs ['snake_speed' ]
231
237
self .fixed_speed = attrs ['fixed_speed' ]
238
+ self .apples_disappear = attrs ['apples_disappear' ]
239
+ self .apple_disappear_ticks = attrs ['apple_disappear_ticks' ]
232
240
return self
233
241
234
242
def to_string (self , frame_corner_char = '+' , frame_horiz_char = '-' , frame_vert_char = '|' ,
@@ -310,7 +318,7 @@ class SnakeGame(object):
310
318
Represents a single instance of a snake game
311
319
"""
312
320
def __init__ (self , width = 40 , height = 30 , wall_wrap = True , initial_direction = Direction .DOWN ,
313
- fixed_speed = None ):
321
+ fixed_speed = None , apples_disappear = True , apple_disappear_ticks = None ):
314
322
"""
315
323
:param int width: Game area width, in units of snake body segments
316
324
:param int height: Game area height, in units of snake body segments
@@ -322,10 +330,15 @@ def __init__(self, width=40, height=30, wall_wrap=True, initial_direction=Direct
322
330
the snake size increases. If set to one of the values defined under the Speed \
323
331
class, then the snake speed will be set to the specified speed for the duration of \
324
332
the game, with no speed increases.
333
+ :param bool apples_disappear: If True, apples will disappear after a fixed period \
334
+ of time if not collected. If False, apples will stay forever until collected.
335
+ :param int apple_disappear_ticks: specifies number of frames before apple disappears. \
336
+ If set to None, a sane default will be used.
325
337
"""
326
338
head_pos = Position (x = int (width / 2 ), y = int (height / 2 ))
327
339
328
- self .state = SnakeGameState (area_width = width , area_height = height , snake_direction = initial_direction )
340
+ self .state = SnakeGameState (area_width = width , area_height = height , snake_direction = initial_direction ,
341
+ apples_disappear = apples_disappear )
329
342
self .state .snake_segments .append (head_pos )
330
343
331
344
self .state .apple_position = self ._new_apple_position ()
@@ -336,6 +349,13 @@ def __init__(self, width=40, height=30, wall_wrap=True, initial_direction=Direct
336
349
self .table = [[0 for _ in range (width )] for _ in range (height )]
337
350
self .table [head_pos .y ][head_pos .x ] = 1
338
351
352
+ if apple_disappear_ticks is None :
353
+ self .state .apple_disappear_ticks = max (width , height )
354
+ else :
355
+ self .state .apple_disappear_ticks = apple_disappear_ticks
356
+
357
+ self .apple_ticks = 0
358
+
339
359
if fixed_speed is not None :
340
360
self .state .fixed_speed = True
341
361
self .state .snake_speed = fixed_speed
@@ -458,11 +478,19 @@ def process(self, direction_input=None):
458
478
# Check if hit apple, delete apple and increment score if so
459
479
if new_head == self .state .apple_position :
460
480
self .state .apple_position = self ._new_apple_position ()
481
+ self .apple_ticks = 0
461
482
self .state .score += 1
462
483
else :
463
484
tail = self .state .snake_segments .pop (0 )
464
485
self .table [tail .y ][tail .x ] = 0
465
486
487
+ if self .state .apples_disappear :
488
+ if self .apple_ticks >= self .state .apple_disappear_ticks :
489
+ self .state .apple_position = self ._new_apple_position ()
490
+ self .apple_ticks = 0
491
+
492
+ self .apple_ticks += 1
493
+
466
494
# Check if we crossed a score threshold
467
495
if not self .state .fixed_speed :
468
496
if self .state .snake_speed == Speed .SLOW :
0 commit comments