DEV Community

loading...
Cover image for PyBoxCar - An Experience

PyBoxCar - An Experience

#ai
Kevlyn Kadamala
I like coding, playing football and listening to music in no particular order.
・3 min read

Hey there👋
This is my first time ever writing a blog post on the internet (yes, the internet), so if by chance you've ended up here, feel free to click away. But incase you're in some way curious to know what I'll be talking about, well then, I hope you enjoy.

Well, the reason I mentioned 'An Experience' in the title was because it is exactly what it says, an experience.
I aim to be a researcher, and I love learning new things. I see plenty of tutorial and walkthroughs on different projects, models, architectures etc, but I hardly see posts that deals with the experience of building (incase I have missed any, I'd love to read them, please do share them with me).
So I thought why not start with something small and with something I like, Genetic Algorithms.

So for those of you who are not aware, the BoxCar2D (here is nice demonstration of the problem and the original version) is a genetic algorithm problem where a car moves from a start point to its destination, as quickly as possible.
You maybe wondering where the algorithm is applied, its on the design of the car. Its very shape and characteristics are evolutionary. Meaning, they evolve over time.

I knew I had to start this project with the mindset of building a game. I had to use a Physics Engine and I was debating the language I wanted to write it in. I had to choose between Python, JavaScript or Godot (a game engine I was learning at the time). I eventually chose Python because I came to a conclusion that I would certainly enjoy the project more if I chose my favourite language.

So once I was sure with the programming language, I decided to go with Arcade and PyMunk for my animation and physics libraries. For some reason I prefer Arcade over PyGame. I never really enjoyed PyGame to be honest, Arcade on the other hand, was fun and really easy to pick up too. I began coding, noting down the different things to be done (I still have the ToDos in my repository). Then came my first (and only :P) major problem.

If you visited the demonstration link you noticed that the car starts from the left, and moves to the right. When the furthermost car reached near the edge of the screen, the screen automatically followed it. I really like this concept, it is very commonly used and you find it in many games, so I decided to give it a shot. But I couldn't get the damned thing to work. I tried for almost a week, and the best I could do was to get a couple of cars floating in the air while the floor they drove on moved towards the left. I almost gave up. Almost.

While I was busy almost giving up, I had a thought. Maybe I don't have to do the same. Maybe, just maybe, if I instead did something a little different, it would be okay right?

So what I did instead was this, instead of making my cars go from left to right, I'll make them go from top left to bottom right. Umm, what? You may ask, and I will repeat, yes from top left to bottom right. Yes, it is a but unorthodox, but this enables me to complete my challenge, something I really wanted to do at this point.

So once I started coding the terrain in this unorthodox fashion, I began wondering, will the genetic algorithm be able to solve this problem. I did some research, particularly on the constraints of genetic algorithms and came to a conclusion that, this problem is probably solvable.

Once I was set, everything came into place, I didn't take too long to complete the rest of the implementation, just a little extra time over the dynamic graphing of matplotlib to be honest, but it wasn't too time consuming.

Overall, I'm glad I didn't give up on the challenge, though it took a bit longer than expected, I am happy with the outcome.

If you're curious you can visit the repository over here.

I know this isn't anything groundbreaking, or life changing, but I'd still like to share my experience while building certain projects. Do let me know if there's anything else you'd like to know, or an experience you'd like to share. Hopefully I'll continue this for my other projects or even maybe Hackathons in the future.

If you read the whole thing and reached here, Thank You!

Discussion (0)

Forem Open with the Forem app