The first thing you do is to create a new game file. There is a game template in the framework so you can create it with the following steps.
- Install the Xcode Project Template for the game framework
- You can download it from here
- Create a new Xcode project with RubyCocoa Game template
- Choose "File" -> "New Project" at the menu bar
- Select "Cocoa-Ruby Game Application"
- Enter the name of a new project
- Add a new game file with the game file template
- Open the "Classes/ruby/games" group at the left pane
- Right-click the "games" group
- Select "Add" -> "New File" at the pop-up menu
- Select "RubyCocoa Game Template"
- Enter the name of a new file (say TankGame.rb)
- The base name of the filename is used to replace the class names
- Open the "Classes/ruby/games" group at the left pane
That's it. Now you have a new file "TankGame.rb" in the games group. By double-clicking the filename, you can see the code below:
require 'Game'
require 'State'
require 'Event'
require 'View'
require 'Utility'
class TankGameModel
def init()
# this method is called when Application is launched.
# It is also called when TankGamePlayingState
# becomes the current state
# do some initialization here
end
# Add some methods here
end
class TankGamePlayingState < State
# define your timer IDs here
MYGAME_TIMER = 1000
def enter()
super()
@model.init()
# Define context dependent event handlers
# Each handlers are defined in a form of [EventType, Content, Proc object]
# See KeyEvent.rb, MouseEvent.rb and TimerEvent.rb for event specific info
# Other game's source code would also help you
contexts = [[TimerEvent::Fired, MYGAME_TIMER, proc {|id| tick() }]]
contexts.each {|context| EventContext.addContext(*context) }
@timer = TimerEvent.new(0.1, true, MYGAME_TIMER)
end
def tick()
# do something when timer event occurs
return true
end
# add some event handlers or context handlers here
end
class TankGameView < View
def notifyStateDidChange()
# If you need some initialization process,
# Override notifyStateDidChange and write what you want here.
# If you don't need any initialization process for this view class,
# Just remove this method
ColorCollector.init(['black'])
end
#
# This method is called from MiniGamesView
#
def drawRect()
NSColor.blackColor.set
NSRectFill(@bounds)
# Use ShadowMaker module to easily draw shadow for objects
setShadow(NSColor.lightGrayColor, [6.0, -6.0], 3.5, 0.6)
# Use TextMaker module to easily draw Text
drawCenterizedText("Draw Something", 70, NSColor.whiteColor)
end
end
class TankGame < Game
def initialize()
@bindings = [[TankGamePlayingState, TankGameModel, TankGameView]]
@initialState = TankGamePlayingState
end
def self.title() "TankGame" end
def self.titleImage() nil end # return the name of an image here
end
Thanks to the Xcode template, you don't have to copy the template nor replace the class names. That's helpful isn't it?
Run It Anyway
The game template actually works as it is so why don't you run it? Press the "Build & Go" button (or Command-R key) at the project window. Then you see the main title of MiniKidsGames.
Select TankGame and hit the space key (at this point, you didn't make any title image for this game so the default title image is used.) You see a message "Draw Something." This game does nothing but draw this message and quit. Press "Ctrl-Q" to go back to the main menu.











