Rapid Game Development with RUby and Gosu – Ruby Manor 4

37
Rapid Game Development with Ruby and Gosu Belén Albeza @ladybenko

description

Rapid Game Development with RUby and Gosu – Ruby Manor 4

Transcript of Rapid Game Development with RUby and Gosu – Ruby Manor 4

Page 1: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Rapid Game Development with

Ruby and GosuBelén Albeza@ladybenko

Page 2: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Aren’t games coded in C++?

Page 3: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Minecraft(Java)

Page 4: Rapid Game Development with RUby and Gosu – Ruby Manor 4

To the Moon(RPG Maker)

Page 5: Rapid Game Development with RUby and Gosu – Ruby Manor 4

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

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

Introducing Gosu

Page 8: Rapid Game Development with RUby and Gosu – Ruby Manor 4

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

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

Show demo

Page 11: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Gosu 101https://github.com/belen-albeza/gosu-rubymanor

Page 12: Rapid Game Development with RUby and Gosu – Ruby Manor 4

The Game Loopsnippets/create_window.rb

Page 13: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Get player input

Update game

Draw game

60 FPS

Page 14: Rapid Game Development with RUby and Gosu – Ruby Manor 4

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

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

Imagessnippets/draw_image.rb

Page 17: Rapid Game Development with RUby and Gosu – Ruby Manor 4

# 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
Page 19: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Inputsnippets/input.rb

Page 20: Rapid Game Development with RUby and Gosu – Ruby Manor 4

# 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

Delta timesnippets/delta_time.rb

Page 22: Rapid Game Development with RUby and Gosu – Ruby Manor 4

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

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 25: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Game Dev Techniques

Page 26: Rapid Game Development with RUby and Gosu – Ruby Manor 4

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

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

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

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

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

click

event = { :type => :talk_to, :data => :friend}

calltalk_to_friend

Page 32: Rapid Game Development with RUby and Gosu – Ruby Manor 4

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

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 35: Rapid Game Development with RUby and Gosu – Ruby Manor 4

The Golden Rule of Game Dev

If you can fake it, then fake it.

Page 37: Rapid Game Development with RUby and Gosu – Ruby Manor 4

Thanks!Questions?