Probably the simplest piece to get done for the project. All I had to do was create the plants using a for-loop and then I just fine tuned the reproduction rate and lifespan.
To create the herbivores, I took the code from the plants and gave them the ability to move across the canvas. I gave the herbivores fewer numbers so that they would not over run the plants and just consume everything. I also added a function so that when the herbivores got close to the plants they would “consume” them and grow.
Same as before, I used the herbivore code to create the carnivores. All I had to do was switch the the eating function to eat the herbivores on contact. The carnivores fewer number since they are faster than the herbivores and so they don’t overrun the herbivores. I also made it so that if the herbivores become bigger than the carnivores they cannot be eaten.
So for some reason my code was randomly crashing at random times. It took me awhile to figure it out but after going through Google Chrome’s console I figured out that it was because of the carnivores’ eating function. At first, I wasn’t sure why that was happening but the answer was right in front of my face the entire time. I originally had the function tracking the herbivores’ array when it should have been tracking the carnivores’. After that my code started to run smoothly again.
I’ll admit once everything was running smoothly, I started to get a bit bored with my little ecosystem. So I decided to create a metaphorical dice for each creature that would be thrown every 10 seconds. This dice would be symbolize disease, prosperity, or being stagnant. Depending on how the dice rolled the creatures could flourish or they could suffer for many “years” until they are able to make a comeback.