snakeng package

Submodules

snakeng.snake module

class snakeng.snake.Direction[source]

Bases: object

Enumerates all possible directions

Variables:
  • LEFT (int) – Left movement direction

  • RIGHT (int) – Right movement direction

  • UP (int) – Upwards movement direction

  • DOWN (int) – Downwards movement direction

DOWN = 3
LEFT = 0
RIGHT = 1
UP = 2
class snakeng.snake.Position(x: int = 0, y: int = 0)[source]

Bases: object

Position of an object within the game area

__init__(x: int = 0, y: int = 0) None
deserialize(attrs)[source]
serialize()[source]
x: int = 0
y: int = 0
class snakeng.snake.SnakeGame(width=40, height=30, wall_wrap=True, initial_direction=3, fixed_speed=None, apples_disappear=True, apple_disappear_ticks=None)[source]

Bases: object

Represents a single instance of a snake game

__init__(width=40, height=30, wall_wrap=True, initial_direction=3, fixed_speed=None, apples_disappear=True, apple_disappear_ticks=None)[source]
Parameters:
  • width (int) – Game area width, in units of snake body segments

  • height (int) – Game area height, in units of snake body segments

  • wall_wrap (bool) – If True, the snake will die when it hits a wall. If False, the snake will ‘teleport’ through the wall and continue from the opposite wall.

  • initial_direction (int) – Initial movement direction for the snake. Expected to be one of the values defined under the Direction class.

  • fixed_speed (int) – If unset, then the snake speed will automatically increase as the snake size increases. If set to one of the values defined under the Speed class, then the snake speed will be set to the specified speed for the duration of the game, with no speed increases.

  • apples_disappear (bool) – If True, apples will disappear after a fixed period of time if not collected. If False, apples will stay forever until collected.

  • apple_disappear_ticks (int) – specifies number of frames before apple disappears. If set to None, a sane default will be used.

deserialize(game_state)[source]

Deserialize a saved game state from a dict and populate this instance with it

Parameters:

game_state (dict) – saved game state to deserialize

direction_input(direction)[source]

Provide a new directional input to the game

process()[source]

Process a single frame of the game, and return the new game state

Returns:

New game state

Return type:

SnakeGameState

serialize()[source]

Serialize the current game state to a dict suitable for json.dump

Returns:

serialized game state as dict

Return type:

dict

class snakeng.snake.SnakeGameState(area_width: int = 120, area_height: int = 120, snake_segments: ~typing.List = <factory>, snake_direction: int = 2, snake_speed: int = 4, fixed_speed: bool = False, score: int = 0, apple_position: ~snakeng.snake.Position = None, dead: bool = False, apples_disappear: bool = True, apple_disappear_ticks: int = None, apple_ticks: int = 0)[source]

Bases: object

Represents the state of the game for a single frame.

Variables:
  • area_width (int) – Width of the game area

  • area_height (int) – Height of the game area

  • snake_segments (list) – List of Position objects representing the current position of each segment of the snake

  • snake_direction (int) – Current movement direction of the snake, one of the constants defined by the Direction class

  • snake_speed (int) – Current movement speed of the snake, one of the constants defined by the Speed class

  • fixed_speed – If True, snake speed does not change relative to snake size

  • score (int) – Number of apples the snake has collected

  • apple_position – Position object representing the current position of the apple, or None if there is no apple.

  • dead (bool) – True if the snake has died

  • apples_disappear (bool) – If True, apples will disappear after a fixed period of time if not collected. If False, apples will stay forever until collected.

  • apple_disappear_ticks (int) – specifies number of frames before apple disappears.

  • apple_ticks (int) – Number of ticks elapsed for current apple.

__init__(area_width: int = 120, area_height: int = 120, snake_segments: ~typing.List = <factory>, snake_direction: int = 2, snake_speed: int = 4, fixed_speed: bool = False, score: int = 0, apple_position: ~snakeng.snake.Position = None, dead: bool = False, apples_disappear: bool = True, apple_disappear_ticks: int = None, apple_ticks: int = 0) None
apple_disappear_ticks: int = None
apple_position: Position = None
apple_ticks: int = 0
apples_disappear: bool = True
area_height: int = 120
area_width: int = 120
dead: bool = False
deserialize(attrs)[source]

Load the instance with values from a serialized dict

Parameters:

attrs (dict) – dict containing instance values

fixed_speed: bool = False
score: int = 0
serialize()[source]

Serialize the instance to a dict suitable for use with json.dump

Returns:

serialized data as a dict

Return type:

dict

snake_direction: int = 2
snake_segments: List
snake_speed: int = 4
to_string(frame_corner_char='+', frame_horiz_char='-', frame_vert_char='|', snake_head_left_char='<', snake_head_right_char='>', snake_head_up_char='^', snake_head_down_char='v', snake_body_char='#', apple_char='@', space_char=' ')[source]

Convert the instance to an ASCII string representing the current game state

Parameters:
  • frame_corner_char (str) – Character to use when drawing the corners of the game area boundary

  • frame_horiz_char (str) – Character to use when drawing the horizontal (top and bottom) sides of the game area boundary

  • frame_horiz_char – Character to use when drawing the vertical (left and right) sides of the game area boundary

  • snake_head_left_char (str) – Character to use for the snake head when snake is moving in the left direction

  • snake_head_right_char (str) – Character to use for the snake head when snake is moving in the right direction

  • snake_head_up_char (str) – Character to use for the snake head when snake is moving in the up direction

  • snake_head_down_char (str) – Character to use for the snake head when snake is moving in the down direction

  • snake_body_char (str) – Character to use when drawing the snake body segments

  • apple_char (str) – Character to use when drawing the apple

  • space_char (str) – Character to use when drawing empty space

class snakeng.snake.Speed[source]

Bases: object

Enumerates all possible movement speeds for the snake

Variables:
  • FASTER (int) – Fastest movement speed (1 segment per frame)

  • FAST (int) – Fast movement speed (1 segment every 2 frames)

  • MEDIUM (int) – Medium movement speed (1 segment every 3 frames)

  • SLOW (int) – Slowest movement speed (1 segment every 4 frames)

FAST = 2
FASTER = 1
MEDIUM = 3
SLOW = 4

Module contents