我正在用 pygame 创建一个小型 RPG 世界。到目前为止一切顺利,我用 Tiled TMX 实体管理器加载我的地图,一切都很好。除了一件事。当我的主角移动时,精灵动画会非常快,我不知道该怎么做才能避免这种情况。这是我的更新()代码:
def update(self):
self.index += 1
if self.index >= 2:
self.index = 0
if self.direction == DIRECTIONS['down']:
self.image = self.down_anim[self.index]
elif self.direction == DIRECTIONS['up']:
self.image = self.up_anim[self.index]
elif self.direction == DIRECTIONS['left']:
self.image = self.left_anim[self.index]
elif self.direction == DIRECTIONS['right']:
self.image = self.right_anim[self.index]
还有我的键盘事件管理:
key=pygame.key.get_pressed()
try:
event = pygame.event.wait()
if event.type == KEYDOWN:
if (event.key == K_LEFT):
if angus.direction != DIRECTIONS['left']:
angus.direction = DIRECTIONS['left']
angus.update()
angus.position.x -= 1
elif (event.key == K_RIGHT):
if angus.direction != DIRECTIONS['right']:
angus.direction = DIRECTIONS['right']
angus.update()
angus.position.x += 1
elif (event.key == K_UP):
if angus.direction != DIRECTIONS['up']:
angus.direction = DIRECTIONS['up']
angus.update()
angus.position.y -= 1
elif (event.key == K_DOWN):
if angus.direction != DIRECTIONS['down']:
angus.direction = DIRECTIONS['down']
angus.update()
angus.position.y += 1
我正在使用时钟来强制 60fps。有没有办法告诉 pygame 例如:更新精灵,但前提是它自上次更新以来已超过 1 秒?
谢谢
编辑:解决方案:
def update(self):
self.timer += 1
if self.timer >= self.UPDATE_TIME:
self.index += 1
self.timer = 0
if self.index >= 2:
self.index = 0
if self.direction == DIRECTIONS['down']:
self.image = self.down_anim[self.index]
elif self.direction == DIRECTIONS['up']:
self.image = self.up_anim[self.index]
elif self.direction == DIRECTIONS['left']:
self.image = self.left_anim[self.index]
elif self.direction == DIRECTIONS['right']:
self.image = self.right_anim[self.index]