Python Pygame游戏照明 [英] Python Pygame Game Lighting
问题描述
我们正在制作2D侧滚动游戏,游戏中的一个项目将成为火炬。我们有一个手臂可以旋转的球员,并且我们可以调整手臂的角度。我们正在寻找一个三角形的光束形状,跟随手臂的角度。我们有一些想法,例如在整个屏幕上制作一张Alpha图像,并根据手臂角度分别从每个像素中删除Alpha,但是我们认为这太费力了。任何想法将不胜感激。
谢谢。
We are making a 2D side scrolling game and an item in the game will be a torch. We have a player who's arm can rotate and we can take the arms angle. We are looking at having a triangular beam shape, following the angle of the arm. We have had a few ideas like have an alpha image over the whole screen and individually removing alpha from each pixel based on the arm angle, but we think this will be too intensive. Any ideas would be greatly appreciated. Thanks.
推荐答案
单独更改像素真的很慢;只有在您使用例如 numpy
来操作图像数据,此后大多数工作将在经过优化的经过编译的C代码中完成,而不是在python运行时中完成。
Changing pixels individually is really slow; it only works if you would use e.g. numpy
to manipulate the image data, since then most work will be done in optimized, compiled C code and not in the python runtime.
一种简单的方法是仅使用另一个 Surface
使用不同的渲染模式(例如 BLEND_RGBA_SUB
。
An easy way is to just use another Surface
to do this manipulation using a different render mode, like 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
circle.png:
截屏:
这篇关于Python Pygame游戏照明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!