This post is a portion of Part 13 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 13.0 – Game States
- 13.1 – Implementing Game States
- 13.2 – Better Game State Implementation
- 13.3 – Finite State Machine Artificial Intelligence
You’re all going to like this one. We can extend the concept of states to also include the behaviors of in game objects. Doing this is called Finite State Machine (FSM) Artificial Intelligence. Granted, we aren’t getting very deep into the concepts of AI in this video, and we are definitely stunted since we aren’t using OOP, but it is a good primer nonetheless. I hope you enjoy it as much as I enjoyed making it.
Full source can be found here.

I loved this tutorial it was awesome i cant wait to try to impliment that into the game im doing and maybe even during the IDLE state create a idolThreshold so that the enemy could lets say walk in a random angled y and x for a small threshold
yay excited!
thanks again for this
k so my brain officially hurts (probably because ive gone through all the 2d and OOP devel lessons in 5 days .. but! lol.. i cant figure out how to make the enemy box walk in a random direction in a smaller threshold until the player enters the chasing threshold.. please help
Really awesome tutorial, thanks for this as I’ve learned alot. Now I’m just studying everything over.
@sparksplur
don’t rush the videos or you won’t really learn anything. Its not about getting everything done now but in time. Trust me, I rushed this stuff before and gave up on programming cause I couldn’t get things done. Btw you should try to implement code in a similar way but differently if you can.
back on topic, great examples and I’m glad to have made it through. Though the math throws me off just a bit since I havent been to college yet due to it being full. I sucked at math cause my teacher never helped the right way. But I guess I can improve, some guy at a starbucks learned calculus on his on
This. Great comment. Thank you
Great video, cant wait to code some heat seeking missiles with this
I originally voted ’3D game development’ for your next series, but after watching this I am definately voting for some more AI instead!
One question about the enemy following algorithm:
If you are above and to the right of the enemy at what I think is about a 45 degree angle, he seems to go faster than at any other angle. Do you know why this is?
the math just takes time, like the programming lol. the best tool you have is flow charts and pseudocode. just sayin’