Create fire particles for the gun

Viewed 45

I'm working on a space shooting game and I want my gun to emit a cluster of particles every time it fires. I tried creating a class for the model of one particle and another class to apply this to 50 particles. Everything was pretty good since there was no syntax error, yet the code did not work. This is my code:

import pygame, sys, random

CLOCK = pygame.time.Clock()
pygame.init()
Screen = pygame.display.set_mode((500,500))

class FireParticles_system:
    def __init__(self,pos):
        self.particles = []
        self.pos_x, self.pos_y = pos[0], pos[1]
        self.radius = 5
    def emit (self,Screen):
        for particle in self.particles:
            particle[0][0] += particle[2][0]
            particle[0][1] += particle[2][1]
            if random.randint(0,100)< 60:
                particle[1] -= 0.1
            pygame.draw.circle(Screen,(255,255,255),particle[0],int(particle[1]))

    def add_particles(self):
        vx, vy = random.randint(10,0)*.1, random.randint(-2,2)
        self.particles.append[[self.pos_x,self.pos_y],self.radius,[vx,vy]]

class Fire:
    def __init__(self,pos):
        self.particles = []
        for i in range(50):
            self.particles.append(FireParticles_system(pos))
    def update(self,Screen):
        for i in self.particles:
            i.emit(Screen)
            self.particles = [particle for particle in self.particles if particle.radius > 0]

fire = []



while True:
    Screen.fill((0,0,0))
    mouse_x, mouse_y = pygame.mouse.get_pos()

    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                f = Fire([mouse_x, mouse_y])       
                fire.append(f)  


    for i in range(len(fire)):
        if len(fire[i].particles) > 0:
            fire[i].update(Screen)   
    
    pygame.display.update()
    CLOCK.tick(60)

I could not spot any problems. Pls, help me T.T

1 Answers

FireParticles_system has a list of particles. However Fire needs just 1 particle system:

import pygame, sys, random

CLOCK = pygame.time.Clock()
pygame.init()
Screen = pygame.display.set_mode((500,500))

class FireParticles_system:
    def __init__(self,pos):
        self.particles = []
        self.pos_x, self.pos_y = pos[0], pos[1]
        self.radius = 5
    def emit (self,Screen):
        for particle in self.particles:
            particle[0][0] += particle[2][0]
            particle[0][1] += particle[2][1]
            particle[1] -= 0.1
            pygame.draw.circle(Screen,(255,255,255),particle[0],int(particle[1]))

    def add_particles(self):
        vx, vy = random.randint(0, 10), random.randint(-2,2)
        self.particles.append([[self.pos_x,self.pos_y],self.radius,[vx,vy]])

class Fire:
    def __init__(self,pos):
        self.particles_system = FireParticles_system(pos)
    def update(self,Screen):
        self.particles_system.add_particles()
        self.particles_system.emit(Screen)

fire = []

while True:
    Screen.fill((0,0,0))
    mouse_x, mouse_y = pygame.mouse.get_pos()

    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                f = Fire([mouse_x, mouse_y])       
                fire.append(f)  


    for i in range(len(fire)):
        fire[i].update(Screen)   
    
    pygame.display.update()
    CLOCK.tick(60)
Related