Skip to main content
GameDev.net gamedev.net
🔒 Locked

colorkey problems

Started by Gabriel Marincu Aug 24, 2012 at 11:59 AM 1 replies 1.1k views
Original Post
Gabriel Marincu
Gabriel Marincu
Hello everyone...I've recently managed to put up some code for a tile based game and it's working well but i've encountered a problem.
I want to use non-colorkeyed images as base tiles(grass,dirt,rock,road), but if i draw a higher resolution image and load it with Pygame's basic image loading function (pygame.image.load) it lags a lot, but if i make the (0,0) pixel white and colorkey that, it works with no lag but it wrecks my tile grid's appearence.

Any ideas why that might happen?
Servant of the Lord
Servant of the Lord
I don't use Python or PyGame, but I know PyGame is based on SDL (a C-language library) which I am familiar with. SDL images are sometimes loaded in formats that aren't the same format as the one being drawn to, causing slowdowns as each draw has to perform some conversions. Typically the solution is to convert the images once, after you load them, instead of costly converting them every draw. I'm guessing this same problem is what is occurring in your PyGame program, and your color-keying probably is converting the images for you without you realizing it.

A few google searches shows that this is likely your problem. See here.
Try calling this code after you load the image but before you start using it for drawing:
myImage = myImage.convert()
It should convert your image to the same image format as the screen (so no conversions are needed when rendering to the screen).
Note: You can't just call:
myImage.convert()
...because convert() returns a new copy without altering the old, so you have to make sure to catch the return value.
It must be:
myImage = myImage.convert()
Gabriel Marincu
Gabriel Marincu
Thanks! I've applied this function to my images and it has reduced the lag but I found out that most part of the lag was because i was calling the map loading function each time the game loop iterated.I fixed both of those problems and now it works like a charm.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.