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. Prototyping
• One Game A Month
www.onegameamonth.com
• Experimental Gameplay
www.experimentalgameplay.com
• Ludum Dare
www.ludumdare.com
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. 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
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
20. # 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
22. 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
23. 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
24. 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
26. 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/
27. 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
28. 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
29. 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/
30. 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.
32. 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
33. 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!)
34. Physics + Gosu
• Use Box2D (low-level) or Chipmunk
• Chipmunk integration tutorial at https://
github.com/jlnr/gosu/wiki/Ruby-ChipmunkIntegration
35. The Golden Rule of Game Dev
If you can fake it,
then fake it.