sprite

This module handles game “objects” with a visual representation - “sprites” - and all of their “actions”.

The sprite classes here are all subclasses of Pygame’s Sprite class.

  • Entity handles the living creature.

  • Food handles the food.

  • Marker handles visual representation of events.

Each group has an accompanying Group in order to deal with all of the sprites as a collection (subclassing Group):

class sprite.Entity(energy, speed, size)

Bases: pygame.sprite.Sprite

Class for a living creature.

A subclass of PyGame’s Sprite class that allows for automated method calls each game loop iteration, “action(s)”, essentially giving the sprite its life. This is done by overriding Sprite.update, a placeholder method that is called with EntityGroup.update() and its arguments. The overridden method contains all actions: moving, mating, eating, dying and modification of values.

Parameters
  • energy (int) – How much energy it has, depleted each turn by a formula (from config) using speed and size

  • speed (int) – How fast it moves each turn, self.rect.(direction) += speed.

  • size (int) – How big is is, in pixels (quadratic).

  • mating_counter (int) – To see if it can mate or not, counter > counter_check is a requirement in mate().

  • age_counter (int) – Its age. Mostly used in die() and mate().

  • image (Pygame.Surface) – What it looks like. Scaled by size.

  • rect (Pygame.Rect) – The rectangle, mostly used for move() and collision checks. Shortcut to self.image.get_rect().

move()

Vertical and horizontal sprite movement

If the condition direction_counter >= counter_check is true, choose a direction to go to and reset the counter.

This works by using PyGame’s Rect magic, incrementing “coordinate attributes” to move. If movement would collide with the border, the counter is set to the check value so that a new direction can be chosen next turn.

mate(entity_group, birth_marker_group)

Mating and birth if all checks are passed.

If a partner is found by collision check, and all of these conditions are met,

  • It and its partner has not mated recently (mating_counter > mating_counter_roof)

  • It and its partner is old enough (age_counter > age_counter_roof)

  • It and its partner has enough energy for minimal cost (energy > cfg.mating_energy_formula_min))

  • It and its partner has enough energy for the parents’ greatest energy cost (energy > greater_cost)

then a new class instance is created. For this new class instance:

  • Energy is equal to greatest_cost * 2 (simulating the parents giving their own share)

  • Speed and Size are equal to a mean value of the parents’ values, +-randint(min_gene_variation, max_gene_vaiation)

The actual “birth” is done in EntityGroup.populate() by calling it in this method.

Parameters
  • entity_group (EntityGroup) – Group of entities.

  • birth_marker_group (MarkerGroup) – Group of birth markers.

eat(food_group)

Consume food.

Check for collisions with food_group, kill any food sprite matches, give config.food_energy for each match.

Parameters

food_group (FoodGroup) – Group of foods

die(entity_group, starve_marker_group)

Die from starvation if conditions are met.

Starve Marker is set if death occurs.

Parameters
  • entity_group (EntityGroup) – Group of entities.

  • starve_marker_group (MarkerGroup) – Group of starve markers

modify_values()

Update all relevant attributes.

Energy is drained with a formula that is a function of speed and size (config.energy_drain_formula) Mating and age counters are incremented by 1. Direction counter is incremented by an integer in a random interval (config.direction_counter_increment)

update(entity_group, food_group, birth_marker_group, starve_marker_group)

Carry out all the actions in a turn.

Parameters
  • entity_group (EntityGroup) – Group of entities.

  • food_group (FoodGroup) – Group of foods.

  • birth_marker_group (MarkerGroup) – Group of birth markers.

  • starve_marker_group (MarkerGroup) – Group of starve markers.

class sprite.EntityGroup

Bases: pygame.sprite.Group

Container group for Entity.

This container is a subclass of Pygame’s Group. It handles birth in populate() and file saving in save_to_file() for statistic purpouses. loop() is called each turn, calling Entity.update(), save_to_file() and drawing the sprites.

populate(placement, birth_marker_group, **kwargs)

Add n new Entity instance(s) to the group.

This method is called for both “program-start” birth placement = None, n = 1 and birth by parents placement = (x, y), n != 1. Birth is marked by Marker.

Parameters
  • placement (Union[tuple, None]) – The coordinates of the new entity. If None, randomly set to within the border rect.

  • energy (int) – The energy of the new instance.

  • birth_marker_group (MarkerGroup) – Group of birth markers

  • kwargs (int, optional) – Passes other parameters to Entity, such as speed and size.

save_to_file(run, elapsed)

Save attributes of all entities to file, only triggered if statistics_counter passes the check.

Parameters
  • run (int or None) – What run the program is on. Will be None if program is not called from simulate.py.

  • elapsed (int) – In seconds, how long since the program started.

loop(run, elapsed, entity_group, food_group, birth_marker_group, starve_marker_group)

Looping method called each game turn.

This method does three things:

  • Call each of the entities’ update() method to do their actions (Entity.update()).

  • Save their attributes to file with save_to_file().

  • Draw them on the screen.

Parameters
  • run (int or None) – What run the program is on. Will be None if program is not called from simulate.py.

  • elapsed (int) – In seconds, how long since the program started.

  • entity_group (EntityGroup) – Group of entities.

  • food_group (FoodGroup) – Group of foods.

  • birth_marker_group (MarkerGroup) – Group of birth markers.

  • starve_marker_group (MarkerGroup) – Group of starve markers.

class sprite.Food

Bases: pygame.sprite.Sprite

Class for a food object.

This is a subclass of PyGame’s Sprite class, but much simpler than Entity. Its only purpose is be spawned by its PyGame Group (FoodGroup) and eaten by an Entity.

Parameters
  • image (Pygame.Surface) – What it looks like. Scaled by size.

  • rect (Pygame.Rect) – The rectangle representation of image. Shortcut to self.image.get_rect().

class sprite.FoodGroup

Bases: pygame.sprite.Group

Container group for Food

This container is a subclass of Pygame’s Group. Mainly of importance to spawn in the food.

Parameters

spawn_counter (int) – Counts up to config.food_spawn_counter_roof by random integer in interval config.food_spawn_counter_increment.

spawn()

Spawn config.food_amount amount of food.

Similar to Entity.populate() but much simpler.

class sprite.Marker(marked_sprite, duration_counter_roof, color)

Bases: pygame.sprite.Sprite

Marker for certain events.

A subclass of Pygame’s Sprite class which is instantiated after certain event, in order to visually clarify when something occurs. This is showed as a simple colored square (color indicates type of event), that disappears after duration_counter == duration_counter_roof

Current valid events:

  • starve: die of lack of energy

  • birth: be instantiated

Parameters
  • marked_sprite (Pygame.Sprite) – The marked Sprite, used to inherit the dimensions and placement.

  • duration_counter (int) – Elapsed duration.

  • duration_counter_roof (int) – When duration_counter == duration_counter_roof, the marker is removed.

  • color (tuple) – (R, G, B) color of the image.

  • image (Pygame.Rect) – What it looks like (single-color) . Scaled by marked_sprite.size.

  • rect (Pygame.Rect) – The rectangle representation of the class. Equivalent of self.image in this particular class.

update()

Check if duration_counter == duration_counter_roof, and kill itself if it is.

class sprite.MarkerGroup(duration_counter_roof, color)

Bases: pygame.sprite.Group

Group container for Marker.

Parameters
  • duration_counter_roof (int) – When duration_counter == duration_counter_roof, the marker is removed.

  • color (tuple) – Color of the image.

create(marked_sprite)

Create and add a new marker to the container.

Parameters

marked_sprite (Pygame.Sprite) – The marked sprite in question, used to inherit dimensions and placement of the marker.

loop()

Looping method called each turn, updates markers and draws them to the screen.