MottledKarma517
MottledKarma517

Reputation: 21

Pygame - blit is causing lag

I am making a simple platformer game, when I add a background using pygame.surface.blit() it causes a lot of lag, However when I use pygame.suface.fill() it runs at ~120 fps.

How do I fix this?

# map vars
mapFloor = []
ground = 700
mapController(True)
bg1Colour = bg1Colour = transform.scale(image.load('assets/PNG/Backgrounds/set1_background.png'), (1280, 1024))
bg1Colour = [bg1Colour, bg1Colour.get_rect()]
bg1Tiles = transform.scale(image.load('assets/PNG/Backgrounds/set1_tiles.png'), (1280, 1024))
bg1Tiles = [bg1Tiles, bg1Tiles.get_rect()]
bg1Hills = transform.scale(image.load('assets/PNG/Backgrounds/set1_hills.png'), (1280, 1024))
bg1Hills = [bg1Hills, bg1Hills.get_rect()]            
           


while not finished:

    for events in event.get():
        exiCheck()
        playerController(True)

    surface.blit(bg1Colour[0], bg1Colour[1])
    surface.blit(bg1Tiles[0], bg1Tiles[1])
    surface.blit(bg1Hills[0], bg1Hills[1])

    mapController(False)
    player = playerController(False)

    display.flip()
    clock.tick(120)

Upvotes: 0

Views: 551

Answers (2)

Rabbid76
Rabbid76

Reputation: 210878

Ensure that the background Surface has the same format as the display Surface. Use convert() (or convert_alpha()) to create a Surface that has the same pixel format. This improves performance when the background is, when the background is blit on the display, because the formats are compatible and blit does not need to perform an implicit transformation.

bg_surf = image.load('assets/PNG/Backgrounds/set1_background.png').convert()
bg1Colour = bg1Colour = transform.scale(bg_surf, (1280, 1024))

Upvotes: 1

Azeem8869786
Azeem8869786

Reputation: 97

Just try to not using lists or tuple try by assingnig them so that the terminal should not check the lists and just use the signed values

Upvotes: 1

Related Questions