Pygame - particle effects

I am making a 2D game using Pygame.
I want to add particle effects to a game I am working on. I want to do things like smoke from smoke, fire, blood, etc. I'm curious if there is an easy way to do this? I don't even know where to start. I just need a basic one that I could expand on.
Pls help.

+3


source to share


2 answers


You might just want to create a class made up of rectangles that go up and randomly move to the right or left each time it is updated for smoke. Then make a ton of them whenever you want. I will try to make the example code below, but I cannot guarantee that it will work. You can create similar classes for other particle effects.

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)

      

Another option is to make the class just a tuple, for example:

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

      



Or, to avoid classes entirely:

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

      

Pick your preference and do something similar for other particle effects.

+3


source


Check out the PyIgnition particle effects library



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

0


source







All Articles