Starting simple
From Rubygame
This wiki has moved! You can view and edit the new wiki on GitHub!
The examples below are working examples. To run them, copy all of the code for an example into a new file called main.rb and then type:
ruby main.rb
..at the command line.
Contents |
Hello Rubygame World!
This is the simplest possible example that runs.
#!/usr/bin/env ruby require "rubygems" require "rubygame" # Open a window with a drawable area measuring 640x480 pixels @screen = Rubygame::Screen.open [ 640, 480] # Set the title of the window @screen.title = "Hello Rubygame World!" # Create a queue to receive events+ # + events such as "the mouse has moved", "a key has been pressed" and so on @event_queue = Rubygame::EventQueue.new # Use new style events so that this software will work with Rubygame 3.0 @event_queue.enable_new_style_events # Wait for an event while event = @event_queue.wait # Show the details of the event p event # Stop this program if the user closes the window break if event.is_a? Rubygame::Events::QuitRequested end
Screen
This example demonstrates some of the features of the Screen class, such as:
- determining the resolution of the display
- running in full-screen mode
- drawing directly to the screen
- double-buffering
#!/usr/bin/env ruby require "rubygems" require "rubygame" # Import the Rubygame module into the current namespace, so that "Screen" may # be referred directly instead of having to write "Rubygame::Screen" include Rubygame # Report the current dimensions of the desktop in pixels. A fullscreen window # should be able to achieve at least this resolution. maximum_resolution = Screen.get_resolution puts "This display can manage at least " + maximum_resolution.join("x") # Open a double-buffered, video-RAM-based window in full-screen mode at the # maximum resolution default_depth = 0 @screen = Screen.open maximum_resolution, default_depth, [ HWSURFACE, DOUBLEBUF, FULLSCREEN] # Show the color depth of the screen puts "The screen has a color depth of %i bits" % @screen.depth # Hide the mouse cursor @screen.show_cursor = false # Screen is a Surface, so all methods on Surface are available center = maximum_resolution.collect! {|axis| axis / 2} radius = maximum_resolution.min - 16 color = [ 0xc0, 0x80, 0x40] @screen.draw_circle center, radius, color # Show the changes to the screen surface by flipping the buffer that is visible # to the user. All changes made to the screen surface will appear # simultaneously @screen.flip # Wait until a key is pressed @event_queue = EventQueue.new @event_queue.enable_new_style_events until @event_queue.wait().is_a? Events::KeyPressed end
TrueType fonts
This example demonstrates the ease with which anti-aliased TrueType fonts may be used.
NOTE: A TrueType font in a file named DejaVuSans-Bold.ttf is required for this example to run.
#!/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
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:
- background.jpg - should be at least 640x480
- meanie.png - I used a 32x32 Firefox icon
#!/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