This project implements a natural selection simulation using the Pygame library. Organisms move, reproduce, eat, and eventually die in an environment with different types of terrain, simulating the basic principles of evolution and adaptation to the environment. When reproducing, organisms pass their characteristics to their offspring within a variation, allowing evolution over time.
The project structure is organized as follows:
natural-selection-simulation
├── main.py
├── constants.py
├── terrain.py
├── organism.py
├── utils.py
├── assets
│ ├── __init__.py
│ └── font.py
└── demo.gif
main.py
: Entry point of the program. Sets up the Pygame window, manages the main loop, and draws elements on the screen.constants.py
: Contains constants used throughout the project, such as sizes, colors, rates, and simulation parameters.terrain.py
: Responsible for drawing the terrain on the screen.organism.py
: Defines the Organism class, which represents the organisms in the simulation. Includes methods to move, eat, reproduce, and draw the organisms.utils.py
: Contains utility functions, such as creating food and drawing the organisms.assets
: Folder containing additional resources. In this case, it includes font loading.demo.gif
: Demonstration of the simulation in action.
- Python 3.x
- Pygame
- Clone this repository:
git clone https://github.com/your-username/natural-selection-simulation.git
- Navigate to the project directory:
cd natural-selection-simulation
- Create a virtual environment (optional but recommended):
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
- Install the dependencies:
pip install pygame
To run the project, use the following command:
python main.py
The simulation starts with an initial population of organisms that move randomly across the screen. They search for food, and upon finding it, consume it to gain energy. Organisms can reproduce and generate offspring with slightly modified characteristics. The simulation tracks and displays the maximum and minimum values of various organism characteristics over time.
The simulation environment consists of three types of terrain:
-
Normal:
- Color: Light Gray
(169, 169, 169)
- Effect: Organisms lose energy at a normal rate when moving on this terrain.
- Color: Light Gray
-
Difficult:
- Color: Dark Gray
(105, 105, 105)
- Effect: Organisms lose energy at double the rate when moving on this terrain due to increased difficulty of movement.
- Color: Dark Gray
-
Trap:
- Color: Black
(50, 50, 50)
- Effect: This terrain is dangerous for organisms. In addition to losing energy, they also lose health proportionally to their escape ability and speed. The lower the organism's escape ability and speed, the greater the damage suffered.
- Color: Black
Organisms adapt their behavior based on the type of terrain they encounter:
- Energy and Health Loss: Moving through difficult and trap terrains increases the rate of energy loss and can decrease health, affecting the longevity and efficiency of organisms.
You can adjust the simulation parameters in the constants.py
file, such as mutation rate, food appearance intervals, and organism characteristics.
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is licensed under the MIT License. See the LICENSE
file for details.
For questions or suggestions, contact:
- Name: Itallo Nardi
- Email: [email protected]
Thank you for using this project!