Skip to content

Commit

Permalink
Assign screen to builtins so it can be used from imported modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmauve committed May 6, 2019
1 parent 7cd71a7 commit b5f84a3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pgzero/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import operator
import time
import asyncio
import builtins

import pygame
import pgzero.clock
Expand All @@ -11,7 +12,6 @@
from . import constants


screen = None
DISPLAY_FLAGS = 0


Expand Down Expand Up @@ -55,7 +55,6 @@ def reinit_screen(self):
Return True if the dimensions of the screen changed.
"""
global screen
changed = False
mod = self.mod

Expand All @@ -74,8 +73,8 @@ def reinit_screen(self):
if hasattr(self.mod, 'screen'):
self.mod.screen.surface = self.screen
else:
self.mod.screen = pgzero.screen.Screen(self.screen)
screen = self.screen # KILL ME
screen = pgzero.screen.Screen(self.screen)
self.mod.screen = builtins.screen = screen
self.width = w
self.height = h

Expand Down Expand Up @@ -215,7 +214,7 @@ def get_draw_func(self):

def run(self):
"""Invoke the main loop, and then clean up."""
loop = asyncio.get_event_loop()
loop = asyncio.SelectorEventLoop()
try:
loop.run_until_complete(self.run_as_coroutine())
finally:
Expand Down
5 changes: 5 additions & 0 deletions test/game_tests/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

RECT = ZRect(30, 30, 40, 40)

def circle():
screen.draw.circle((50, 50), radius=20, color='cyan')
8 changes: 8 additions & 0 deletions test/game_tests/importing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import helper

assert helper.RECT


def draw():
helper.circle()

Binary file modified test/images/expected_wrapped_gradient_text.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ def test_run(self):
clock.schedule_unique(sys.exit, 0.05)
with self.assertRaises(SystemExit):
load_and_run(str(game_tests / 'utf8.py'))

def test_import(self):
"""Games can import other modules, which can acccess the builtins."""
clock.schedule_unique(sys.exit, 0.05)
with self.assertRaises(SystemExit):
load_and_run(str(game_tests / 'importing.py'))

0 comments on commit b5f84a3

Please sign in to comment.