lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
536 stars 188 forks source link

How to import other scripts that use pgzero? #176

Closed bokov closed 5 years ago

bokov commented 5 years ago

Let's say I have a script with the main game logic, and a separate script to hold long repetitive data objects.

game_data.py

import pgzrun
imglist = [images.floor, images.pillar, images.soil]

mygame.py:

import pgzrun
WIDTH = 800
HEIGHT =600
from game_data import *

def draw():
    screen.blit(imglist[0],(10,10))
    screen.blit(imglist[1],(40,40))
    screen.blit(imglist[2],(70,70))
pgzrun.go()

If I run game_data.py directly it runs fine. But if I run mygame.py, it give the error:

Traceback (most recent call last):
  File "/home/a/Desktop/escape/mygame.py", line 4, in <module>
    from foo import *
  File "/home/a/Desktop/escape/game_data.py", line 2, in <module>
    imglist = [images.floor, images.pillar, images.soil]
NameError: name 'images' is not defined

What is the correct way for one script to import another if both rely on pgzero functionality?

Thanks.

lordmauve commented 5 years ago

Actually, I think this is fixed in master but not yet released.