I am trying to implement a fairly basic boids simulation in python. My goal is to have a simulation with a basic predator prey setup. I found some pseudocode (can't post more than two links but it is the first result if you google boids pseudocode
) and some code here and decided to give it a go. Because I wanted to add predators, I decided to give take the code I found modify it so that the boids (that will become prey) are sprites, and then go from there. However, I have run into this problem.
After I modified the code to use pygame sprites, all of the boids move to the lower right hand corner (the original code worked correctly).
My code (just clone the repo) is here(github). Has anyone ever run into the first issue? Does anyone have any ideas to solve it? As for question 2, could someone please explain how to do that?
Thank you and any help would be greatly appreciated.
P.S.
The behavior of the boids (their movement) appears to be working fine apart from the fact that they always go to the lower right hand corner.
P.P.S.
Thanks to furas the prey behave correctly now.
P.P.P.S.
As the debugging problem has been solved, the part of my question that remains involves an explanation, and I think should be on topic.
You have to differences in your code
You use different velocity speed at start - in
__init__
but this shouldn't do difference.You updates object in different moment.
You move all preys (using Group update) at the same time - after all calculations.
Original code moves every boid after its calculations so next boid use different data to calculate its move.
I put
prey.update()
insidefor
loops and removeall_sprites_list.update()
I organize code in a little different:
EDIT: real problem was dividing of two integer numbers in Python 2 which gives result rounded to integer number
Solution:
Use this before other imports.