gpt4 book ai didi

python - Pygame 图像动画循环未正确循环

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:15 25 4
gpt4 key购买 nike

我有一个小的动画循环,可以快速循环浏览图像。我们的目标是使其保持恒定和流畅,但我似乎遇到了一个问题,即图像显示一秒钟并停留在第一帧。

以下代码来自带有动画的整个播放器类:

class Ship(pygame.sprite.Sprite):

change_x = 0

delay = 0
frame = 1

def __init__(self):
pygame.sprite.Sprite.__init__(self)

self.image = PlayerShip0.convert_alpha()

self.rect = self.image.get_rect()

self.rect.x = SCREEN_X / 2 - 100
self.rect.y = SCREEN_Y - 40

def move(self, speed):
self.change_x = speed

def stop(self):
self.change_x = 0

def update(self, screen):
self.rect.x += self.change_x

if self.rect.x < 0:
self.rect.x = 0
elif self.rect.right > SCREEN_X:
self.rect.x -= 3

self.delay += 1
if self.delay >= 50:
if self.frame == 1:
self.image = PlayerShip0.convert_alpha()
self.frame = 2
self.delay = 1
if self.frame == 2:
self.image = PlayerShip1.convert_alpha()
self.frame = 3
self.delay = 1
if self.frame == 3:
self.image = PlayerShip2.convert_alpha()
self.frame = 1
self.delay = 1

print(self.frame)

screen.blit(self.image,self.rect)

最佳答案

而不是

        if self.frame == 1:
self.image = PlayerShip0.convert_alpha()
self.frame = 2
self.delay = 1
if self.frame == 2:
self.image = PlayerShip1.convert_alpha()
self.frame = 3
self.delay = 1
if self.frame == 3:
self.image = PlayerShip2.convert_alpha()
self.frame = 1
self.delay = 1

使用

        if self.frame == 1:
self.image = PlayerShip0.convert_alpha()
self.frame = 2
self.delay = 1
elif self.frame == 2:
self.image = PlayerShip1.convert_alpha()
self.frame = 3
self.delay = 1
elif self.frame == 3:
self.image = PlayerShip2.convert_alpha()
self.frame = 1
self.delay = 1

否则,您可以在第一个 if block 和第二个 if block 中将 self.frame 设置为 2 block 会立即将其设置为 3,使第三个 if block 的条件为真,依此类推。

关于python - Pygame 图像动画循环未正确循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44446493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com