如何限制精灵更新率?

时间:2013-07-19 17:28:40

标签: python pygame

我正在用pygame创建一个小型RPG游戏。到目前为止,我用Tiled TMX实体管理器加载我的地​​图,一切都很棒。除了一件事。当我的主角移动时,精灵动画会快速播出,我不知道该怎么做才能避免这种情况。这是我的update()代码:

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]

1 个答案:

答案 0 :(得分:1)

保留一个单独的变量来负责计时是否增加self.index

## within __init__
    self.UPDATE_TIME = 60 # or whatever works
    self.timer = 0

def update(self):
    self.timer += 1
    if self.timer = self.UPDATE_TIME:
        self.timer = 0
        self.index += 1

    # unmodified below here
    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]