I created a class that handles particles in pygame. In the emit function which moves and displays the particles, I also have it set to gradually darken each particle. It darkens each particle but very minimally. This is because somehow the color of each particle, stored in a list with the particle's other data, is only changed by a small amount. For example, its default value is (255, 255, 255), after a single darken, it becomes around (245, 245, 245). It should continue to darken, but it just stops instead. When I change the other values of the particle, like its position, they don't reset, and each particle has its own list of values, so I have no idea why it is resetting. Here is part of the code(inside a class):
def emit(self, screen, delta_time):
if self.particles: # If there is anything in the particles list:
self.null_particles() # Delete all faded particles from list
for particle in self.particles: # Loop through all particles in list
# Shift particle spawn location
particle[0][0] = random.randint(-20, 20)
particle[0][1] = random.randint(-10, 10)
# If the direction isn't set, make it random
if particle[2] == [0, 0]:
particle[2] = [random.randint(-3, 3), random.randint(-3, 3)]
elif particle[2][0] == 0:
particle[2][0] = random.randint(-3, 3)
elif particle[2][1] == 0:
particle[2][1] = random.randint(-3, 3)
# Move particle based off of direction parameters
particle[0][0] = particle[2][0]
particle[0][1] = particle[2][1]
# Make particle smaller (fade out effect)
particle[1] -= 50*delta_time
# Make color gradually get darker
proxy_color = (particle[3][0]-10*delta_time, particle[3][1]-10*delta_time, particle[3][2]-10*delta_time)
if proxy_color[0] < 0 or proxy_color[1] < 0 or proxy_color[2] < 0:
pass
else:
particle[3] = proxy_color
# Display particle
pygame.draw.circle(screen, particle[3], particle[0], int(particle[1]))
def add_particles(self, pos_x, pos_y, direction_x=0, direction_y=0, color=(255, 255, 255)):
pos_x = pos_x
pos_y = pos_y
radius = 7
particle_circle = [[pos_x, pos_y], radius, [direction_x, direction_y], color]
self.particles.append(particle_circle)
def null_particles(self):
particle_copy = [particle for particle in self.particles if particle[1] > 0]
self.particles = particle_copy
def delete_particles(self):
self.particles = []
Next part, inside a player class, where the particle object is being used:
def particle_display(self, screen, delta_time):
if pygame.time.get_ticks() - self.time_jumping <= 200: # While player jumped within less than .2 seconds ago:
dust_particle.add_particles(self.rect.midbottom[0], self.ground_level, 0, -10) # Add more particles
dust_particle.add_particles(self.rect.midbottom[0], self.ground_level, 0, -10) # Add more particles
dust_particle.add_particles(self.rect.midbottom[0], self.ground_level, 0, -10) # Add more particles
dust_particle.emit(screen, delta_time) # Display said particles
else:
# Delay added to give particles time to disperse, so particles dont get deleted when buttons get deleted
if pygame.time.get_ticks() - self.jump_ended >= 200:
# Delete all particles after .2 sec of jump, or before jumping happened
dust_particle.delete_particles()
The above code, inside a player class, is called in an update method inside the class. This update method is called in a while 1 loop(infinite game loop).
Particle[3] (particles color data), is never manually (or purposefully), reset by me.
Sample output when particle[3] (the part of the list that stores the color) is printed:
Blockquote (253.68000000000006, 253.68000000000006, 253.68000000000006) (253.68000000000006, 253.68000000000006, 253.68000000000006) (253.68000000000006, 253.68000000000006, 253.68000000000006) (253.85000000000005, 253.85000000000005, 253.85000000000005) (253.85000000000005, 253.85000000000005, 253.85000000000005) (253.85000000000005, 253.85000000000005, 253.85000000000005) (254.02000000000004, 254.02000000000004, 254.02000000000004) (254.02000000000004, 254.02000000000004, 254.02000000000004) (254.02000000000004, 254.02000000000004, 254.02000000000004) (254.19000000000003, 254.19000000000003, 254.19000000000003) (254.19000000000003, 254.19000000000003, 254.19000000000003) (254.19000000000003, 254.19000000000003, 254.19000000000003) (254.36, 254.36, 254.36) (254.36, 254.36, 254.36) (254.36, 254.36, 254.36) (254.52, 254.52, 254.52) (254.52, 254.52, 254.52) (254.52, 254.52, 254.52) (254.68, 254.68, 254.68) (254.68, 254.68, 254.68) (254.68, 254.68, 254.68) (254.84, 254.84, 254.84) (254.84, 254.84, 254.84) (254.84, 254.84, 254.84) (255, 255, 255) (255, 255, 255) (255, 255, 255)
All help is appreciated.
CodePudding user response:
For this issue, as with many other things related to delta_time. If something doesn't work as expected, simply raise the value you are multiplying by delta_time, as when you multiply by delta_time, it makes the change over time consistent at different frame rates, but it is a consistently smaller change than before on the same frame rate with delta_time.
In this case, all I had to do was lower the value from -10 to -800.
So : proxy_color = (particle[3][0]-10*delta_time, particle[3][1]-10*delta_time, particle[3][2]-10*delta_time)
-> proxy_color = (particle[3][0]-800*delta_time, particle[3][1]-800*delta_time, particle[3][2]-800*delta_time)