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 for
s are too slow. How can I improve their performance?
for
loops to change all of the pixels' alpha, depending on the distance from the source, so that the light power will become lower and lower until it won't exist anymore when very distant from the center. \$\endgroup\$