1
votes

Why does my game start to lag when i change the background image? i just change the image when my character collides with the door? heres some code..

this declares the backgrounds

bg = pygame.image.load("map.png")
bg1 = pygame.image.load("house interior.png")
bgnum = 1

this is where i switch them...

 if bgnum == 1:
        if x > 158 and x < 379 and y > 115 and y <295:
            x_change = 0

        if y > 82 and y < 295 and x > 158 and x < 379:
            y_change = 0

        if x > 535 and x < 749 and y > 82 and y <295:
            x_change = 0

        if y > 82 and y < 295 and x > 540 and x < 754:
            y_change = 0

        if y > 285 and y < 305 and x > 610 and x < 660:
            bgnum = 2
            bg = bg1

my game lags quite a bit when i enter the door and the background changes..

2
Can you paste your whole file or files? I can't see or reproduce any issues from the snippet you've posted. - spirulence

2 Answers

0
votes

A tentative guess - you haven't called convert() or convert_alpha() on the background images before you're blitting them. I don't know this for sure because you haven't (yet) provided us with a full code example.

0
votes

Expanding on spirulense's answer, whenever you load an image, always add .convert_alpha() to the end. I always use a function to do this so I have to type less. Feel free to use the following function in your code:

def loadify(imgname):
    return pygame.image.load(imgname).convert_alpha()

This function greatly increases the blitting speed of your images. It made one of my games go from 18 to 30 fps! You always want to use this unless you require a specific image format, like in image conversion programs, but when making video games, this should work in all cases.

If your game still lags, more coding is going to need to be shown to be able to solve your problem.

Try this website:

https://www.pygame.org/docs/tut/newbieguide.html

I'm not calling you a newbie, even I learned some stuff from there, and I've been coding for 6 years! There is a lot of advice in there telling how to speed up your code.

Good luck!