Pygame Genetic Algorithm with Level Editor!
March 18 2026 - 3 Minutes - Source
pygame - ai - python

A quick demonstration of the level editing and genetic algorithm in progess
Hello everyone! I just had a bit of inspiration a few days ago, so I decided to make a quick weekend project, an implementation of the genetic algorithm in pygame, but with a level editor!
A lot of this code is based off my sand/particle game tutorial and the very first blog post I ever made, about implementing the GA in pygame!
Some of this code is also based off of a little pygame pathfinding test I made years ago that never worked, but did have a level editor, that I was able to take some inspiration from.
THe main difference between this and the last one, is that the world the agents are in are dynamic!
How the world works
This is where I used some code from the sand game. There is a dictonary called GRID_MAP, It takes in the coordinates of a tile as a tuple for the key. It stores only a reference to the object at that tile, and only for tiles where something actually is.
This makes it easy to check if a spot is valid, just coordinate in GRID_MAP, nothing else required.
To make my life a little easier, I also just have a is_pos_valid() function, that just checks if a position is in GRID_MAP or if it is off of the map.
How this is different from my first one
Number one, the levels are more "dynamic" as it is possible to change spawn/goal position without restarting the program.
Number two, gates! They are another thing that can be placed, like spawners goals and walls, but what they do, is they add some amount of fitness to any agent that passes through it.
Number three, the big one, walls!
If you remember, I tried implementing walls in the first one, but let myself from then explain:
One would also need to add an incentive to go around the wall instead of just going near the wall, as I have tried to implement this into the simulation however the agents simply hit the wall and never went around it.
Minejerik, 2023
I am not sure exactly why, but when I added the walls this time, the agents were able to quickly learn to walk around them, even without needing the gates...
I added gates because I thought it would be needed for them to learn to walk around walls, but I guess not!
Number four, a less noticeable difference, I started using more tuples.
I used so many tuples, that I had to make a function just to add_tuples!
I have been using them as vectors, for the movement, so instead of up, down, left, right they have (0,1), (0,-1), (-1,0), (1,0).
It seems to be quicker, as this new simulation runs faster than the last one!
Challenges
I regret not creating some sort of vector class, to use instead of tuples, as tuples not being able to be multiplied or added or subtracted or etc, is pretty annoying.
But generally, no other challenges were faced!
Conclusion
This was a shorter blog post, for a shorter project, so I guess it fits!
I hope you enjoyed reading!
if you are interested in checking out the code for this projects, its available on my git server.