snake.game module

Holds the snake game.

The only thing necessary to run a game of snake is SnakeGame.

class SnakeGame(board_size, walls, random_seed)[source]

Bases: object

Represents a game of snake.

The game can be run with run(), or stepwise with run_stepwise(). The game runs in a self contained loop and will not take input from the keyboard or display itself on the screen. The game is interacted with purely programatically. However, you can write code that captures keyboard input and sends it to the game. You can also write code that looks at the state of the game, as contained in this class, and writes it to the screen. GameIO does both of these things.

The snake is controlled by setting its movement direction. This can be done with queue_snake_movement_direction(). The user will use this method to queue which direction the snake will move, the next time the snake takes a step. The queue allows the user to queue several steps ahead, which results in the game feeling more responsive when played.

SnakeGame can be initialized with walls, allowing the user to create a level.

If you want to interact with the game in an automated way you can do something like

def get_next_direction(game):
    # This is a user-defined function which decides on the
    # direction the snake should take on the next turn.

    ...

def apply_action(game):
    # This is a user-defined function which looks at the
    # state of the game an decides on sending actions to it.

    direction = get_next_direction(game)
    game.queue_snake_movement_direction(direction)

game = SnakeGame(
    board_size=(23, 34),
    walls=((1, 1), (2, 2), (3, 3)),
    random_seed=12,
)

for step_number in game.run_stepwise():
    apply_action(game)

Methods

get_apple(self) Return the coordinates of the apple.
get_board_size(self) Return the board size.
get_snake(self) Yield the positions occupied by the snake.
get_snake_length(self) Return the length of the snake.
get_snake_velocity(self[, step]) Return the step the snake will take.
get_walls(self) Yield the coordinates of the walls.
queue_snake_movement_direction(self, direction) Queue a movement direction for the snake.
run(self) Run the game.
run_stepwise(self) Run the game, but yield after every step.
__init__(self, board_size, walls, random_seed)[source]

Initialize a SnakeGame.

Parameters:
  • board_size (tuple) – A tuple of the form (23, 12) which represents the size of the board in the x and y directions.
  • walls (iterable of tuple) – An iterable holding the position of every wall segment.
  • random_seed (int) – The random seed to be used with the game. Used to generate apple locations.
get_apple(self)[source]

Return the coordinates of the apple.

Returns:A tuple of the form (21, 12), holding the coordinates of the apple the snake is meant to eat.
Return type:tuple
get_board_size(self)[source]

Return the board size.

Returns:A tuple of the form (23, 12) which represents the size of the board in the x and y directions.
Return type:tuple
get_snake(self)[source]

Yield the positions occupied by the snake.

Yields:tuple – The position of a segment of the snake’s body.
get_snake_length(self)[source]

Return the length of the snake.

Returns:The length of the snake.
Return type:int
get_snake_velocity(self, step=0)[source]

Return the step the snake will take.

Parameters:step (int, optional) – The step for which the velocity is returned. 0 is the current step.
Returns:The tuple can be one of (0, 1), (0, -1), (1, 0) or (-1, 0), representing the step the snake will take.
Return type:tuple
get_walls(self)[source]

Yield the coordinates of the walls.

Yields:tuple – The position of a wall segment.
queue_snake_movement_direction(self, direction)[source]

Queue a movement direction for the snake.

When this method is called multiple times between snake steps, it allows the caller to queue multiple directions, which will be resolved at a rate of one per step.

Parameters:direction (str) – Can be 'up', 'down', 'right' or 'left' to signify the movement direction the snake will have the when it moves.
Returns:True if a movement direction was successfully queued and False otherwise.
Return type:bool
run(self)[source]

Run the game.

Returns:None
Return type:NoneType
run_stepwise(self)[source]

Run the game, but yield after every step.

Yields:int – The step number.