Starting simple

From Rubygame

Jump to: navigation, search
(New page: The examples below are working examples. To run them, copy all of the code for an example into a new file called <tt>main.rb</tt> and then type: ruby main.rb ..at the command line. ...)
 
Line 27: Line 27:
      
      
# Use new style events so that this software will work with Rubygame 3.0
# Use new style events so that this software will work with Rubygame 3.0
-
@event_queue.enable_new_style_events  
+
@event_queue.enable_new_style_events
      
      
# Wait for an event
# Wait for an event
Line 33: Line 33:
      
      
   # Show the details of the event
   # Show the details of the event
-
   puts  event.inspect
+
   p event
      
      
   # Stop this program if the user closes the window
   # Stop this program if the user closes the window
Line 90: Line 90:
# Wait until a key is pressed
# Wait until a key is pressed
-
@event_queue = Rubygame::EventQueue.new
+
@event_queue = EventQueue.new
@event_queue.enable_new_style_events
@event_queue.enable_new_style_events
-
until @event_queue.wait().is_a? Rubygame::Events::KeyPressed
+
until @event_queue.wait().is_a? Events::KeyPressed
 +
end
 +
</source>
 +
 
 +
 
 +
== TrueType fonts ==
 +
 
 +
This example demonstrates the ease with which anti-aliased TrueType fonts may be used.
 +
 
 +
'''NOTE:''' A TrueType font in a file named <tt>DejaVuSans-Bold.ttf</tt> is required for this example to run.
 +
 
 +
<source lang="ruby">
 +
#!/usr/bin/env ruby
 +
 
 +
require "rubygems"
 +
require "rubygame"
 +
 
 +
include Rubygame
 +
 
 +
# Set up the TrueType Font module
 +
TTF.setup
 +
point_size = 20
 +
$font = TTF.new "DejaVuSans-Bold.ttf", point_size
 +
 
 +
@screen = Screen.open [ 640, 480]
 +
 
 +
# Render some text to a new surface using the TrueType font.  Using render_utf8
 +
# is good because players might input text with accents or other non-ASCII text
 +
smooth = true
 +
YELLOW = [ 0xee, 0xee, 0x33]
 +
@text_surface = $font.render_utf8 "Hello TrueType text!", smooth, YELLOW
 +
 
 +
# Determine the dimensions in pixels of the area used to render the text.  The
 +
# "topleft" of the returned rectangle is at [ 0, 0]
 +
rt = @text_surface.make_rect
 +
 
 +
# Re-use the "topleft" of the rectangle to indicate where the text should
 +
# appear on screen ( in this case, 8 pixels from the top right hand corner of
 +
# the screen
 +
rt.topleft = [ @screen.width - 8 - rt.width,  8]
 +
 
 +
# Copy the pixels of the rendered text to the screen
 +
@text_surface.blit @screen, rt
 +
 
 +
@screen.flip
 +
@event_queue = EventQueue.new
 +
@event_queue.enable_new_style_events
 +
until @event_queue.wait().is_a? Events::KeyPressed
 +
end
 +
</source>
 +
 
 +
 
 +
== Sprites ==
 +
 
 +
This example demonstrates:
 +
 
 +
* how to turn an object into a sprite
 +
* framerate-independent animation
 +
* sprite groups
 +
 
 +
'''NOTE:''' This example requires two image files in order to run:
 +
* <tt>background.jpg</tt> - should be at least 640x480
 +
* <tt>meanie.png</tt> - I used a 32x32 Firefox icon
 +
 
 +
<source lang="ruby">
 +
#!/usr/bin/env ruby
 +
 
 +
require "rubygems"
 +
require "rubygame"
 +
 
 +
include Rubygame
 +
 
 +
@screen = Screen.open [ 640, 480]
 +
 
 +
 
 +
# Defines a class for an example object in the game that will have a
 +
# representation on screen ( a sprite)
 +
class Meanie
 +
 
 +
  # Turn this object into a sprite
 +
  include Sprites::Sprite
 +
 
 +
  def initialize
 +
    # Invoking the base class constructor is important and yet easy to forget:
 +
    super()
 +
 
 +
    # @image and @rect are expected by the Rubygame sprite code
 +
    @image = Surface.load "meanie.png"
 +
    @rect  = @image.make_rect
 +
 
 +
    @angle = 2*Math::PI * rand
 +
  end
 +
 
 +
  # Animate this object.  "seconds_passed" contains the number of ( real-world)
 +
  # seconds that have passed since the last time this object was updated and is
 +
  # therefore useful for working out how far the object should move ( which
 +
  # should be independent of the frame rate)
 +
  def update  seconds_passed
 +
 
 +
    # This example makes the objects orbit around the center of the screen.
 +
    # The objects make one orbit every 4 seconds
 +
    @angle = ( @angle + 2*Math::PI / 4 * seconds_passed) % ( 2*Math::PI)
 +
 
 +
    @rect.topleft = [ 320 + 100 * Math.sin(@angle),
 +
                      240 - 100 * Math.cos(@angle)]
 +
  end
 +
 
 +
  def draw  on_surface
 +
    @image.blit  on_surface, @rect
 +
  end
 +
end
 +
 
 +
 
 +
@clock = Clock.new
 +
@clock.target_framerate = 60
 +
 
 +
# Ask Clock.tick() to return ClockTicked objects instead of the number of
 +
# milliseconds that have passed:
 +
@clock.enable_tick_events
 +
 
 +
# Create a new group of sprites so that all sprites in the group may be updated
 +
# or drawn with a single method invocation.
 +
@sprites = Sprites::Group.new
 +
Sprites::UpdateGroup.extend_object @sprites
 +
3.times do @sprites << Meanie.new end
 +
 
 +
# Load a background image and copy it to the screen
 +
@background = Surface.load "background.jpg"
 +
@background.blit @screen, [ 0, 0]
 +
 
 +
@event_queue = EventQueue.new
 +
@event_queue.enable_new_style_events
 +
 
 +
should_run = true
 +
while should_run do
 +
 
 +
  seconds_passed = @clock.tick().seconds
 +
 
 +
  @event_queue.each do |event|
 +
    case event
 +
      when Events::QuitRequested, Events::KeyReleased
 +
        should_run = false
 +
    end
 +
  end
 +
 
 +
  # "undraw" all of the sprites by drawing the background image at their
 +
  # current location ( before their location has been changed by the animation)
 +
  @sprites.undraw @screen, @background
 +
 
 +
  # Give all of the sprites an opportunity to move themselves to a new location
 +
  @sprites.update  seconds_passed
 +
 
 +
  # Draw all of the sprites
 +
  @sprites.draw @screen
 +
 
 +
  @screen.flip
end
end
</source>
</source>

Latest revision as of 04:01, 9 February 2011

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox