pygame - 粒子效应

时间:2013-02-12 03:39:10

标签: python 2d pygame particles

我正在使用Pygame进行2-d游戏 我想在我正在研究的游戏中添加粒子效果。我想做像产生烟雾,火,血等的东西。我很好奇有没有一种简单的方法可以做到这一点?我真的不知道从哪里开始。
我只需要一个可以扩展的基础案例。
请帮助。

2 个答案:

答案 0 :(得分:3)

你可能想要制作一个由每个为烟雾更新时随机向右或向左移动的rects组成的类。然后随时随地制作一大堆。我将尝试在下面制作一个示例代码,但我不能保证它会起作用。您可以为其他粒子效果制作类似的类。

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))

选择您的偏好,为其他粒子效果做类似的事情。

答案 1 :(得分:0)

检查库中的粒子效果PyIgnition

http://www.pygame.org/shots/1527.jpg