2013-02-12 83 views
3

我正在使用Pygame製作2D遊戲。
我想在我正在處理的遊戲中添加粒子效果。我想做一些事情,如產卵煙,火,血等。我很好奇,有沒有一種簡單的方法來做到這一點?我甚至不知道從哪裏開始。
我只需要一個基礎案例,我可以擴展..
請幫助。pygame - 粒子效果

回答

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

挑選您的喜好,並做一些事情類似於其他粒子效應。

+0

? – user1758231 2013-02-13 02:18:38

+0

@ user1758231放置在兩個位置的列表中,x和y是這樣的:'[132,45]'帶有x first和y second('[x,y]') – PygameNerd 2013-02-13 19:51:39

0

檢查你所說的插入位置在這裏你是什麼意思圖書館的粒子效果PyIgnition

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