不要在 Pygame 中显示第一帧(精灵)
Don't display first frame in Pygame (sprites)
我正在为 pygame
制作的游戏制作热门动画,使用
sprites
。我做到了并且它有效,但我的问题是,动画结束后,第一帧停留在屏幕上。
这是我的动画代码:
class HitAnimation(pygame.sprite.Sprite):
def __init__(self,pos_x,pos_y):
super().__init__()
self.sprites = []
self.is_animating = False
self.sprites.append(pygame.image.load("hit0.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit1.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit2.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit3.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit4.gif").convert_alpha())
self.current_sprite = 0
self.image = self.sprites[self.current_sprite]
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x,pos_y]
def animate(self):
self.is_animating = True
def update(self):
if self.is_animating == True:
self.current_sprite += 0.3
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.is_animating = False
self.image = self.sprites[int(self.current_sprite)]
moving_hitsprites = pygame.sprite.Group()
在主要游戏功能中比后面:
while True:
#basic stuff, the game itself
#than here I am updating and drawing the hit animation
moving_hitsprites.update()
moving_hitsprites.draw(WIN)
if event.type==PLAYER1_HIT:
location = HitAnimation(playerrect.x-playerrect.width//2,playerrect.y-playerrect.height//2)
moving_hitsprites.add(location)
location.animate() # here I am starting the animation of the hit effect
动画出现了,但是在遍历所有帧之后,它会一直显示第一帧。我该如何解决这个问题?
你必须kill
动画结束时的动画精灵:
class HitAnimation(pygame.sprite.Sprite):
# [...]
def update(self):
if self.is_animating == True:
self.current_sprite += 0.3
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.is_animating = False
self.kill() # <---
self.image = self.sprites[int(self.current_sprite)]
kill
从所有 Groups 中删除 Sprite,因此 Sprite将在动画结束时从 moving_hitsprites
中删除。
实际上,您根本不需要 is_animating
属性:
class HitAnimation(pygame.sprite.Sprite):
# [...]
def update(self):
self.current_sprite += 0.3
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.kill()
self.image = self.sprites[int(self.current_sprite)]
我正在为 pygame
制作的游戏制作热门动画,使用
sprites
。我做到了并且它有效,但我的问题是,动画结束后,第一帧停留在屏幕上。
这是我的动画代码:
class HitAnimation(pygame.sprite.Sprite):
def __init__(self,pos_x,pos_y):
super().__init__()
self.sprites = []
self.is_animating = False
self.sprites.append(pygame.image.load("hit0.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit1.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit2.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit3.gif").convert_alpha())
self.sprites.append(pygame.image.load("hit4.gif").convert_alpha())
self.current_sprite = 0
self.image = self.sprites[self.current_sprite]
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x,pos_y]
def animate(self):
self.is_animating = True
def update(self):
if self.is_animating == True:
self.current_sprite += 0.3
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.is_animating = False
self.image = self.sprites[int(self.current_sprite)]
moving_hitsprites = pygame.sprite.Group()
在主要游戏功能中比后面:
while True:
#basic stuff, the game itself
#than here I am updating and drawing the hit animation
moving_hitsprites.update()
moving_hitsprites.draw(WIN)
if event.type==PLAYER1_HIT:
location = HitAnimation(playerrect.x-playerrect.width//2,playerrect.y-playerrect.height//2)
moving_hitsprites.add(location)
location.animate() # here I am starting the animation of the hit effect
动画出现了,但是在遍历所有帧之后,它会一直显示第一帧。我该如何解决这个问题?
你必须kill
动画结束时的动画精灵:
class HitAnimation(pygame.sprite.Sprite):
# [...]
def update(self):
if self.is_animating == True:
self.current_sprite += 0.3
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.is_animating = False
self.kill() # <---
self.image = self.sprites[int(self.current_sprite)]
kill
从所有 Groups 中删除 Sprite,因此 Sprite将在动画结束时从 moving_hitsprites
中删除。
实际上,您根本不需要 is_animating
属性:
class HitAnimation(pygame.sprite.Sprite):
# [...]
def update(self):
self.current_sprite += 0.3
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.kill()
self.image = self.sprites[int(self.current_sprite)]