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

发布于 2025-01-17 07:47:53 字数 4399 浏览 0 评论 0原文

我创建了一个处理 pygame 中的粒子的类。在移动和显示粒子的发射函数中,我还将其设置为逐渐使每个粒子变暗。它使每个粒子变暗,但程度非常小。这是因为每个粒子的颜色(与粒子的其他数据一起存储在列表中)以某种方式仅发生少量更改。例如,它的默认值为(255, 255, 255),单次变暗后,变为(245, 245, 245)左右。它应该继续变暗,但它只是停止了。当我更改粒子的其他值(例如其位置)时,它们不会重置,并且每个粒子都有自己的值列表,所以我不知道为什么它会重置。以下是部分代码(在类内部):

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 = []

下一部分是在玩家类内部,其中使用粒子对象:

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

上面的代码在玩家类内部,在类内部的更新方法中调用。此更新方法在 while 1 循环(无限游戏循环)中调用。

粒子[3](粒子颜色数据),从来不是由我手动(或故意)重置的。

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

块引用 (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)

感谢所有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2025-01-24 07:47:53

对于这个问题,就像许多其他与 delta_time 相关的事情一样。如果某些东西没有按预期工作,只需增加乘以 delta_time 的值,因为当乘以 delta_time 时,它​​会使随时间的变化在不同的帧速率下保持一致,但在相同的帧速率下,它的变化始终比以前小。帧速率与 delta_time。

在这种情况下,我所要做的就是将值从 -10 降低到 -800。
所以:proxy_color =(粒子[3][0]-10*delta_time,粒子[3][1]-10*delta_time,粒子[3][2]-10*delta_time) -> ; proxy_color = (粒子[3][0]-800*delta_time,粒子[3][1]-800*delta_time,粒子[3][2]-800*delta_time)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文