Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating Tile Background pygame

Is there a feature similar to this in pygame?

screen.fill(pygame.image.load('brick.bmp').convert()) 

I would like to fill the window (background) with an image to produce an effect similar to this:

enter image description here

My only idea would be to blit each brick onto the screen, but i think that would slow down the game. Does anyone have an idea how to do this (and also provide some example code)?

Heres a snippet of a method that works but goes slow

    While True:
        for y in range(0,600,32):
            for x in range(0,800,32):
                screen.blit( self.img,(x,y))
        ...
        pygame.display.flip()
like image 913
user1357159 Avatar asked Jun 01 '12 23:06

user1357159


2 Answers

blit each brick onto the screen, but i think that would slow down the game.

Not as much as you probably think. bliting is very fast. Even if there were some sort of "fill" command, it would still amount to copying the image to a surface until the surface is covered.

If the background image is stationary, you can render to an off-screen surface when your program starts, and instead of erasing the screen by filling with a solid color, just blit the pre-rendered background instead.

like image 114
SingleNegationElimination Avatar answered Oct 11 '22 17:10

SingleNegationElimination


I can't find this feature in Pygame. However, I have a way to improve the performence of your method. You can create a surface first. Let's call it bgSurface. Then you blit all bricks to bgSurface using your double loop method. Then in the main loop, you do screen.blit(bgSurface, (0, 0)). Have a try.

like image 38
Ray Avatar answered Oct 11 '22 19:10

Ray