rapid game development with ruby and gosu – ruby manor 4
DESCRIPTION
Rapid Game Development with RUby and Gosu – Ruby Manor 4TRANSCRIPT
![Page 1: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/1.jpg)
Rapid Game Development with
Ruby and GosuBelén Albeza@ladybenko
![Page 2: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/2.jpg)
Aren’t games coded in C++?
![Page 3: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/3.jpg)
Minecraft(Java)
![Page 4: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/4.jpg)
To the Moon(RPG Maker)
![Page 5: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/5.jpg)
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 :)
![Page 6: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/6.jpg)
Prototyping
• One Game A Month www.onegameamonth.com
• Experimental Gameplay www.experimentalgameplay.com
• Ludum Darewww.ludumdare.com
![Page 7: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/7.jpg)
Introducing Gosu
![Page 8: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/8.jpg)
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
![Page 9: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/9.jpg)
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
![Page 10: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/10.jpg)
Show demo
![Page 11: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/11.jpg)
Gosu 101https://github.com/belen-albeza/gosu-rubymanor
![Page 12: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/12.jpg)
The Game Loopsnippets/create_window.rb
![Page 13: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/13.jpg)
Get player input
Update game
Draw game
60 FPS
![Page 14: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/14.jpg)
require 'rubygems'require 'gosu'
class Game < Gosu::Window # ...end
game = Game.newgame.show
![Page 15: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/15.jpg)
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 endend
![Page 16: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/16.jpg)
Imagessnippets/draw_image.rb
![Page 17: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/17.jpg)
# 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.
Instance of Gosu::Window
![Page 18: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/18.jpg)
![Page 19: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/19.jpg)
Inputsnippets/input.rb
![Page 20: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/20.jpg)
# callback for key up eventsdef button_up(key) close if key == Gosu::KbEscapeend
# check if a key is being presseddef update if self.button_down?(Gosu::KbLeft) move_left endend Instance of Gosu::Window
![Page 21: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/21.jpg)
Delta timesnippets/delta_time.rb
![Page 22: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/22.jpg)
4px / frame @ 60 FPSvs
240 pixels / second
13 ms 16 ms 17 ms
4 px 4px 4 px= 46 ms
= 12 px
13 ms 16 ms 17 ms
3.12 px 3.84 px 4.08 px= 46 ms
= 11.04 px
4px / frame
240 px / second
![Page 23: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/23.jpg)
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_timeend
# simple movement@x += SHIP_SPEED * @delta
# with inertia@speed_x += SHIP_ACCELERATION * @delta@x += @speed_x * @delta
![Page 24: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/24.jpg)
Distribution
• Mac: App wrapper with a Ruby on it https://github.com/jlnr/gosu/wiki/Ruby-Packaging-on-OS-X
• Windows: OCRA https://github.com/jlnr/gosu/wiki/Ruby-Packaging-on-Windows
![Page 25: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/25.jpg)
Game Dev Techniques
![Page 26: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/26.jpg)
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-collision-detection-in-2d-part-1/
![Page 27: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/27.jpg)
Finite State Machines
• Easy to implement, cheap, lots of uses...
• AI: character behaviors
• Scene stack
Patrol
ChaseAttack
seeing player?
in attacking distance?
out of attacking distance?
not seeing player?
http://www.generation5.org/content/2003/fsm_tutorial.asp
![Page 28: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/28.jpg)
Tiles
• Divide a level into a grid
• Visual grid != Logic grid... but we can map them :)
• Useful to save memory, make a level editor, implement simple physics, etc.
http://www-cs-students.stanford.edu/~amitp/gameprog.html#tiles
![Page 29: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/29.jpg)
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/
![Page 30: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/30.jpg)
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.
![Page 31: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/31.jpg)
click
event = { :type => :talk_to, :data => :friend}
calltalk_to_friend
![Page 32: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/32.jpg)
Scripting example# this method is called when the event# talk_to is triggered on the :pirate# NPCdef talk_to_pirate npc_say(:pirate, ‘Aaaarrrr’) add_to_inventory(:rum)end
![Page 33: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/33.jpg)
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!)
![Page 34: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/34.jpg)
Physics + Gosu
• Use Box2D (low-level) or Chipmunk
• Chipmunk integration tutorial at https://github.com/jlnr/gosu/wiki/Ruby-Chipmunk-Integration
![Page 35: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/35.jpg)
The Golden Rule of Game Dev
If you can fake it, then fake it.
![Page 36: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/36.jpg)
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
![Page 37: Rapid Game Development with RUby and Gosu – Ruby Manor 4](https://reader034.vdocuments.site/reader034/viewer/2022051314/54b614244a79598d038b4604/html5/thumbnails/37.jpg)
Thanks!Questions?