Skip to content

Commit

Permalink
Updated pause functionality, in accordance with PR comments
Browse files Browse the repository at this point in the history
Moving the logic for need_redraw to more relevant places
  • Loading branch information
lucas-james committed Sep 19, 2018
1 parent 3467b47 commit 9afb94d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion examples/asteroids/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import operator

from pygame.math import Vector2
from pgzero.builtins import pause, is_paused

from space import create_star_scape
from actors import Player, Asteroid
Expand Down
23 changes: 15 additions & 8 deletions pgzero/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def reinit_screen(self):
w = getattr(mod, 'WIDTH', 800)
h = getattr(mod, 'HEIGHT', 600)
if w != self.width or h != self.height:
self.need_redraw = True
self.screen = pygame.display.set_mode((w, h), DISPLAY_FLAGS)
if hasattr(self.mod, 'screen'):
self.mod.screen.surface = self.screen
Expand Down Expand Up @@ -199,9 +200,13 @@ def get_update_func(self):
except AttributeError:
return None
else:
if update.__code__.co_argcount == 0:
return lambda dt: update()
return update
def update_wrapper(dt):
if update.__code__.co_argcount == 0:
update()
else:
update(dt)
self.need_redraw = True
return update_wrapper

def get_draw_func(self):
"""Get a draw function.
Expand Down Expand Up @@ -250,7 +255,7 @@ def mainloop(self):

pgzclock = pgzero.clock.clock

self.need_redraw = not paused
self.need_redraw = True
while True:
# TODO: Use asyncio.sleep() for frame delay if accurate enough
yield from asyncio.sleep(0)
Expand All @@ -268,13 +273,15 @@ def mainloop(self):
self.keyboard._release(event.key)
self.dispatch_event(event)


if update and not paused:
if not paused:
pgzclock.tick(dt)
update(dt)

if update:
update(dt)


screen_change = self.reinit_screen()
if screen_change or update or pgzclock.fired or self.need_redraw:
if pgzclock.fired or self.need_redraw:
draw()
pygame.display.flip()
self.need_redraw = False

0 comments on commit 9afb94d

Please sign in to comment.