BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Developing iOS Games on Ruby

Developing iOS Games on Ruby

This item in japanese

Bookmarks

Brian Sam-Bodden, founder of Integrallis, gave a demonstration at the Barcelona Ruby Conference on how to leverage RubyMotion and open source 2D graphical libraries to quickly create 2D games for iOS in plain Ruby without any knowledge of Object-C.

Ruby Motion is the Ruby implementation for iOS and OS X, on top of the Objective-C runtime and the foundation framework. The graphical libraries used were Cocos2D game engine and Box2D for applying laws of physic to objects in the game. Joybox is a gem that wraps both libraries and exposes them via a clean Ruby API. Thus avoiding installation hassles and allowing development in pure Ruby environment.

Brian gave a first example of a Tetris clone for iOS in Ruby using only Apple’s CoreAnimation library from Ruby:

class TetrisController < UIViewController

This controller can then use the animations of 2D views provided by CoreAnimation, for example to flip the game view:

def flip(view)
  UIView.transitionWithView(view,
                            duration: 0.5,
                            options: UIViewAnimationOptionTransitionFlipFromBottom,
                            animations: proc {
                              # any other code that we want to run!
                            },
                            completion:nil)
end

The second, more complex, example consisted of a Super Mario Brothers-like game using the Joybox gem:

@director = Joybox::Configuration.setup do
      director display_stats: true
end

The above code returns a director instance with a default set of configurations. This object essentially controls the game workflow. In this example CoreAnimation views are no longer manipulated directly to setup the game action. That wouId require a lot more co-ordination and graphical animation code than the Tetris clone example did. Instead this example uses Joybox’s Layer class which handles the drawing canvas and all the user interaction. A game layer can then have multiple children focusing on different facets of the game:

@blue_sky = LayerColor.new color: "#6365fc".to_color
self << @blue_sky

In fact Director, Layers and Sprites (2D images that can be animated, they are the dynamic elements in the game) are all concepts inherited from the Cocos2D library, but wrapped in Joybox’s API, thus allowing development of the game purely in Ruby despite the lower libraries Objective-C implementation.

Finally by making use of Box2Ds physics engine, also wrapped by Joybox’s Physics Sprite class, Brian exemplified how to easily apply physics laws to the game world being created:

@world = World.new(gravity: [0, -9.8])

Creating a body inside the game world also becomes a matter of specifying its characteristics:

@player_body = @world.new_body(
      position: [16*1, 16*9],
      type: Body::Dynamic,
      fixed_rotation: true
    )

Other functionality such as audio effects is also available via the Joybox gem.

Brian also highlighted the fact that developing for iOS using RubyMotion allows the use of common Ruby testing frameworks such as RSpec that don’t require UI manipulation.

The presentation seemed to spark interest in the audience as several attendees praised the speaker on Twitter.

 

Rate this Article

Adoption
Style

BT