Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I limit my sprite update rate?
    primarykey
    data
    text
    <p>I'm creating a small RPG worl with pygame. So far so good, I load my maps with Tiled TMX entity manager, and everything is great. Except one thing. When my main character moves, the sprite animation is going to damn fast and I don't know what to do to avoid that. Here is my update() code :</p> <pre><code>def update(self): self.index += 1 if self.index &gt;= 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] </code></pre> <p>And my keyboard event management :</p> <pre><code>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 </code></pre> <p>I'm using a clock to force 60fps. Is there a way I can tell pygame for example : update the sprite but only if it has been more than 1 second since the last update ?<br> Thanks</p> <p>EDIT : Solution :</p> <pre><code>def update(self): self.timer += 1 if self.timer &gt;= self.UPDATE_TIME: self.index += 1 self.timer = 0 if self.index &gt;= 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] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload