Reputation: 21
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
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
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