Greetings!
I’m creating a simple snake game. I want to expand my classes in different modules e.i. have menu class in a separate script from my main game loop. In other words, I want my imported script to take the pygame init which was called earlier in main script.
Here is a quick example using pseudo code of my problem:
one.py
def version():
print pygame.version
In main.py i have imported pygame and did pygame.init(). From here, I also want to use the def version() from one.py
main.py
import pygame
import one
pygame.init()
one.version()
However, it gives me the no pygame defined error. I know the reason why it give me an error is because when the one.py is called from within the main.py, it doesnt retain the declarations from main.py.
What I want to know is a method to doing the mentioned above that will actually work.
Thank you!
The imports of the module that imports module X don’t leak through to X’s namespace (which is a good thing – it would either require dynamic scoping or C/C++-style
#include, both are almost never useful and often even harmful). It’s a completely seperate namespace on its own. If you want to use something (e.g.pygame) in a module (e.g.one), import it there.