in Swift

Building a simple multi-player Swift game in Playground

Ever since I’ve seen the Playground demo at WWDC I felt like I wanted to do a real game using Playground. If you are already familiar with using Playground (if not, then check out this post: Swift and Playgrounds: Learn to program in a fun way!), then you’ll probably know, that Playgrounds aren’t actually built to deal with (real-time) user input.

So how would I get people to control a game? Turns out, it’s actually not too complicated. So the game I made is fairly simple. It is a multi-player game called “horseabout” which can be played by two teams (team fire and team ice). Each of those teams controls one horse. The winning team is the one with the horse who first crosses the finish line (grey line).

Swift Playground Game

Swift Playground Game : horseabout

 

But how can those teams control the game? Well, do you remember those days when every website had a hit counter? I’ve heard of people (obviously not me), who bragged about how successful their websites were according to their hit counters. But usually those sites featuring black backgrounds, animated GIFs and multi-color Comic Sans texts weren’t so successful after all. Their webmasters  just found out that refreshing the page increases the hit counter by one.

Since this principle worked out so well back then, why not reuse it nowadays to control a game. So each team gets one individual hit counter (I’ve been using free text-based counters from: freehostedscripts.net/texthitcounter/ *). The challenge is to increase the counters as fast as possible by refreshing the page. Obviously this is not an approach you would take for a game you’d release, but you’ll likely never ever release a Playground game either, so I guess it serves the purpose.

Implementation

First let’s do our imports and setup our constants. Either replace the XXXXXXX part of the team URLs with your own site ids or insert your own URLs. If you are using your own counters you will probably also have to change the regular expression in gameDataSrcRegEx to match your input.

 

Now we can create our game scene. You can download the images here: opengameart.org/content/2d-platformer-art-assets-from-horse-of-spring

 

Now we need to create a class responsible for retrieving and storing the actual game data.

 

So far we’ve got the scene and are able to retrieve data for it. Finally we need a mechanism which allows us to retrieve data while our scene is displayed in our Playground’s XCPShowView. This is where it gets a bit tricky. Actually it would have been sufficient to use NSTimer like:

 

But this will not work in Playground. So the solution I came up with is nothing you would ever want to use in anything but an experimental project like this one. In the run() function we continuously retrieve data and also trigger the display of our retrieved data.

 

Finally we can run the game. You should change the timeout of your Playground to make your game run longer.

 

Bottom line

In this highly experimental project I wanted to show one way of implementing a functional multi-player game in Playground. It has obvious flaws (the game mechanics for example is far from being commercially usable) but hopefully you still enjoyed this brief article. Let me know if you need any help implementing it yourself or if you’ve got any feedback.

Download this Playground:

 

* You can use your own counter, but you’ll probably have to adapt the regular expression. If you’re using a free service, then don’t send out unreasonable amounts of requests (we don’t want a DDoS!)