Simple AI Example

Oct 27, 2012

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].

Try playing it in browser.

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.

Here are the fully commented Processing source files (zip download).

I’ve also included in the zip a simple index.html file along with a recent version of ProcessingJS, which combined make this demo playable in-browser as JavaScript (same link as above).

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!



5 Comments

  1. Jonny D says:

    No go with Chrome here (22.0.1229.94 on Linux Mint). I can see the instructions and an otherwise black field.

  2. […] you’re starting with (or switching to!) Processing for prototyping/demonstrations, ActionScript 3 to make a web game, C#/Unity for a 3D game, or C++/SDL for a high performance […]

  3. […] you’re starting with (or switching to!) Processing for prototyping/demonstrations, ActionScript 3 to make a web game, C#/Unity for a 3D game, or C++/SDL for a high performance […]

Leave a comment

Comments Form
  • Your email will not be used for any purpose other than to update you in replies to your comments. Your website address will be shown as a link from your name with your comment. Your profile photo is auto-magically provided by Gravatar.

All contents Copyright ©2017 Chris DeLeon.

Site production by Ryan Burrell.