iPhone Life magazine

Game Center Source Code: multiplayer Pong client

As some of you may know, I, among other things, also lecture on iOS programming. Due to the request of my students, I've spent some days on discovering the secrets of Game Center, which has been added to iOS in version 4.1 and is, consequently, pretty new and, apart from Apple's own documentation, very scarcely documented, let alone stand-alone, full demo application sources, of which I only know of GCPing, which is, for some reason, pretty much buried in the Apple developer forums. (I don't provide a link to GCPing because I don't know whether the developer wants to keep it pretty much private. Now that there's no NDA over Game Center any more, he may want to publish it to the public.)

 

First, the sources. They're here:

http://www.winmobiletech.com/sekalaiset/Pong.zip

 

The contents of the ZIP file are ordered in three directories in the root: Local, GameKit and GC. All versions support, from ground up, all screen confgurations and are Universal. (The latter is needed if you want to play multiplayer games over Game Center between an iPhone and an iPad.) Apart from the four-player GC version, all remote capable games support coordinate transformation and rescaling between the iPhone (iPod Touch; of course, also ones with the Retina screen) and the iPad. The only exception is the 4-player GC versions, where I didn't want to bother with implementing this to keep the code (relatively) small and clean.

 

The more advanced GC-capable versions also support voice transfer. There are two of them: "v3-4playersPlusAudio" only has a broadcast mode (everyone-to-everyone), while the most advanced one, "v4-UtilityApp", also supports private channels on top of the public (broadcast) one. You can set the target of your private channel in the flip view (by tapping the info icon) and can, after this, quickly switch between the two channels by using the newly introdued UISwitch in the gaming screen. If you do establish a private channel, make sure you tell the passive target of the channel to also select the other party so that his/her voice also gets thorugh the channel.

 

The local version allows for 1, 2 and 4 players. I've "only" written these versions to demonstrate how easy it is to write a local multiplayer Pong under iOS. The one-player version has a slightly more advanced version, "1player-with shift", which allows for handling offset in controlling the paddle. As it makes the paddle coordinate computation considerably less easy to read (particularly with two or four remote players, particularly when the iPad and iPhone screen size difference must also be taken into account), I haven't implemented this in other versions. Also note that the skew computation is very simple: based on the distance between the center point of the pad and the ball, I simply add a computed movement x (with vertical paddles, y) offset to the original one. A somewhat more dedicate algorithm would have been needed to avoid the ball gradually speeding up.

 

The GameKit version only supports two players and nothing fancy, except for rescaling when an iPad and an iPhone plays each other. The latter is also supported in the two-player GC version, but, to keep the code readable, not in the 4-player one.

 

Note that I use GC auto-mathcing as I will only later teach GC achievements/invites/leaderboards etc.

 

Have fun with the sources and let me know if you'd like to see a full step-by-step tutorial much easier to digest than Apple's own documentation on all these questions. When I have some free time (highly unlikely before 22/Nov), I will definitely publish one.

Want to master your iPhone and iPad? Sign up here to get our tip of the day delivered right to your inbox.
Topics:
Email icon
Want more? Get our weekly newsletter:

Werner Ruotsalainen is an iOS and Java programming lecturer who is well-versed in programming, hacking, operating systems, and programming languages. Werner tries to generate unique articles on subjects not widely discussed. Some of his articles are highly technical and are intended for other programmers and coders.

Werner also is interested in photography and videography. He is a frequent contributor to not only mobile and computing publications, but also photo and video forums. He loves swimming, skiing, going to the gym, and using his iPads. English is one of several languages he speaks.