Categories
Swift Kick

Swift roundup: Game programming!

swift kick

Today’s Swift Kick features a roundup of tutorials and resources for the Swift developer who’s into game programming. If you’ve been meaning to learn Swift, games are a fun way to do so, and if you’ve been meaning to learn game programming, you’ll find these useful!

Brian Advent’s tutorial on building a networked tic-tac-toe game with Swift and the Multipeer Connectivity Framework

Brian Advent (whom I’ve written about before) has posted a video tutorial showing how to build a networked tic-tac-toe game using iOS’ Multipeer Connectivity Framework, which allows you to connect and share data with devices nearby.

RayWenderlich.com’s Sprite Kit Tutorial for Beginners updated for Swift

ray wenderlich ninja game

Two years ago, Ray Wenderlich posted the sort of game programming tutorial that he wanted to see: in his own words, “very simple but functional game with animation, collisions, and audio without using too many advanced features.” The resulting game is the one pictured above, featuring a shuriken-hurling ninja taking on approaching monsters.

The game was originally written in Objective-C and used the open source cross-platform 2D gaming framework Cocos2D 1.X, and was followed by an update for Cocos2D 2.X. When Sprite Kit came out, Ray wrote a new version of the game using that framework, and I took some ideas from that game and turned it into a simple shoot ’em up game in Swift.

Ray’s updated his Sprite Kit Tutorial for Beginners for Swift, and it’s a great way to brush up on Swift and game programming at the same time. Check it out!

Conway’s Game of Life in functional Swift

life

While it can be argued that Swift is not a functional programming language, it certainly lends itself better to functional programming techniques than many other languages, Objective-C included. With this in mind, take The Game of Life with Functional Swift, written by Colin Eberhardt, one of the co-authors of the RayWenderlich.com books iOS8 by Tutorials, Swift By Tutorials, and Core Data by Tutorials. He writes:

This blog post shows an implementation of Conway’s Game of Life using functional techniques in Swift. This results in code which is a clear and concise representation of the game’s logic. I also take a closer look at ranges, intervals, the pattern match operator, ~= and how local functions help organise your code.

Taking a functional programming approach allowed Colin to condense the rules of Conway’s Game of Life down to this:

// rules of life
let liveCells = cells.filter { $0.state == .Alive }
let deadCells = cells.filter { $0.state != .Alive }

let dyingCells = liveCells.filter { livingNeighboursForCell($0) !~= 2...3 }
let newLife =  deadCells.filter { livingNeighboursForCell($0) == 3 }

// updating the world state
newLife.each { (cell: Cell) in cell.state = .Alive }
dyingCells.each { (cell: Cell) in cell.state = .Dead }

If you’re looking for a challenge, you might want to take MakeGamesWith.Us’ implementation of the Game of Life (pictured below) and refactor it using Colin’s functional approach.

life in swift

SwiftCast: Game Programming in Swift

The latest Swiftcast, which you can listen to using the player above, is all about game programming in Swift:

Swift game development is very exciting. If it isn’t already obvious with the content of our site, we love to talk about game development. Mobile gaming is a rapidly growing market, and iOS is by far the most rewarding and exciting to build games for. If you’ve ever wanted to learn how to build an iOS game as an indie developer, we have some resources and advice for you.

iOS Games by Tutorials updated for Swift

RayWenderlich.com’s book, iOS Games by Tutorials, has been updated for iOS 8 and Swift. In 28 chapters and over 800 pages, you’ll learn game programing with Swift and Sprite Kit by building the following games:

zombie conga

Zombie Conga, where the player is a partying zombie who’s trying to build a conga line of cats while avoiding crazy cat ladies. It covers introductory topics such as sprites, processing touches, collision detection and scrolling.

XBlaster

XBlaster: a space shooter where you’ll work with physics and particle systems.

cat nap

Cat Nap, where you’re trying to help a sleepy kitty get to bed. This is a “physics-based” game, so this introduces the Sprite Kit’s physics engine starting with the basics, but getting into stuff advanced enough for you to write your own version of Angry Birds or Cut the Rope.

pest control

Pest Control, a tile-mapped game where you take a Schwarzeneggarian hero on a bug-killing spree. This covers building a tile-mapping engine and adding all sorts of effects to a game.

circuit racer

Circuit Racer, a racing game made more complicated by obstacles on the track. This one features mixing Sprite Kit-based UIs with UIKit-based UIs, using the accelerometer, and interfacing with Game Center.

Also covered:

  • Porting your iOS games to OS X
  • Using texture atlases for reduced memory overhead and better performance
  • Tips and tricks for getting the most performance out of your game code
  • Basic game art-making for programmers

iOS Games by Tutorials is available in PDF form for $54, and it’s a great deal at the price. Buying it also gets you free updates for the life of the book; if you bought the earlier Objective-C/iOS 7 edition, you already own the current Swift/iOS 8 edition!

…and don’t forget…

The Flappening, in which I fix the implementation of Flappy Bird that changes to iOS’ Swift APIs broke, and…

my simple “shoot ’em up” game in Swift, which has also been updated to work with post-beta Swift.