MaxTriesToCode
MaxTriesToCode

Reputation: 45

How to fix lag in pygame when drawing multiple surfaces

What I want in my game is a circle around the player where everything is visible but outside of that circle its very hard to see stuff. Basically a flashlight aura and everything outside of that is dark and hard to see. It took me a while to get it working but now that I did the game runs at like 20FPS and is visibly laggy when normally it was running fine at 60FPS. Any ideas of how to keep the same idea but make the game run better?

class Flashlight:
    def __init__(self):
        self.flashlight_surf = pygame.Surface((SCREEN_W, SCREEN_H))
        self.flashlight_surf2 = pygame.Surface((SCREEN_W, SCREEN_H))

        self.flashlight_surf2.set_colorkey((1,1,1))
        self.flashlight_surf2.set_alpha(200)
        self.flashlight_radius = 200

    def flashlight_update(self):
        self.flashlight_surf2.fill(black)

        pygame.draw.circle(self.flashlight_surf2, (1,1,1), (round(player.x) + 
        32, round(player.y) + 32), self.flashlight_radius)

        display.blit(self.flashlight_surf2, (0,0))
        self.flashlight_surf2.blit(self.flashlight_surf, (0,0))

Upvotes: 1

Views: 330

Answers (1)

Resistnz
Resistnz

Reputation: 72

Try changing

self.flashlight_surf2 = pygame.Surface((SCREEN_W, SCREEN_H))

to

self.flashlight_surf2 = pygame.Surface((SCREEN_W, SCREEN_H)).convert()

Converting the surface can increase the blitting speed by 6 times as stated here

If convert() doesn't work use convert_alpha()

Upvotes: 1

Related Questions