How to write Akka Actors: An example video game

Way back in 2013 — before my first fake heart attack followed by learning that I had thyroid cancer — I thought I was about to go “back to work”, and I decided to try to write another visual demo of Akka Actors before I went back to work. I gave myself 10 hours to write something, and at first I decided to just create some bubbles that would move about randomly on screen. But I got that working so fast that I decided to do something else.

Eventually I came up with the idea of a little “kill the bubbles” game, which turned into a “kill the characters” game. This video shows how it works:

Discussion

The audio problems with the video have something to do with the audio/video recording software I use. The game seems to work okay when that recording software isn't running.

The game is very much an alpha release, and has some bugs. If you increase the speed too much, it seems to have redrawing problems, and I haven't taken the time to figure out why the characters jump around at the end of the game. (I changed a few things in the 10th hour that led to these bugs.)

That being said, I hope that the source code for the game shows the following:

  • How to write an Akka Actor
  • How to start an Actor
  • How to communicate between actors
  • How to organize actors (though the code needs some refactoring)
  • How to kill an Actor
  • A few Swing GUI things, such as how to draw and animate the circles

The SBT build process also shows how to create a single JAR file from your own source code and a collection of dependencies.

The source code

The source code is available at GitHub:

Feel free to clone that code and do what you will with it. I want to keep the code simple for new Scala/Akka developers, but if you think you've fixed something, please let me know.

The project is a normal Scala/Akka/SBT project, but I put a few notes out there about how to compile, package, and run it. See the README-Developers.md project file for more information.

A binary release?

I thought about releasing a JAR file version of the game, but at the moment I don't think that serves any real purpose. I created this code to demonstrate Akka actors and Scala, and the best way to learn those technologies is to clone the source code, read it, and experiment with it. On my two Macs it works as shown in the video, so if you want to see it run, just clone the project and then run it with sbt run.

If anyone thinks there's a good reason to release a JAR file, leave a note in the Comments section below, and I'll be glad to think about it.

The messed up screen at the end

I'm not a Swing expert by any stretch of the imagination, but what I think happens at the end of the game is that when I draw the "Game Over" or "You Win" message on the GlassPane, this causes a re-draw of the JFrame's ContentPane. Because all of the bubble/circle actors are dead, well, I don't know exactly what happens ... they're dead, so they can't help to be drawn again ... I think the last thing that was drawn is drawn again, but typically with x=0.

That's just a theory, but I know that if I don't draw the final message on the GlassPane, all of the circles stay in their last known (proper) positions. When I have a little more free time I'll see what I can do about that.

(Update: A quick test confirms that this is the problem.)

Future plans

I've thought about wrapping a little more of a GUI interface around this and releasing it as a complete app (for Mac systems), but at the moment my 10 hours are up, and I need to move on to some other things. I'll try to give this a little more time in the future when I get some more free time.

I really enjoy programming with actors, so this was a lot of fun. (I tip my proverbial hat and say "Thanks" to the folks who created Scala and Akka.)

I hope it has been helpful. All the best, Al.