单独更改像素确实很慢;它仅在您使用例如numpy操纵图像数据的情况下才有效,因为那时大多数工作将在优化的编译C代码中完成,而不是在python运行时中完成。
一种简单的方法是仅使用另一个Surface来使用不同的渲染模式(例如)进行此操作BLEND_RGBA_SUB。
这是一个最小的示例:
import pygame
pygame.init()
screen=pygame.display.set_mode((640, 480))
light=pygame.image.load('circle.png')
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT: break
else:
screen.fill(pygame.color.Color('Red'))
for x in xrange(0, 640, 20):
pygame.draw.line(screen, pygame.color.Color('Green'), (x, 0), (x, 480), 3)
filter = pygame.surface.Surface((640, 480))
filter.fill(pygame.color.Color('Grey'))
filter.blit(light, map(lambda x: x-50, pygame.mouse.get_pos()))
screen.blit(filter, (0, 0), special_flags=pygame.BLEND_RGBA_SUB)
pygame.display.flip()
continue
break
Run Code Online (Sandbox Code Playgroud)
circle.png:

屏幕截图:
