engine.snake documentation - wladekpal/The-Lazy-Snek GitHub Wiki

engine.snake.Snake

Snake class keeps track of snake location, state and provides interface for interacting with snake.

Properties:

  • is_alive - Boolean value informing about snake state.
  • grow_at_next_move - Boolean value informing whether snake should grow during the next move sequence.
  • infinite_grow - Boolean value informing whether snake sould grow at every subsequent move.
  • segments - list of coordinate pairs holding snake segments location. First segment on list is tail, last is the head.
  • color - string value holding snake color, snake textures will depend on this value.
  • direction - engine.direction.Direction object holding snake's head orientation.
  • board - engine.board.Board object that snake is located on.
  • head_texture, body_texture, bent_body_texture, tail_texture - pygame.image objects holding segments textures.

Constructor

Takes segments, color, direction, board. Constructor method, sets class attributes, the path to textures images is built using color argument. Additionally this methods informs fields, that snake has been placed on them.

get_direction_betwen_segments(segment, other_segments)

Method used to determine the direction between the first of given segments, and the second one, it returns a string informing of direction. If segments are not in one of possible directions engine.snake.BadSegmentOrientation is raised.

calculate_neighbours_directions(segment)

Method used to determine the orientation of a segment based on it's neighbours location. It returns string describing orientation of given segment.

get_segment_texture(segment, neighbours_directions)

Method used to return the correct texture for given segment, the texture can be rotated in wrong way.

draw_segment(frame, draw_coords, side_length, segment_coords)

Method used to draw given segment on pygame.frame object. It takes pygame frame, coordinates to draw segment to, length of drawn segment and coordinates of segment on board. If segment is not present in snake it will raise engine.snake.SegmentNotInSnake.

move()

Method used to move the snake according to direction attribute. The method first checks whether the engine.field.Field can be moved to, if not snake is destroyed. Then snake segments are moved, and if grow_at_next_move attribute is set to True the snake grows. Finally snake informs Field, about the move.

grow()

Method used to ensure, that snake will grow at next move. The grow_at_next_move attribute is set to True.

enable_infinite_grow()

Method used to ensure, that snake will grow at every move, until it's death. The infinite_grow attribute is set to True.

destroy()

Methods used to inform snake, that it has to be destroyed. Snake removes itself from all the occupied Field objects and sets is_alive attribute to False.

interact_with_snake(other_snake)

Method used to handle snake collision. Snake is informed, that other_snake colided with it. If the snakes heads colided both are destroyed, otherwise only other_snake is destroyed.

reverse()

Method used to reverse snake. This method reverses the list of segments as well as the direction of the snake.