Skip to content

Commit

Permalink
Make screen a permanent object
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmauve committed May 15, 2021
1 parent d02e3ca commit c83f0b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pgzero/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
from .keyboard import keyboard
from .animation import animate
from .rect import Rect, ZRect

from .loaders import images, sounds

from .constants import mouse, keys, keymods

from .game import exit

# The actual screen will be installed here
from .screen import screen_instance as screen


__all__ = [
'screen', # graphics output
'Actor', 'images', # graphics
'sounds', 'music', 'tone', # sound
'clock', 'animate', # timing
Expand Down
7 changes: 2 additions & 5 deletions pgzero/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pgzero.keyboard
import pgzero.screen
import pgzero.loaders
import pgzero.screen

from . import constants

Expand Down Expand Up @@ -69,11 +70,7 @@ def reinit_screen(self):
h = getattr(mod, 'HEIGHT', 600)
if w != self.width or h != self.height:
self.screen = pygame.display.set_mode((w, h), DISPLAY_FLAGS)
if hasattr(self.mod, 'screen'):
self.mod.screen.surface = self.screen
else:
screen = pgzero.screen.Screen(self.screen)
self.mod.screen = builtins.screen = screen
pgzero.screen.screen_instance._set_surface(self.screen)
self.width = w
self.height = h

Expand Down
6 changes: 4 additions & 2 deletions pgzero/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ def textbox(self, *args, **kwargs):

class Screen:
"""Interface to the screen."""

def __init__(self, surface):
def _set_surface(self, surface):
self.surface = surface
self.width, self.height = surface.get_size()

Expand Down Expand Up @@ -148,3 +147,6 @@ def draw(self):

def __repr__(self):
return "<Screen width={} height={}>".format(self.width, self.height)


screen_instance = Screen()

0 comments on commit c83f0b3

Please sign in to comment.