Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RubyConf 2009 - Writing 2D games for the OSX platform in Ruby

RubyConf 2009 - Writing 2D games for the OSX platform in Ruby

Are you a developer who would love to get into video games but get scared when he hears "OpenGL" or "rendering engines"? Or Maybe, you never considered writing a video game because you have heard Ruby was slow and you are not ready to give up on your favorite programming language. This talk is for you then. Together, we will build a very simple game using Ruby and the development tools offered by Apple. At the end of the presentation you will be ready to get started on your own project.

Video: http://www.confreaks.com/videos/192-rubyconf2009-writing-2d-games-for-the-osx-platform-in-ruby
Website: http://matt.aimonetti.net/posts/2009/11/21/rubyconf-2009-2d-games-for-os-x/

Matt Aimonetti

November 22, 2009
Tweet

More Decks by Matt Aimonetti

Other Decks in Programming

Transcript

  1. rubyconf 2009
    2d video game
    development
    with
    MacRuby
    matt aimonetti
    Sunday, November 22, 2009

    View full-size slide

  2. test your memory
    Sunday, November 22, 2009

    View full-size slide

  3. Sunday, November 22, 2009

    View full-size slide

  4. Sunday, November 22, 2009

    View full-size slide

  5. Sunday, November 22, 2009

    View full-size slide

  6. Sunday, November 22, 2009

    View full-size slide

  7. Sunday, November 22, 2009

    View full-size slide

  8. Sunday, November 22, 2009

    View full-size slide

  9. Sunday, November 22, 2009

    View full-size slide

  10. Sunday, November 22, 2009

    View full-size slide

  11. Sunday, November 22, 2009

    View full-size slide

  12. Sunday, November 22, 2009

    View full-size slide

  13. Sunday, November 22, 2009

    View full-size slide

  14. Sunday, November 22, 2009

    View full-size slide

  15. Sunday, November 22, 2009

    View full-size slide

  16. Sunday, November 22, 2009

    View full-size slide

  17. Sunday, November 22, 2009

    View full-size slide

  18. Sunday, November 22, 2009

    View full-size slide

  19. Sunday, November 22, 2009

    View full-size slide

  20. Sunday, November 22, 2009

    View full-size slide

  21. Sunday, November 22, 2009

    View full-size slide

  22. Sunday, November 22, 2009

    View full-size slide

  23. Sunday, November 22, 2009

    View full-size slide

  24. Sunday, November 22, 2009

    View full-size slide

  25. Sunday, November 22, 2009

    View full-size slide

  26. Video Games
    are fun
    Sunday, November 22, 2009

    View full-size slide

  27. Ruby
    Programming
    is fun
    Sunday, November 22, 2009

    View full-size slide

  28. You have a mac
    Sunday, November 22, 2009

    View full-size slide

  29. (if not, get one!)
    Sunday, November 22, 2009

    View full-size slide

  30. video games on
    OSX
    OLD SCHOOL
    from scratch
    Sunday, November 22, 2009

    View full-size slide

  31. truth
    Sunday, November 22, 2009

    View full-size slide

  32. Sunday, November 22, 2009

    View full-size slide

  33. Sunday, November 22, 2009

    View full-size slide

  34. Sunday, November 22, 2009

    View full-size slide

  35. but
    Sunday, November 22, 2009

    View full-size slide

  36. Sunday, November 22, 2009

    View full-size slide

  37. Sunday, November 22, 2009

    View full-size slide

  38. popular
    games
    Sunday, November 22, 2009

    View full-size slide

  39. Massively
    Multiplayer online
    role-playing games
    (MMORPG)
    Sunday, November 22, 2009

    View full-size slide

  40. Sunday, November 22, 2009

    View full-size slide

  41. Sunday, November 22, 2009

    View full-size slide

  42. Sunday, November 22, 2009

    View full-size slide

  43. too much work for a
    hacking project
    Sunday, November 22, 2009

    View full-size slide

  44. first person
    shooter
    Sunday, November 22, 2009

    View full-size slide

  45. Sunday, November 22, 2009

    View full-size slide

  46. Sunday, November 22, 2009

    View full-size slide

  47. Sunday, November 22, 2009

    View full-size slide

  48. new types of
    games
    Sunday, November 22, 2009

    View full-size slide

  49. Sunday, November 22, 2009

    View full-size slide

  50. Sunday, November 22, 2009

    View full-size slide

  51. online
    games
    Sunday, November 22, 2009

    View full-size slide

  52. Sunday, November 22, 2009

    View full-size slide

  53. Sunday, November 22, 2009

    View full-size slide

  54. Sunday, November 22, 2009

    View full-size slide

  55. iphone games
    Sunday, November 22, 2009

    View full-size slide

  56. Sunday, November 22, 2009

    View full-size slide

  57. Sunday, November 22, 2009

    View full-size slide

  58. demo
    Sunday, November 22, 2009

    View full-size slide

  59. MacRuby
    Sunday, November 22, 2009

    View full-size slide

  60. Ruby for SCottish
    Sunday, November 22, 2009

    View full-size slide

  61. Laurent Sansonetti
    Sunday, November 22, 2009

    View full-size slide

  62. Sunday, November 22, 2009

    View full-size slide

  63. Sunday, November 22, 2009

    View full-size slide

  64. on
    obj-c runtime
    and GC
    Sunday, November 22, 2009

    View full-size slide

  65. COCOA
    Apple's Objective-C based programming environment for
    Mac OS X
    Sunday, November 22, 2009

    View full-size slide

  66. VIDEO
    GAME
    Sunday, November 22, 2009

    View full-size slide

  67. Sunday, November 22, 2009

    View full-size slide

  68. Sunday, November 22, 2009

    View full-size slide

  69. keyboard
    Sunday, November 22, 2009

    View full-size slide

  70. key event
    keyboard
    Sunday, November 22, 2009

    View full-size slide

  71. key event
    keyboard
    GameData
    Sunday, November 22, 2009

    View full-size slide

  72. Sunday, November 22, 2009

    View full-size slide

  73. game loop
    Sunday, November 22, 2009

    View full-size slide

  74. game loop
    174
    ❶ update
    layers
    Sunday, November 22, 2009

    View full-size slide

  75. game loop
    174
    ❶ update
    layers
    update
    game items
    Sunday, November 22, 2009

    View full-size slide

  76. game loop
    174
    ❶ update
    layers
    reposition
    update
    game items
    Sunday, November 22, 2009

    View full-size slide

  77. game loop
    174
    ❶ update
    layers
    reposition
    update
    game items
    ❷ collisions
    Sunday, November 22, 2009

    View full-size slide

  78. game loop
    174
    ❶ update
    layers
    reposition
    update
    game items
    ❷ collisions
    lives
    points
    sound
    Sunday, November 22, 2009

    View full-size slide

  79. LOOP play
    DATA
    GAME
    Sunday, November 22, 2009

    View full-size slide

  80. GAME
    PLAY
    Sunday, November 22, 2009

    View full-size slide





  81. Sunday, November 22, 2009

    View full-size slide

  82. NSView subclass
    Interface Builder
    Sunday, November 22, 2009

    View full-size slide

  83. NSView subclass
    Interface Builder
    Sunday, November 22, 2009

    View full-size slide

  84. class KeyboardControlView < NSView
    def keyDown(event)
    end
    end
    Sunday, November 22, 2009

    View full-size slide

  85. class KeyboardControlView < NSView
    def keyDown(event)
    end
    end


    Sunday, November 22, 2009

    View full-size slide

  86. acceleration
    Sunday, November 22, 2009

    View full-size slide

  87. relative
    positioning
    Sunday, November 22, 2009

    View full-size slide

  88. Sunday, November 22, 2009

    View full-size slide

  89. GAME
    LOOP
    Sunday, November 22, 2009

    View full-size slide

  90. usual
    workflow
    Sunday, November 22, 2009

    View full-size slide

  91. usual
    workflow
    Sunday, November 22, 2009

    View full-size slide

  92. video game
    Sunday, November 22, 2009

    View full-size slide

  93. video game
    Sunday, November 22, 2009

    View full-size slide

  94. 30 x
    per
    second
    Sunday, November 22, 2009

    View full-size slide

  95. moves player
    moves objects
    resolves collisions
    updates score/level
    redraws graphics
    plays sounds
    Sunday, November 22, 2009

    View full-size slide

  96. module GameLoop
    def start_refreshing
    @timer = NSTimer.scheduledTimerWithTimeInterval 0.03,
    target: self,
    selector: 'refresh_screen:',
    userInfo: nil,
    repeats: true
    end
    def refresh_screen(timer=nil)
    #…
    end
    end
    Sunday, November 22, 2009

    View full-size slide

  97. NSTimer.scheduledTimerWithTimeInterval 0.03,
    target: self,
    selector: 'refresh_screen:',
    userInfo: nil,
    repeats: true
    cocoa class method time interval
    method to call
    on the target
    some stuff
    we don’t care
    about ;)
    Sunday, November 22, 2009

    View full-size slide

  98. IntervalTimer.new(0.03, :target => self,
    :selector => 'refresh_screen:')
    Sunday, November 22, 2009

    View full-size slide

  99. def refresh_screen(timer=nil)
    GameData.all_layers.each{ |layer| layer.update }
    collided_bombs, collided_rubies=GameData.collisions
    if !collided_bombs.empty?
    loose_a_life
    collided_bombs.each{|layer| layer.item.reset! }
    else
    collided_rubies.each do |layer|
    GameData.increase_points(layer.item.points)
    points.attributedStringValue =
    GameData.points.to_s
    layer.item.reset!
    end
    SoundEffects.collision(0.2) unless
    collided_rubies.empty?
    level_change! if change_level?
    end
    end
    Sunday, November 22, 2009

    View full-size slide

  100. GameData.all_layers.each do |layer|
    layer.update
    end
    Sunday, November 22, 2009

    View full-size slide

  101. collided_bombs, collided_rubies =
    GameData.collisions
    Sunday, November 22, 2009

    View full-size slide

  102. def collide_with?(other_rect)
    NSIntersectsRect(rect_version, other_rect)
    end
    Sunday, November 22, 2009

    View full-size slide

  103. if !collided_bombs.empty?
    loose_a_life
    collided_bombs.each do |layer|
    layer.item.reset!
    end
    Sunday, November 22, 2009

    View full-size slide

  104. else
    collided_rubies.each do |layer|
    GameData.increase_points(layer.item.points)
    update_points_display
    layer.item.reset!
    end
    Sunday, November 22, 2009

    View full-size slide

  105. unless collided_rubies.empty?
    SoundEffects.frog(0.2)
    end
    level_change! if change_level?
    end
    Sunday, November 22, 2009

    View full-size slide

  106. module SoundEffects
    module_function
    @frog = NSSound.soundNamed("Frog")
    def frog(delay=0)
    @frog.performSelector(:play,
    withObject: nil,
    afterDelay: delay)
    end
    end
    Sunday, November 22, 2009

    View full-size slide

  107. GAME
    DATA
    Sunday, November 22, 2009

    View full-size slide

  108. Sunday, November 22, 2009

    View full-size slide

  109. module GameData
    module_function
    end
    Sunday, November 22, 2009

    View full-size slide

  110. class GameController
    def awakeFromNib
    GameData.register_controller(self)
    end
    end
    Sunday, November 22, 2009

    View full-size slide

  111. GameData.all_layers
    Sunday, November 22, 2009

    View full-size slide

  112. class GameController
    def display_item(item)
    new_layer =
    ImageLayer.alloc.initWithItem(item)
    GameData.all_layers << new_layer
    # [...]
    end
    end
    Sunday, November 22, 2009

    View full-size slide

  113. module GameConfig
    module_function
    def data
    @data ||= { :levels => [
    { :name => 'Pond',
    :rubies => 3,
    :bombs => 12,
    :score_limit => 30,
    :vehicle => 'nenuphar',
    :bomb_image => 'bomb',
    :bomb_ratio => 1,
    :ruby_ratio => 1.5,
    :player_width => 0.2,
    :player_height => 0.2
    }]
    }
    end
    Sunday, November 22, 2009

    View full-size slide

  114. cocoa
    hax
    Sunday, November 22, 2009

    View full-size slide

  115. CocoaClass =~ RubyClass
    Sunday, November 22, 2009

    View full-size slide

  116. don’t
    like an
    API?
    Sunday, November 22, 2009

    View full-size slide

  117. Wrap it
    Sunday, November 22, 2009

    View full-size slide

  118. class NSButton
    def title_color=(color)
    current_font =
    self.attributedTitle.attribute(NSFontAttributeName,
    atIndex: 0,
    effectiveRange: nil)
    opts = { NSForegroundColorAttributeName => color,
    NSFontAttributeName => current_font }
    self.attributedTitle =
    NSAttributedString.alloc.initWithString( self.title,
    attributes: opts)
    end
    end
    Sunday, November 22, 2009

    View full-size slide

  119. compilation
    Sunday, November 22, 2009

    View full-size slide

  120. Sunday, November 22, 2009

    View full-size slide

  121. PATH="$PATH:/usr/local/bin"
    macruby_deploy "$TARGET_BUILD_DIR/
    $PROJECT_NAME.app" --embed --no-stdlib
    --compile
    Sunday, November 22, 2009

    View full-size slide

  122. wanna
    hack?
    Sunday, November 22, 2009

    View full-size slide

  123. best score
    post score
    Sunday, November 22, 2009

    View full-size slide

  124. gosu
    framework
    Sunday, November 22, 2009

    View full-size slide

  125. chipmunk
    physics
    Sunday, November 22, 2009

    View full-size slide

  126. http://github.com/
    mattetti/phileas_frog
    Sunday, November 22, 2009

    View full-size slide

  127. Sunday, November 22, 2009

    View full-size slide

  128. Thanks
    Sunday, November 22, 2009

    View full-size slide