Skip to content

Commit

Permalink
(demo) More comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacius committed Oct 24, 2008
1 parent dbb3090 commit 4de4cd9
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions samples/demo_rubygame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def initialize( screen, background )
# PANDA CLASSES #
#################


# Base class for our panda sprites. This provides the core
# logic for initialization and movement of the sprites.
class Panda
include Sprites::Sprite
include EventHandler::HasEventHandler
Expand Down Expand Up @@ -126,6 +129,9 @@ def update( event )
end


# A panda that spins around and around. The update_image
# method is called once per frame to generate the new
# image (in this case by rotating the original image).
class SpinnyPanda < Panda
attr_accessor :rate
def initialize(x,y,rate=0.1)
Expand All @@ -141,6 +147,8 @@ def update_image(time)
end


# A panda that grows and shrinks in size. Like the other
# panda classes, it updates its image every frame.
class ExpandaPanda < Panda
attr_accessor :rate
def initialize(x,y,rate=0.1)
Expand All @@ -157,6 +165,8 @@ def update_image(time)
end


# A panda that wobbles and jiggles. Like the other
# panda classes, it updates its image every frame.
class WobblyPanda < Panda
attr_accessor :rate
def initialize(x,y,rate=0.1)
Expand All @@ -179,6 +189,8 @@ def update_image(time)
panda2 = ExpandaPanda.new(150,50)
panda3 = WobblyPanda.new(200,50,0.5)

# Set their depths. This affects which one appears in front
# of the other in case they overlap.
panda1.depth = 0 # in between the others
panda2.depth = 10 # behind both of the others
panda3.depth = -10 # in front of both of the others
Expand All @@ -191,19 +203,28 @@ def update_image(time)
###############


# Create a spritegroup to manage the pandas.
pandas = Sprites::Group.new
pandas.extend(Sprites::UpdateGroup)
pandas.extend(Sprites::DepthSortGroup)

# Add the pandas to the group.
pandas.push(panda1,panda2,panda3)


# Extend the pandas group with event hooks.
class << pandas
include EventHandler::HasEventHandler

# Draw all the sprites and refresh
# those parts of the screen
def do_draw( event )
dirty_rects = draw( event.screen )
event.screen.update_rects(dirty_rects)
end

# Erase the sprites from the screen by
# drawing over them with the background.
def do_undraw( event )
undraw( event.screen, event.background )
end
Expand Down Expand Up @@ -447,6 +468,7 @@ def toggle_smooth( event )
end
# Update the window title to display the current framerate.
def update_framerate( event )
unless @old_framerate == event.framerate
@screen.title = "Rubygame test [%d fps]"%event.framerate
Expand All @@ -455,6 +477,7 @@ def update_framerate( event )
end
# Refresh the whole screen.
def update_screen( event )
@screen.update()
end
Expand All @@ -463,14 +486,17 @@ def update_screen( event )
private
# Create a new Clock to manage the game framerate
# so it doesn't use 100% of the CPU
def _setup_clock
# Create a new Clock to manage the game framerate
# so it doesn't use 100% of the CPU
@clock = Clock.new()
@clock.target_framerate = 50
end


# Create an EventQueue to take events from the keyboard, etc.
# The events are taken from the queue and passed to objects
# as part of the main loop.
def _setup_queue
# Create EventQueue with new-style events (added in Rubygame 2.4)
@queue = EventQueue.new()
Expand All @@ -484,8 +510,12 @@ def _setup_queue


$game = Game.new( screen, background )

# Register the pandas to receive events.
$game.register( pandas, panda1, panda2 )

# Start the main game loop. It will repeat forever
# until the user quits the game!
$game.go

Rubygame.quit()

0 comments on commit 4de4cd9

Please sign in to comment.