1. Computing
Send to a Friend via Email

Loading and Displaying Your Sprite

By

5 of 5

Creating and Displaying the Player

Now that you have a player class, you can create and display a player. There's no way to move him around just yet, he'll just sit there on the screen, see the next article in this series to learn how to do that.

Creating a player is very easy, just call player = Player.new. Drawing the player is just as easy--just call player.draw(screen). However, the order in which you draw things is important, since everything you draw writes over the things you previously drew. If you were to draw the background last it would draw over everything else you've drawn since it takes up the whole screen. Construct the player before your main loop, near where you loaded the background image and, just after you draw your background image, display the player in your main loop.

def main_loop
  screen = Screen.new [640, 480]
  queue = EventQueue.new
  clock = Clock.new
  clock.target_framerate = 30

  background = Surface['background.png']
  player = Player.new

  loop do
    clock.tick

    queue.each do|e|
      return if e.is_a? QuitEvent
    end

    background.blit( screen, [0,0] )
    player.draw( screen )

    screen.update
  end

ensure
  Rubygame.quit
end
 
  1. About.com
  2. Computing
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. Creating and Displaying the Player

©2014 About.com. All rights reserved.