Skip to main content
removed retired [tag:surface] : https://gamedev.meta.stackexchange.com/a/2867/33287
Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54
removed unnecessary tag
Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54
Source Link

Too slow for cycle in Pygame

I'm using Pygame for Python 2 and I have the following code:

self.radius = 100 self.light = pygame.Surface((2*self.radius, 2*self.radius), pygame.SRCALPHA) self.light.fill((0, 0, 0, 0)) pygame.draw.circle(self.light, (255, 255, 255), (self.radius, self.radius), self.radius) alphas = pygame.surfarray.pixels_alpha(self.light) for x, col in enumerate(alphas): for y, pixel in enumerate(col): pixel = 255 - f.distance(pygame.Rect(x, y, 1, 1), pygame.Rect(self.radius, self.radius, 1, 1)) 

Basically this code creates a new surface, makes it transparent so that it doesn't interfere with the background, draws a circle on it, and then makes two nested for cycles, going through all of the pixels. The problem is, these fors are too slow. How can I improve their performance?