I made some changes to the particle script I showed some days ago (here). The result is a little more complicated and maybe nicer, but it’s made just to deepen a little bit our knowledge of particles with pygame.
I made a class Particle to use this code also in other applications easily, maybe.
The code of the particles generator
# pygame.math module # https://www.pygame.org/docs/ref/math.html # # Pygame swap text with another text # https://stackoverflow.com/questions/60944070/pygame-swap-text-with-another-text/60953697#60953697 # # GitHub - PyGameExamplesAndAnswers - Draw 2D - Particles # https://github.com/Rabbid76/PyGameExamplesAndAnswers/blob/master/documentation/pygame/pygame_2D.md import pygame import random import sys class Particle: def __init__(self): pygame.init() self.screen = pygame.display.set_mode((300, 300)) self.clock = pygame.time.Clock() self.snow = pygame.image.load("snow.png").convert() self.cloud = pygame.image.load("cloud.png") self.window2 = pygame.image.load("window2.png") self.particles = [] def mainloop(self): run = True while run: self.clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # it adds particles until they are removed # if the size - [2] - is smaller than 0 self.screen.blit(self.snow, (0,0)) self.particles_generator() self.screen.blit(self.cloud, (-110,-100)) self.screen.blit(self.cloud, (-50, 250)) self.screen.blit(self.window2, (0, 0)) pygame.display.flip() pygame.quit() exit() # list of particles # the surface with the snow as a background def particles_generator(self): "This makes the circles move rfrom random start position at the top until bottome and the disappear" # Every particle starts at a random horizontal position at the top self.particles.append([ [random_pos(), 50], # [0][0] coordinates for circle posit. [ random.randint(0, 20) / 10 - 1, # moves left or right 2], # goes down same speed random.randint(4, 6)]) # [0][2] size or radius # Every particle moves... if particles[2] (the radius) is >= than 0 it is removed for particle in self.particles[:]: # the starting point changes going right or left particle[0][0] += particle[1][0] # the x movement random particle[0][1] += particle[1][1] # the y movement fixed 2 # but increases down here particle[2] -= 0.005 # the snowflake shriks particle[1][1] += 0.01 # * random.randint(-3, 6) # increase down speed a bit if particle[2] <= 0: self.particles.remove(particle) # draws a circle on the screen white, at x y corrds and with a ray of particle[2] for particle in self.particles: pos = particle[0][0] speed = particle[0][1] radius = particle[2] # circle: surface, color, pos, radius pygame.draw.circle( self.screen, (255, 244, 255), # color ( round(pos), # Pos x, y round(speed)), # direction / speed round(radius)) # radius def random_pos(): " to make a flame like particles [150, 20] # flame, so that all circles with start at the same point" return random.randint(0, 300) # return 150 fx = Particle() fx.mainloop()
Single images
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame