Forums Archived

The forums are being archived. See this topic for more information.

Displaying Background and Objects

Get help and support with Rubygame

Displaying Background and Objects

Postby Fridgecrisis » Fri Sep 24, 2010 11:18 pm

I'm working on an RPG project, and I'm having a problem with displaying the background map and the objects on it correctly. My current approach is to have two surfaces: a background surface containing a .png image, and a second surface for displaying objects. At the beginning of the program, I tell the background surface to draw to the screen, only once. Then on each update, the object surface fills with a certain grey color I've set as that surface's colorkey (so it's as good as transparent) and then I draw my objects onto that surface.

So you'd think this would let me keep my background image on the screen and have this second, mostly transparent surface updating over it, but the background just looks black when I run the program. I currently have the background drawing again on each frame, but that slows down the program considerably.

I'm wondering what the "theory" is behind drawing and updating backgrounds and the objects over them. What's the best approach?

Here's the applicable part of my code. A second class that inherits from this one supplies the name of the map, a couple objects, and turns @active to true.

Code: Select all
class Map
  def initialize(name)
    @background = Rubygame::Surface.load("#{name}.png")
    @w = @background.w
    @h = @background.h
    @surface =[@w, @h])
    @surface.colorkey = [100, 100, 100]
    @screen_rect =, 0, $screen.w, $screen.h)
    @objects = {}
    @active = false

  def draw
    return if @active == false
    @surface.fill([100, 100, 100])
    @background.blit($screen, [0, 0], [0, 0, @w, @h]) #This makes the program run slower, but it's the only way it seems to work...
    @surface.blit($screen, [0, 0], @screen_rect)
  def draw_objects
    return if @active == false
    @objects.each_value do |obj|
  def draw_background
    @background.blit($screen, [0, 0], [0, 0, @w, @h])

Okay, I discovered a screen.fill in my main script. Removing it allowed the background to be seen, but everything was being drawn on top of the old stuff, creating hundreds of little guys all over the screen.

I've been thinking, and it seems that I will need to draw the background to the screen on every frame, whether I want to or not. But why does it affect the program's performance so much? The surface is a simple .png file, 1000 x 1000 pixels, stored in an instance variable. What am I doing wrong?
Posts: 4
Joined: Tue Sep 21, 2010 4:15 pm

Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest