1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Loading and Displaying Images in Gosu


Loading and Displaying Images with Gosu
Loading and Displaying Images in Gosu

To oversimplify a rather complex topic, a game is a program that loads images and displays them at the screen. So vital to game programming, loading and displaying images is one of the first things you should learn. Gosu does a pretty good job of making this easy, to a certain degree.

Gosu can load two image formats: BMP and PNG. If you have to load images in any other format, Gosu can also interface with the RMagick library (which in turn uses the ImageMagick library). However, that's not covered by this article so if you have images in other formats, it's recommended that you convert them to PNG first. PNG is used throughout this series of articles, as is supports alpha channels and is compressed.

To load an image, call the Gosu::Image.new method. It takes three arguments: the game window, a filename, and a true or false parameter we'll call "tileable" for now. Notice that the first parameter is the game window. This is the first parameter to many Gosu methods, so we'll have to design our code around this fact. The object returned is an image loaded into video memory that can be displayed on the screen.

The following is a minimal program that creates a window, loads an image and displays it at the top-left corner of the screen. In order for this program to work, you'll need an image.png file in the same directory as this Ruby file. You can download this script with an image file here.

#!/usr/bin/env ruby
require 'rubygems'
require 'gosu'

class MyWindow < Gosu::Window
 def initialize
    super(640, 480, false)
    @image = Gosu::Image.new(self, 'image.png', false)

 def draw
    @image.draw(0, 0, 1)


Since there's only really two lines of code here, things should be pretty self-explanatory. The image is loaded with Gosu::Image.new, and displayed with the image's draw method. The image's draw method takes three parameters, the X and Y coordinates and a Z coordinate. There will be more on Z coordinates later, but for now, just use the value 1 for the Z coordinate.

If you're wondering about the tileable parameter to the Gosu::Image.new method, you won't need to pass true for this unless you're making tilemaps or other sprites that must match up on their edges. When sprites are displayed by Gosu, if the sprite is scaled the edges can be "blurry," a product of the scaling used by the graphics card. This is usually a good thing, it gives scaled sprites a smoother look. However, if these images must be displayed right next to each other, the edges must be flat. So the only real reason you'd need to pass true to the third parameter of Gosu::Image.new is if you're using tilemaps.

Now that you know how to load and display images, it's time to learn how to poll for keyboard and mouse input. Then, you can start making some interesting things happen.

This article is part of a series. Read more articles about Rapid Game Prototyping in Ruby

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Rapid Game Prototyping with Gosu
  6. Loading and Displaying Images in Gosu

©2014 About.com. All rights reserved.