1

I'm working on a school project with pygame and the main objectives is to make a lot of objects appear and move randomly. So I wanted to make some particles spawn and move randomly and I came up with this.

import pygame, random

# -- Variables --

pygame.init()
clock = pygame.time.Clock()
fps_limit = 60.0

resx = 1600 
resy = 900
screen = pygame.display.set_mode([resx,resy])

# -- Classes --

class Particule():
    def __init__ (self,color,radius):
        pygame.sprite.Sprite.__init__(self)
        self.color = color
        self.radius = radius
        self.position_x = resx/2
        self.position_y = resy/2

    def update(self):
        pygame.draw.circle(screen, self.color, (self.position_x,self.position_y),self.radius)

    def random_movement(self):
        self.position_x += random.randint(0,10)
        self.position_y -= random.randint(0,5)

    def life(self):
        self.radius -= 1

# -- Objects --

particule1 = Particule((255,0,0),100)


# -- Game loop --

running = True
while running:
    clock.tick(fps_limit)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0,0,0))

    particule1.update()
    particule1.random_movement()
    particule1.life()

    pygame.display.set_caption("Baguette")

    pygame.display.flip()   
pygame.quit()

I tried doing some while/if/for loops but only one object appears and then none

2
  • Is the issue solved? Commented Mar 21, 2021 at 9:48
  • Yes everything is in order Commented Mar 23, 2021 at 8:55

1 Answer 1

0

Crate a list for the particles:

particles = []

Use pygame.time.get_ticks() to get number of milliseconds since pygame.init() was called. Define the time at which the 1st particle must appear. If the time exceeds create a particle and set the time for the next particle to be created:

next_particle_time = 0

running = True
while running:
    # [...]

    current_time = pygame.time.get_ticks()
    if current_time > next_particle_time:
        particles.append(Particule((255,0,0), 100))         
        next_particle_time = current_time + 200    # 0.2 second interval

Draw and move all the particle in a loop:

running = True
while running:
    # [...]

    for p in particles[:]:
        p.update()
        p.random_movement()
        p.life()
        if p.radius < 1:
            particles.remove(p)

Complete example

import pygame, random

# -- Variables --
pygame.init()
clock = pygame.time.Clock()
fps_limit = 60.0
resx = 1600 
resy = 900
screen = pygame.display.set_mode([resx,resy])
pygame.display.set_caption("Baguette")

# -- Classes --
class Particule():
    def __init__ (self,color,radius):
        pygame.sprite.Sprite.__init__(self)
        self.color = color
        self.radius = radius
        self.position_x = random.randint(radius, 1600-radius)
        self.position_y = random.randint(radius, 900-radius)

    def update(self):
        pygame.draw.circle(screen, self.color, (self.position_x,self.position_y),self.radius)

    def random_movement(self):
        self.position_x += random.randint(-10,10)
        self.position_y -= random.randint(-5,5)

    def life(self):
        self.radius -= 1

# -- Objects --
particles = []
next_particle_time = 0

# -- Game loop --
running = True
while running:
    clock.tick(fps_limit)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    current_time = pygame.time.get_ticks()
    if current_time > next_particle_time:
        particles.append(Particule((255,0,0), 100))         
        next_particle_time = current_time + 200 # 0.2 second interval

    screen.fill((0,0,0))

    for p in particles[:]:
        p.update()
        p.random_movement()
        p.life()
        if p.radius < 1:
            particles.remove(p)
    print(len(particles))

    pygame.display.flip()   

pygame.quit()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.