Rapid Game Development with RUby and Gosu – Ruby Manor 4

Post on 14-Jan-2015

9.921 views 1 download

Tags:

description

Rapid Game Development with RUby and Gosu – Ruby Manor 4

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

Rapid Game Development with

Ruby and GosuBelé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 development, prototypes to test ideas, etc.)

• Happiness :)

Prototyping

• One Game A Month www.onegameamonth.com

• Experimental Gameplay www.experimentalgameplay.com

• Ludum Darewww.ludumdare.com

Introducing Gosu

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

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

Show demo

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

The Game Loopsnippets/create_window.rb

Get player input

Update game

Draw game

60 FPS

require 'rubygems'require 'gosu'

class Game < Gosu::Window # ...end

game = Game.newgame.show

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

Imagessnippets/draw_image.rb

# 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

Inputsnippets/input.rb

# 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

Delta timesnippets/delta_time.rb

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

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

Game Dev Techniques

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/

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

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

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/

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.

click

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

calltalk_to_friend

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

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!)

The Golden Rule of Game Dev

If you can fake it, then fake it.

Thanks!Questions?