Skip to content

Commit

Permalink
Added pause functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-james committed Sep 19, 2018
1 parent f0cddc7 commit 3467b47
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples/asteroids/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 Expand Up @@ -139,9 +140,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
13 changes: 10 additions & 3 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 @@ -243,7 +250,7 @@ def mainloop(self):

pgzclock = pgzero.clock.clock

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

pgzclock.tick(dt)

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

screen_change = self.reinit_screen()
Expand Down

0 comments on commit 3467b47

Please sign in to comment.