pygame - 粒子特效
我正在用Pygame制作一个二维游戏。
我想在我正在开发的游戏中添加粒子效果,比如生成烟雾、火焰、血液等等。我很好奇有没有简单的方法可以做到这一点?我甚至不知道从哪里开始。
我只需要一个基础的例子,方便我在此基础上进行扩展。
请帮帮我。
2 个回答
1
查看这个库,它可以用来制作粒子特效,链接在这里:PyIgnition
3
你可以考虑创建一个由矩形组成的类,每次更新时这些矩形会向上移动,并随机向左或向右移动。然后你可以在需要的时候生成很多这样的矩形。我会尝试在下面给出一个示例代码,但不能保证它一定能用。你也可以为其他粒子效果创建类似的类。
class classsmoke(pygame.Rect):
'classsmoke(location)'
def __init__(self, location):
self.width=1
self.height=1
self.center=location
def update(self):
self.centery-=3#You might want to increase or decrease this
self.centerx+=random.randint(-2, 2)#You might want to raise or lower this as well
#use this to create smoke
smoke=[]
for i in range(20):
smoke.append(classsmoke(insert location here))
#put this somewhere within your game loop
for i in smoke:
i.update()
if i.centery<0:
smoke.remove(i)
else:
pygame.draw.rect(screen, GREY, i)
另一种选择是把这个类简单地做成一个元组,像这样:
class classsmoke():
'classsmoke(location)'
def __init__(self, location):
self.center=location
def update(self):
self.center[1]-=3
self.center[0]+=random.randint(-2, 2)
#to create smoke
smoke=[]
for i in range(20):
smoke.append(classsmoke(insert location here))
#put inside game loop
for i in smoke:
i.update()
if i.centery<0:
smoke.remove(i)
else:
pygame.draw.rect(screen, GREY, (i.center[0], i.center[1], 1, 1))
或者,完全不使用类:
#to create smoke:
smoke=[]
for i in range(20):
smoke.append(insert location here)
#put within your game loop
for i in smoke:
i[1]-=3
i[0]+=random.randint(-2, 2)
if i[1]<0:
smoke.remove(i)
else:
pygame.draw.rect(screen, GREY, (i[0], i[1], 1, 1))
选择你喜欢的方式,然后为其他粒子效果做类似的处理。