0

私は pygame で小さな RPG ワールドを作成しています。これまでのところ、Tiled TMX エンティティ マネージャーを使用してマップを読み込んでいますが、すべてがうまくいっています。1つのことを除いて。メイン キャラクターが動くと、スプライト アニメーションが非常に高速になり、それを回避する方法がわかりません。ここに私の 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]
4

1 に答える 1