Note that there are some explanatory texts on larger screens.

plurals
  1. POStop time without freezing the program
    primarykey
    data
    text
    <p>In my pong game, whenever someone scores (the ball goes out to the left or to the right) the ball position is reset to the middle of screen and it waits one second before moving. In that one second I have a little animation going on. </p> <p>My problem is this: if I pause the game in the middle of the animation, <em>even though none of the objects are updated and only the pause text is drawn</em>, time keeps rolling in. And if I wait time enough, the animation just stops right after I unpause the game. Here's what I mean. This is the ball update:</p> <pre><code>def update(self, dt): now = pygame.time.get_ticks() / 1000 # if time elapsed since the ball got out &gt;= BALL_WAIT_TIME if now - self._spawn_time &gt;= BALL_WAIT_TIME: self.rect = self.calcnewpos(dt) self.handle_collision() # spawn animation else: step = 255 / (FPS * BALL_WAIT_TIME) value = int(self._frame * step) rgb = (value, value, value) self._draw_ball(rgb) self._frame += 1 </code></pre> <p>From <a href="http://pygame.org/docs/ref/time.html#pygame.time.get_ticks" rel="nofollow">http://pygame.org/docs/ref/time.html#pygame.time.get_ticks</a>:</p> <blockquote> <p><strong>pygame.time.get_ticks()</strong></p> <p>Return the number of millisconds since pygame.init() was called. Before pygame is initialized this will always be 0.</p> </blockquote> <p>So even though nothing is drawn or updated while the game is paused, pygame.time.get_ticks() will still return the time elapsed since <code>pygame.init</code>. How can I solve this? Sorry if that is a little hard to understand, I'll post the rest of the code if needed.</p>
    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