列表中的项目乘以 delta_time 未在屏幕上显示更改

Item in list multiplied by delta_time not showing change on screen

我创建了一个 class 来处理 pygame 中的粒子。在移动和显示粒子的发射函数中,我还将它设置为逐渐使每个粒子变暗。它使每个粒子变暗但非常小。这是因为与粒子的其他数据一起存储在列表中的每个粒子的颜色以某种方式仅发生了少量变化。比如它的默认值是(255, 255, 255),单次变暗后就变成了(245, 245, 245)左右。它应该继续变暗,但它只是停止了。当我改变粒子的其他值时,比如它的位置,它们不会重置,每个粒子都有自己的值列表,所以我不知道为什么要重置。这是代码的一部分(在 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 = []

下一部分,在播放器内部 class,使用粒子对象的地方:

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

上述代码在播放器 class 内,在 class 内的更新方法中调用。此更新方法在 while 1 循环(无限游戏循环)中调用。

Particle[3](粒子颜色数据),从不手动(或有意),由我重置。

打印粒子[3](列表中存储颜色的部分)时的示例输出:

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)

感谢所有帮助。

对于这个问题,以及与 delta_time 相关的许多其他事情。如果某些东西没有按预期工作,只需提高乘以 delta_time 的值,因为当乘以 delta_time 时,它会使随时间的变化在不同的帧速率下保持一致,但它是在 delta_time.

的相同帧速率下,变化始终比以前更小

在这种情况下,我所要做的就是将值从 -10 降低到 -800。 所以: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)