2D Video Games with MacRuby

3,709 views

Published on

Demonstration of how to write a simple 2D game using MacRuby and CoreAnimation.

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,709
On SlideShare
0
From Embeds
0
Number of Embeds
56
Actions
Shares
0
Downloads
40
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

2D Video Games with MacRuby

  1. 1. rubyconf 2009 2d video game development with MacRuby matt aimonetti Sunday, November 22, 2009
  2. 2. test your memory Sunday, November 22, 2009
  3. 3. Sunday, November 22, 2009
  4. 4. Sunday, November 22, 2009
  5. 5. Sunday, November 22, 2009
  6. 6. Sunday, November 22, 2009
  7. 7. Sunday, November 22, 2009
  8. 8. Sunday, November 22, 2009
  9. 9. Sunday, November 22, 2009
  10. 10. Sunday, November 22, 2009
  11. 11. Sunday, November 22, 2009
  12. 12. Sunday, November 22, 2009
  13. 13. Sunday, November 22, 2009
  14. 14. Sunday, November 22, 2009
  15. 15. Sunday, November 22, 2009
  16. 16. Sunday, November 22, 2009
  17. 17. Sunday, November 22, 2009
  18. 18. Sunday, November 22, 2009
  19. 19. Sunday, November 22, 2009
  20. 20. Sunday, November 22, 2009
  21. 21. Sunday, November 22, 2009
  22. 22. Sunday, November 22, 2009
  23. 23. Sunday, November 22, 2009
  24. 24. Sunday, November 22, 2009
  25. 25. Sunday, November 22, 2009
  26. 26. Video Games are fun Sunday, November 22, 2009
  27. 27. Ruby Programming is fun Sunday, November 22, 2009
  28. 28. You have a mac Sunday, November 22, 2009
  29. 29. (if not, get one!) Sunday, November 22, 2009
  30. 30. video games on OSX OLD SCHOOL from scratch Sunday, November 22, 2009
  31. 31. truth Sunday, November 22, 2009
  32. 32. Sunday, November 22, 2009
  33. 33. Sunday, November 22, 2009
  34. 34. Sunday, November 22, 2009
  35. 35. but Sunday, November 22, 2009
  36. 36. Sunday, November 22, 2009
  37. 37. Sunday, November 22, 2009
  38. 38. popular games Sunday, November 22, 2009
  39. 39. Massively Multiplayer online role-playing games (MMORPG) Sunday, November 22, 2009
  40. 40. Sunday, November 22, 2009
  41. 41. Sunday, November 22, 2009
  42. 42. Sunday, November 22, 2009
  43. 43. too much work for a hacking project Sunday, November 22, 2009
  44. 44. first person shooter Sunday, November 22, 2009
  45. 45. Sunday, November 22, 2009
  46. 46. Sunday, November 22, 2009
  47. 47. Sunday, November 22, 2009
  48. 48. new types of games Sunday, November 22, 2009
  49. 49. Sunday, November 22, 2009
  50. 50. Sunday, November 22, 2009
  51. 51. online games Sunday, November 22, 2009
  52. 52. Sunday, November 22, 2009
  53. 53. Sunday, November 22, 2009
  54. 54. Sunday, November 22, 2009
  55. 55. iphone games Sunday, November 22, 2009
  56. 56. Sunday, November 22, 2009
  57. 57. Sunday, November 22, 2009
  58. 58. demo Sunday, November 22, 2009
  59. 59. MacRuby Sunday, November 22, 2009
  60. 60. Ruby for SCottish Sunday, November 22, 2009
  61. 61. Laurent Sansonetti Sunday, November 22, 2009
  62. 62. Sunday, November 22, 2009
  63. 63. Sunday, November 22, 2009
  64. 64. on obj-c runtime and GC Sunday, November 22, 2009
  65. 65. COCOA Apple's Objective-C based programming environment for Mac OS X Sunday, November 22, 2009
  66. 66. VIDEO GAME Sunday, November 22, 2009
  67. 67. Sunday, November 22, 2009
  68. 68. Sunday, November 22, 2009
  69. 69. keyboard Sunday, November 22, 2009
  70. 70. key event keyboard Sunday, November 22, 2009
  71. 71. key event keyboard GameData Sunday, November 22, 2009
  72. 72. Sunday, November 22, 2009
  73. 73. game loop Sunday, November 22, 2009
  74. 74. game loop 174 ❶ update layers Sunday, November 22, 2009
  75. 75. game loop 174 ❶ update layers update game items Sunday, November 22, 2009
  76. 76. game loop 174 ❶ update layers repositionupdate game items Sunday, November 22, 2009
  77. 77. game loop 174 ❶ update layers repositionupdate game items ❷ collisions Sunday, November 22, 2009
  78. 78. game loop 174 ❶ update layers repositionupdate game items ❷ collisions lives points sound Sunday, November 22, 2009
  79. 79. LOOP playDATA GAME Sunday, November 22, 2009
  80. 80. GAME PLAY Sunday, November 22, 2009
  81. 81. ⇧ ⇧ ⇧ ⇧ Sunday, November 22, 2009
  82. 82. NSView subclass Interface Builder Sunday, November 22, 2009
  83. 83. NSView subclass Interface Builder Sunday, November 22, 2009
  84. 84. class KeyboardControlView < NSView def keyDown(event) end end Sunday, November 22, 2009
  85. 85. class KeyboardControlView < NSView def keyDown(event) end end ⇧ ⇧ Sunday, November 22, 2009
  86. 86. acceleration Sunday, November 22, 2009
  87. 87. relative positioning Sunday, November 22, 2009
  88. 88. Sunday, November 22, 2009
  89. 89. GAME LOOP Sunday, November 22, 2009
  90. 90. usual workflow Sunday, November 22, 2009
  91. 91. usual workflow Sunday, November 22, 2009
  92. 92. video game Sunday, November 22, 2009
  93. 93. video game Sunday, November 22, 2009
  94. 94. 30 x per second Sunday, November 22, 2009
  95. 95. moves player moves objects resolves collisions updates score/level redraws graphics plays sounds Sunday, November 22, 2009
  96. 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
  97. 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
  98. 98. IntervalTimer.new(0.03, :target => self, :selector => 'refresh_screen:') Sunday, November 22, 2009
  99. 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
  100. 100. GameData.all_layers.each do |layer| layer.update end Sunday, November 22, 2009
  101. 101. collided_bombs, collided_rubies = GameData.collisions Sunday, November 22, 2009
  102. 102. def collide_with?(other_rect) NSIntersectsRect(rect_version, other_rect) end Sunday, November 22, 2009
  103. 103. if !collided_bombs.empty? loose_a_life collided_bombs.each do |layer| layer.item.reset! end Sunday, November 22, 2009
  104. 104. else collided_rubies.each do |layer| GameData.increase_points(layer.item.points) update_points_display layer.item.reset! end Sunday, November 22, 2009
  105. 105. unless collided_rubies.empty? SoundEffects.frog(0.2) end level_change! if change_level? end Sunday, November 22, 2009
  106. 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
  107. 107. GAME DATA Sunday, November 22, 2009
  108. 108. Sunday, November 22, 2009
  109. 109. module GameData module_function end Sunday, November 22, 2009
  110. 110. class GameController def awakeFromNib GameData.register_controller(self) end end Sunday, November 22, 2009
  111. 111. GameData.all_layers Sunday, November 22, 2009
  112. 112. class GameController def display_item(item) new_layer = ImageLayer.alloc.initWithItem(item) GameData.all_layers << new_layer # [...] end end Sunday, November 22, 2009
  113. 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
  114. 114. cocoa hax Sunday, November 22, 2009
  115. 115. CocoaClass =~ RubyClass Sunday, November 22, 2009
  116. 116. don’t like an API? Sunday, November 22, 2009
  117. 117. Wrap it Sunday, November 22, 2009
  118. 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
  119. 119. compilation Sunday, November 22, 2009
  120. 120. Sunday, November 22, 2009
  121. 121. PATH="$PATH:/usr/local/bin" macruby_deploy "$TARGET_BUILD_DIR/ $PROJECT_NAME.app" --embed --no-stdlib --compile Sunday, November 22, 2009
  122. 122. wanna hack? Sunday, November 22, 2009
  123. 123. best score post score Sunday, November 22, 2009
  124. 124. gosu framework Sunday, November 22, 2009
  125. 125. chipmunk physics Sunday, November 22, 2009
  126. 126. http://github.com/ mattetti/phileas_frog Sunday, November 22, 2009
  127. 127. Sunday, November 22, 2009
  128. 128. Thanks Sunday, November 22, 2009

×