A dynamic ecosystem simulation built with Python and Pygame. Watch an autonomous world evolve where Preys hunt for food while escaping Predators, creating a natural life cycle of growth, extinction, and balance.
- Autonomous Agents: Entities use vector mathematics to calculate distances, flee from threats, and chase targets.
- Metabolism System: Every movement costs energy. Agents must eat to survive; otherwise, they starve.
- Reproduction: Agents that gather enough energy replicate, passing their traits to the next generation.
- Dynamic Environment: Food (grass) grows naturally over time, but can be depleted by overpopulation.
- Real-time Interaction: Add new predators dynamically with a mouse click to disrupt the balance.
The simulation follows three main rules of nature:
-
๐ข Prey (Green):
- Wanders randomly when safe.
- Senses predators nearby and flees in the opposite direction.
- Seeks food (White dots) when hungry.
- Reproduces when energy is high.
-
๐ด Predators (Red):
- Actively hunts the nearest Prey.
- Consumes more energy than Preys (requires constant hunting).
- Dies of starvation if the Prey population collapses.
-
โช Grass (White):
- Grows randomly over time.
- Sustains the Prey population.
world.py: The main engine. Handles the game loop, drawing, collision detection, and population management.prey.py: Defines the Prey class (fleeing and foraging logic).predator.py: Defines the Predator class (hunting logic).
You need Python installed on your machine.
It is recommended to use a virtual environment:
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtpython world.py