I recently put together a simple AI example in Processing. If you haven’t heard of Processing, and would like a quick intro to it, I’ve also made a video tutorial on getting started with Processing [update: this video series I made later is a much more thorough programming introduction, also using Processing].
This AI illustration isn’t anything fancy or complicated. Arguably it’s about the simplest demonstration possible of what looks like “AI” rather than some purely mechanical behavior like bouncing off walls or simply seeking toward a target. Nor do these source files reflect the sort of best practices that should be adhered to for a huge project. It’s just a simple demonstration of how to make this sort of behavior work, and I figure other than tinkering with this demo, you’ll likely be reimplementing it in another language (AS3, C++, etc.) anyhow if you mean to do something ‘real’ with it.
There was a particular process to writing it in a layered fashion, which is important to know if you’re new to coding. However since there’s so little code, describing it here will actually be easier to follow than if I shared zipped versions of the code from each step. From a high level, my approach was:
1. Set up the screen size so there’s space to move
2. Create a way to place and display stationary Agents as dots
3. Give those Agents a way to move randomly/constantly
4. Give the Agents a way to switch after a delay between either standing still or moving randomly
5. Add more types of behaviors for the Agents to switch between
6. Add a way to add and display food pellets
7. Add new code for the Agents to remove food they’re close enough to, and a new behavior that seeks a nearby food pellet
8. Add a hunger timer that forces the Agents to seek food when they’re running low, and remove any Agents that don’t reach food in time
9. After things worked, I went back and commented everything to help thoroughly document the code for HobbyGameDev readers (i.e. there were only a few comments while actually writing the code, for the most part I aimed to make the code self-documenting based on variable and function names)
Note that Processing.js seems to not run locally in Chrome, I assume for security reasons, however it should run fine when the html and pde files are hosted online as they are here.
Learn and practice team game development with Gamkedo Club.
Membership worldwide. Professional support. Proven process.
Subscribe by e-mail to receive weekly updates with Gamkedo.Community interviews and YouTube training videos for game developers!