Skip to content

Commit

Permalink
(demo) Pandas update when ClockTicked.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacius committed Oct 24, 2008
1 parent 4f6f582 commit 4595e24
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions samples/demo_rubygame.rb
Expand Up @@ -33,6 +33,18 @@
end


# Custom event class to hold information about the
# clock, created each frame.
class ClockTicked
attr_reader :time, :framerate

def initialize( ms, framerate )
@time = ms / 1000.0
@framerate = framerate
end
end


# Set up autoloading for Surfaces. Surfaces will be loaded automatically
# the first time you use Surface["filename"]. Check out the docs for
# Rubygame::NamedResource for more info about that.
Expand All @@ -55,18 +67,21 @@ def initialize(x,y)
@speed = 40
@image = @@pandapic
@rect = Rect.new(x,y,*@@pandapic.size)

make_magic_hooks( ClockTicked => :update )

end

def update_image(time)
# do nothing in base class, rotate/zoom image in subs
end

def update(time)
def update( event )
x,y = @rect.center
self.update_image(time)
self.update_image( event.time * 1000.0 )
@rect.size = @image.size

base = @speed * time/1000.0
base = @speed * event.time
@rect.centerx = x + @vx * base
@rect.centery = y + @vy * base
end
Expand Down Expand Up @@ -301,17 +316,6 @@ def joyreleased( button )



class ClockTicked
attr_reader :time, :framerate

def initialize( ms, framerate )
@time = ms / 1000.0
@framerate = framerate
end
end



class Game
include EventHandler::HasEventHandler

Expand Down Expand Up @@ -379,24 +383,24 @@ def update_screen( event )
$game = Game.new( screen )
$game.register( panda1, panda2 )
$game.register( panda1, panda2, panda3 )
catch(:rubygame_quit) do
loop do
pandas.undraw(screen,background)
queue.each do |event|
$game.handle( event )
end
pandas.undraw(screen,background)
pandas.update(update_time)
dirty_rects = pandas.draw(screen)
screen.update_rects(dirty_rects)
update_time = clock.tick
queue << ClockTicked.new( update_time, clock.framerate )
queue << ClockTicked.new( clock.tick, clock.framerate )
end
end
Expand Down

0 comments on commit 4595e24

Please sign in to comment.