Heads Up Display

From Rubygame

Jump to: navigation, search

This wiki has moved! You can view and edit the new wiki on GitHub!

# Basic example of bliting some text on the screen
 
require 'rubygame'
 
# for convenience/readability.
include Rubygame
include Rubygame::Events
 
# A class representing the HUD
class Hud
 
  # construct the HUD
  def initialize options
    @screen = options[:screen]  # need that to blit on it
 
    # TTF.setup must be called before any font object is created
    TTF.setup
 
    # point to the TTF file
    filename = File.join(File.dirname(__FILE__), '..', 'resources', 'fonts', 'SF Cosmic Age Condensed.ttf')
 
    # creates the font object, which is used for drawing text
    @cosmic_font = TTF.new filename, 24
 
    # initialize options that are displayed, here time
    @time = "-"
 
  end
 
  # called from the game class in each loop. updates options that are displayed
  def update options
    @time = options[:time]
  end
 
  # called from the game class in the draw method. render any options 
  # and blit the surface on the screen
  def draw
    timer = @cosmic_font.render @time.to_i.to_s, true, [123,123,123]
    timer.blit @screen, [@screen.w-timer.w-6, 6]   # blit to upper right corner
  end
 
end

in the game class:

# to create the hud
@hud = Hud.new :screen => @screen
 
# to update contents
@hud.update :time => @time
 
# to draw
@screen.fill :black
@hud.draw
@screen.update
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox