我正在寻找的是平滑的精灵动画,在它们之间有过渡动画。而不是主要角色向左或向右闪烁然后进行动作动画,而是执行另一种转弯动画,然后继续。
所以我想让pygame KEYDOWN事件完成一项任务,然后让动画过去(我认为它是270〜毫秒,即60 fps时为16帧),然后我想让另一个动画重复播放只要按住按钮,而不必一遍又一遍地重复转换,这就是我目前所拥有的。 相关循环在这里:
while Playing:
dt = Clock.tick()/1000
keys=pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN:
if keys[K_RIGHT]:
walkerspritesheet = WalkerStandToSlow_Right_List
walkerxlocation += walkermovespeed
if Walker_Counter == 16:
walkerspritesheet = WalkerSlow_Right_List
if keys[K_LEFT]:
walkerspritesheet = WalkerStandToSlow_Left_List
walkerxlocation -= walkermovespeed
if Walker_Counter == 16:
walkerspritesheet = WalkerSlow_Left_List
XXXXX_List变量是Sprite工作表切片的列表。在主while循环的更下方,它使用walkerspritesheet中包含的图像播放Walker的动画。 Walker_Counter是动画在60帧中的哪一帧。
谢谢,让我知道我是否缺少任何必要的信息。
答案 0 :(得分:0)
我尝试创建一个最小的示例。当您按向右或向左箭头键时,子画面将移动。最初,精灵将变为浅色,然后变为深色。释放钥匙后,它将恢复为原始颜色。
import pygame
pygame.init()
width = 320
height = 240
FPS = 30
clock = pygame.time.Clock()
screen = pygame.display.set_mode([width, height])
class Player(pygame.sprite.Sprite):
def __init__(self, starting_position=(0,0)):
pygame.sprite.Sprite.__init__(self)
width, height = 10, 20
self.color= pygame.Color("brown")
self.image = pygame.Surface([width, height])
self.image.fill(self.color)
# Fetch the rectangle object that has the dimensions of the image
self.rect = self.image.get_rect()
#initialise position
self.rect.center = starting_position
self.velx = 0
self.counter = 0
def animate(self):
if self.velx == 0:
self.image.fill(self.color) # reset to original image
else:
self.counter += 1 # increment counter
if self.velx > 0: # moving right
if self.counter < 20:
self.image.fill(pygame.Color("lightgreen"))
else:
self.image.fill(pygame.Color("darkgreen"))
elif self.velx < 0: # moving left
if self.counter < 20:
self.image.fill(pygame.Color("lightblue"))
else:
self.image.fill(pygame.Color("darkblue"))
def stop(self):
self.velx = 0
self.counter = 0
def move_left(self):
self.velx -= 1
def move_right(self):
self.velx += 1
def update(self):
self.animate()
self.rect.x += self.velx
# check bounds
if not 0 < self.rect.x < (width - 20):
self.rect.x -= self.velx # out of bounds, undo movement
def draw(self, screen):
screen.blit(self.image, self.rect.center)
player = Player((50, 100))
done = False
# main loop
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
elif event.key == pygame.K_LEFT:
player.move_left()
elif event.key == pygame.K_RIGHT:
player.move_right()
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
player.stop()
# update sprite(s)
player.update()
#redraw screen
screen.fill(pygame.Color("black")) # use black background
# draw sprite
player.draw(screen)
# update screen
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
如果您有任何疑问,请告诉我。