Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Issue #119) Added pause functionality to builtins #123

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/asteroids/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ def on_key_down(key):
game.player.turn += 1
if key == keys.RIGHT:
game.player.turn -= 1
if key == keys.SPACE and not game.player.frozen:
if key == keys.SPACE and not game.player.frozen and not is_paused():
sounds.fire.play()
game.bullets.append(game.player.fire())
if key == keys.RETURN:
pause()
elif game.stage is GameStage.game_over:
if key == keys.BACKSPACE:
game.initials = game.initials[:-1]
Expand Down
2 changes: 1 addition & 1 deletion pgzero/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

from .constants import mouse, keys, keymods

from .game import exit
from .game import exit, pause, is_paused
28 changes: 21 additions & 7 deletions pgzero/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@

screen = None
DISPLAY_FLAGS = 0
paused = False

def pause():
global paused
paused = not paused

def is_paused():
return paused

def exit():
"""Wait for up to a second for all sounds to play out
Expand Down Expand Up @@ -70,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 @@ -192,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 @@ -261,13 +273,15 @@ def mainloop(self):
self.keyboard._release(event.key)
self.dispatch_event(event)

pgzclock.tick(dt)
if not paused:
pgzclock.tick(dt)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong - pgzclock needs to run if not paused, even if update is not defined.


if update:
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