Rapid Game
Development with
Ruby and Gosu
Belén Albeza
@ladybenko
Aren’t games coded in
C++?
Minecraft
(Java)
To the Moon
(RPG Maker)
So?
• Some games will require C++
• Some games won’t
• You can trade performance for:
• Better productivity (faster develo...
Prototyping
• One Game A Month

www.onegameamonth.com

• Experimental Gameplay

www.experimentalgameplay.com

• Ludum Dare...
Introducing Gosu
What is Gosu?
• Gosu is a minimalistic 2D game library
www.libgosu.org

• Free, Open source (MIT License)
• Multiplatform ...
Gosu’s API is very small
• ~100 methods in 9 classes
• Gosu provides a way to:
• Create an OpenGL window
• Load and draw i...
Show demo
Gosu 101

https://github.com/belen-albeza/gosu-rubymanor
The Game Loop
snippets/create_window.rb
Get player input

60 FPS

Update game

Draw game
require 'rubygems'
require 'gosu'
class Game < Gosu::Window
# ...
end
game = Game.new
game.show
class Game < Gosu::Window
def initialize
super(800, 600, false)
end
def draw # gets called every frame
end
def update # ge...
Images

snippets/draw_image.rb
Instance of Gosu::Window
# load
@img_bg =
Gosu::Image.new(self,‘space.png’)
# draw
@img_bg.draw(0, 0, 0)
@ship.draw_rot(40...
Input

snippets/input.rb
# callback for key up events
def button_up(key)
close if key == Gosu::KbEscape
end
# check if a key is being pressed
def u...
Delta time

snippets/delta_time.rb
4px / frame
= 12 px
4 px

4px

4 px
= 46 ms

13 ms

4px / frame @ 60 FPS
vs
240 pixels / second

16 ms

17 ms

240 px / se...
def update_delta
current_time = Gosu::milliseconds /
1000.0
# tip: always cap your delta
@delta = [current_time - @last_ti...
Distribution
• Mac: App wrapper with a Ruby on it

https://github.com/jlnr/gosu/wiki/RubyPackaging-on-OS-X

• Windows: OCR...
Game Dev Techniques
Bounding boxes
•

Quick collisions, but not
very accurate

•

Shapes can be combined
to increase accuracy

•

Beware of ro...
Finite State Machines
•
•
•

Patrol

Easy to implement,
cheap, lots of uses...
AI: character behaviors
Scene stack

seeing...
Tiles
•
•

Divide a level into a grid

•

Useful to save memory,
make a level editor,
implement simple
physics, etc.

Visu...
Path-finding
•

They are usually very
expensive... try to
minimise their use

•

Dijkstra is enough for
simple graphs (ie. ...
Scripting
• Scripting transforms a simple arcade level

into a mission or a quest (see Cave Story)

• Embed a VM into your...
event = {
:type => :talk_to,
:data => :friend
}

click

call
talk_to_friend
Scripting example
# this method is called when the event
# talk_to is triggered on the :pirate
# NPC
def talk_to_pirate
np...
Physics engine
•

Real physics for your
games! Done by smart
people! And free!

•

They are slow, so try to
minimise the a...
Physics + Gosu
• Use Box2D (low-level) or Chipmunk
• Chipmunk integration tutorial at https://

github.com/jlnr/gosu/wiki/...
The Golden Rule of Game Dev

If you can fake it,
then fake it.
Resources
• Chingu: game framework for Gosu https://
github.com/ippa/chingu

• Creative Commons art: http://

www.lostgard...
Thanks!
Questions?
Rapid Game Development with RUby and Gosu – Ruby Manor 4
Upcoming SlideShare
Loading in …5
×

Rapid Game Development with RUby and Gosu – Ruby Manor 4

13,558 views

Published on

Rapid Game Development with RUby and Gosu – Ruby Manor 4

Published in: Technology, Design

Rapid Game Development with RUby and Gosu – Ruby Manor 4

  1. 1. Rapid Game Development with Ruby and Gosu Belén Albeza @ladybenko
  2. 2. Aren’t games coded in C++?
  3. 3. Minecraft (Java)
  4. 4. To the Moon (RPG Maker)
  5. 5. So? • Some games will require C++ • Some games won’t • You can trade performance for: • Better productivity (faster development, prototypes to test ideas, etc.) • Happiness :)
  6. 6. Prototyping • One Game A Month www.onegameamonth.com • Experimental Gameplay www.experimentalgameplay.com • Ludum Dare www.ludumdare.com
  7. 7. Introducing Gosu
  8. 8. What is Gosu? • Gosu is a minimalistic 2D game library www.libgosu.org • Free, Open source (MIT License) • Multiplatform (Win, OS X, Linux) • Has bindings for Ruby and C++ • $gem install gosu
  9. 9. Gosu’s API is very small • ~100 methods in 9 classes • Gosu provides a way to: • Create an OpenGL window • Load and draw images and fonts • Load and play sounds • Gather player’s input
  10. 10. Show demo
  11. 11. Gosu 101 https://github.com/belen-albeza/gosu-rubymanor
  12. 12. The Game Loop snippets/create_window.rb
  13. 13. Get player input 60 FPS Update game Draw game
  14. 14. require 'rubygems' require 'gosu' class Game < Gosu::Window # ... end game = Game.new game.show
  15. 15. class Game < Gosu::Window def initialize super(800, 600, false) end def draw # gets called every frame end def update # gets called every frame end def button_up(key) # callback end end
  16. 16. Images snippets/draw_image.rb
  17. 17. Instance of Gosu::Window # load @img_bg = Gosu::Image.new(self,‘space.png’) # draw @img_bg.draw(0, 0, 0) @ship.draw_rot(400, 300, 0, 45) # note: audio and fonts follow the same # approach.
  18. 18. Input snippets/input.rb
  19. 19. # callback for key up events def button_up(key) close if key == Gosu::KbEscape end # check if a key is being pressed def update if self.button_down?(Gosu::KbLeft) move_left end end Instance of Gosu::Window
  20. 20. Delta time snippets/delta_time.rb
  21. 21. 4px / frame = 12 px 4 px 4px 4 px = 46 ms 13 ms 4px / frame @ 60 FPS vs 240 pixels / second 16 ms 17 ms 240 px / second = 11.04 px 3.12 px 3.84 px 4.08 px = 46 ms 13 ms 16 ms 17 ms
  22. 22. def update_delta current_time = Gosu::milliseconds / 1000.0 # tip: always cap your delta @delta = [current_time - @last_time, 0.25].min @last_time = current_time end # simple movement @x += SHIP_SPEED * @delta # with inertia @speed_x += SHIP_ACCELERATION * @delta @x += @speed_x * @delta
  23. 23. Distribution • Mac: App wrapper with a Ruby on it https://github.com/jlnr/gosu/wiki/RubyPackaging-on-OS-X • Windows: OCRA https://github.com/jlnr/ gosu/wiki/Ruby-Packaging-on-Windows
  24. 24. Game Dev Techniques
  25. 25. Bounding boxes • Quick collisions, but not very accurate • Shapes can be combined to increase accuracy • Beware of rotations! http://devmag.org.za/2009/04/13/basic-collisiondetection-in-2d-part-1/
  26. 26. Finite State Machines • • • Patrol Easy to implement, cheap, lots of uses... AI: character behaviors Scene stack seeing player? not seeing player? out of attacking distance? Chase Attack in attacking distance? http://www.generation5.org/content/2003/ fsm_tutorial.asp
  27. 27. Tiles • • Divide a level into a grid • Useful to save memory, make a level editor, implement simple physics, etc. Visual grid != Logic grid... but we can map them :) http://www-cs-students.stanford.edu/~amitp/ gameprog.html#tiles
  28. 28. Path-finding • They are usually very expensive... try to minimise their use • Dijkstra is enough for simple graphs (ie. an adventure) • A* for everything else (action RPG’s, strategy, etc.) http://theory.stanford.edu/~amitp/GameProgramming/
  29. 29. Scripting • Scripting transforms a simple arcade level into a mission or a quest (see Cave Story) • Embed a VM into your engine (most popular for games is Lua)... but Ruby is already a script language :D • Useful triggers: enter an area, exit an area, talk to NPC, pick up item, kill an enemy, etc.
  30. 30. event = { :type => :talk_to, :data => :friend } click call talk_to_friend
  31. 31. Scripting example # this method is called when the event # talk_to is triggered on the :pirate # NPC def talk_to_pirate npc_say(:pirate, ‘Aaaarrrr’) add_to_inventory(:rum) end
  32. 32. Physics engine • Real physics for your games! Done by smart people! And free! • They are slow, so try to minimise the amount of physical entities • You need to map your visual world into an invisible physical world (beware of units!)
  33. 33. Physics + Gosu • Use Box2D (low-level) or Chipmunk • Chipmunk integration tutorial at https:// github.com/jlnr/gosu/wiki/Ruby-ChipmunkIntegration
  34. 34. The Golden Rule of Game Dev If you can fake it, then fake it.
  35. 35. Resources • Chingu: game framework for Gosu https:// github.com/ippa/chingu • Creative Commons art: http:// www.lostgarden.com/search/label/free %20game%20graphics • More: http://www.libgosu.org/cgi-bin/mwf/ board_show.pl?bid=4
  36. 36. Thanks! Questions?

×