8
8
FAST_SCORE_THRESHOLD = 70
9
9
10
10
11
- class SnakeDirection (object ):
11
+ class Direction (object ):
12
12
"""
13
13
Enumerates all possible directions
14
14
@@ -23,7 +23,7 @@ class SnakeDirection(object):
23
23
DOWN = 3
24
24
25
25
26
- class SnakeSpeed (object ):
26
+ class Speed (object ):
27
27
"""
28
28
Enumerates all possible movement speeds for the snake
29
29
@@ -39,10 +39,10 @@ class SnakeSpeed(object):
39
39
40
40
41
41
_MOVEMAP = {
42
- SnakeDirection .LEFT : (- 1 , 0 ),
43
- SnakeDirection .RIGHT : (1 , 0 ),
44
- SnakeDirection .UP : (0 , 1 ),
45
- SnakeDirection .DOWN : (0 , - 1 )
42
+ Direction .LEFT : (- 1 , 0 ),
43
+ Direction .RIGHT : (1 , 0 ),
44
+ Direction .UP : (0 , 1 ),
45
+ Direction .DOWN : (0 , - 1 )
46
46
}
47
47
48
48
@@ -82,9 +82,9 @@ class SnakeGameState(object):
82
82
:ivar list snake_segments: List of Position objects representing the current \
83
83
position of each segment of the snake
84
84
:ivar int snake_direction: Current movement direction of the snake, one of \
85
- the constants defined by the SnakeDirection class
85
+ the constants defined by the Direction class
86
86
:ivar int snake_speed: Current movement speed of the snake, one of \
87
- the constants defined by the SnakeSpeed class
87
+ the constants defined by the Speed class
88
88
:ivar fixed_speed: If True, snake speed does not change relative to snake size
89
89
:ivar int score: Number of apples the snake has collected
90
90
:ivar apple_position: Position object representing the current position of \
@@ -94,8 +94,8 @@ class SnakeGameState(object):
94
94
area_width : int = 120
95
95
area_height : int = 120
96
96
snake_segments : List = field (default_factory = lambda : [])
97
- snake_direction : int = SnakeDirection .UP
98
- snake_speed : int = SnakeSpeed .SLOW
97
+ snake_direction : int = Direction .UP
98
+ snake_speed : int = Speed .SLOW
99
99
fixed_speed : bool = False
100
100
score : int = 0
101
101
apple_position : Position = None
@@ -183,13 +183,13 @@ def to_string(self, frame_corner_char='+', frame_horiz_char='-', frame_vert_char
183
183
table [pos .y ][pos .x ] = snake_body_char
184
184
185
185
snake_head_char = '#'
186
- if self .snake_direction == SnakeDirection .UP :
186
+ if self .snake_direction == Direction .UP :
187
187
snake_head_char = snake_head_up_char
188
- elif self .snake_direction == SnakeDirection .DOWN :
188
+ elif self .snake_direction == Direction .DOWN :
189
189
snake_head_char = snake_head_down_char
190
- elif self .snake_direction == SnakeDirection .LEFT :
190
+ elif self .snake_direction == Direction .LEFT :
191
191
snake_head_char = snake_head_left_char
192
- elif self .snake_direction == SnakeDirection .RIGHT :
192
+ elif self .snake_direction == Direction .RIGHT :
193
193
snake_head_char = snake_head_right_char
194
194
195
195
snakehead = self .snake_segments [- 1 ]
@@ -214,17 +214,17 @@ class SnakeGame(object):
214
214
"""
215
215
Represents a single instance of a snake game
216
216
"""
217
- def __init__ (self , width = 40 , height = 30 , wall_wrap = True , initial_direction = SnakeDirection .DOWN ,
217
+ def __init__ (self , width = 40 , height = 30 , wall_wrap = True , initial_direction = Direction .DOWN ,
218
218
fixed_speed = None ):
219
219
"""
220
220
:param int width: Game area width, in units of snake body segments
221
221
:param int height: Game area height, in units of snake body segments
222
222
:param bool wall_wrap: If True, the snake will die when it hits a wall. If False, \
223
223
the snake will 'teleport' through the wall and continue from the opposite wall.
224
224
:param int initial_direction: Initial movement direction for the snake. Expected to \
225
- be one of the values defined under the SnakeDirection class.
225
+ be one of the values defined under the Direction class.
226
226
:param int fixed_speed: If unset, then the snake speed will automatically increase as \
227
- the snake size increases. If set to one of the values defined under the SnakeSpeed \
227
+ the snake size increases. If set to one of the values defined under the Speed \
228
228
class, then the snake speed will be set to the specified speed for the duration of \
229
229
the game, with no speed increases.
230
230
"""
@@ -277,13 +277,13 @@ def _new_apple_position(self):
277
277
def _wall_collision (self , x , y ):
278
278
ret = None
279
279
if x <= 0 :
280
- ret = SnakeDirection .LEFT
280
+ ret = Direction .LEFT
281
281
elif x >= (self .state .area_width - 1 ):
282
- ret = SnakeDirection .RIGHT
282
+ ret = Direction .RIGHT
283
283
elif y <= 0 :
284
- ret = SnakeDirection .DOWN
284
+ ret = Direction .DOWN
285
285
elif y >= (self .state .area_height - 1 ):
286
- ret = SnakeDirection .UP
286
+ ret = Direction .UP
287
287
288
288
return ret
289
289
@@ -309,22 +309,22 @@ def _new_head(self):
309
309
self .state .dead = True
310
310
else :
311
311
# Wrap new snake head position around to the opposite wall
312
- if collision_dir == SnakeDirection .LEFT :
312
+ if collision_dir == Direction .LEFT :
313
313
newx = self .state .area_width - 2
314
- elif collision_dir == SnakeDirection .RIGHT :
314
+ elif collision_dir == Direction .RIGHT :
315
315
newx = 1
316
- elif collision_dir == SnakeDirection .UP :
316
+ elif collision_dir == Direction .UP :
317
317
newy = 1
318
- elif collision_dir == SnakeDirection .DOWN :
318
+ elif collision_dir == Direction .DOWN :
319
319
newy = self .state .area_height - 2
320
320
321
321
return Position (x = newx , y = newy )
322
322
323
323
def _opposite_dirs (self , dir1 , dir2 ):
324
324
dirs = [dir1 , dir2 ]
325
- if (SnakeDirection .UP in dirs ) and (SnakeDirection .DOWN in dirs ):
325
+ if (Direction .UP in dirs ) and (Direction .DOWN in dirs ):
326
326
return True
327
- elif (SnakeDirection .LEFT in dirs ) and (SnakeDirection .RIGHT in dirs ):
327
+ elif (Direction .LEFT in dirs ) and (Direction .RIGHT in dirs ):
328
328
return True
329
329
330
330
return False
@@ -370,14 +370,14 @@ def process(self, direction_input=None):
370
370
371
371
# Check if we crossed a score threshold
372
372
if not self .state .fixed_speed :
373
- if self .state .snake_speed == SnakeSpeed .SLOW :
373
+ if self .state .snake_speed == Speed .SLOW :
374
374
if self .state .score >= SLOW_SCORE_THRESHOLD :
375
- self .state .snake_speed = SnakeSpeed .MEDIUM
376
- elif self .state .snake_speed == SnakeSpeed .MEDIUM :
375
+ self .state .snake_speed = Speed .MEDIUM
376
+ elif self .state .snake_speed == Speed .MEDIUM :
377
377
if self .state .score >= MEDIUM_SCORE_THRESHOLD :
378
- self .state .snake_speed = SnakeSpeed .FAST
379
- elif self .state .snake_speed == SnakeSpeed .FAST :
378
+ self .state .snake_speed = Speed .FAST
379
+ elif self .state .snake_speed == Speed .FAST :
380
380
if self .state .score >= FAST_SCORE_THRESHOLD :
381
- self .state .snake_speed = SnakeSpeed .FASTER
381
+ self .state .snake_speed = Speed .FASTER
382
382
383
383
return self .state
0 commit comments