0
我有一個Python/Pygame模塊,其結構如下。這被保存爲一個名爲text_factory.py的python文件。導入的Python模塊沒有初始化內部導入
import pygame, sys, os
from pygame.locals import KEYDOWN, K_ESCAPE, RLEACCEL
class Text_Factory(pygame.sprite.Sprite):
def __init__(self, posx = None, posy = None, font = None, fontsize = 100, colour = (255, 50, 64), text1 = "Level ", text2 = " Complete!", level = 1, frames = 30, pause = 1500, slowdown = 30, fade = False, rotate = None):
pygame.sprite.Sprite.__init__(self)
self.screen = pygame.display.get_surface()
if self.screen == None:
if sys.platform == 'win32' or sys.platform == 'win64':
#os.environ['SDL_VIDEO_CENTERED'] = '0'# center of screen
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (10, 30)#top left corner
self.screen =pygame.display.set_mode((800, 600), 1, 32) # demo screen size
back = pygame.image.load("D:\\IMAGES\\space_11.jpg").convert()
self.screen.blit(back, (0, 0))
# the subsurface is a rect
self.subsurface = None #self.screen.subsurface(0, 0, self.screen.get_width(), self.screen.get_height()).convert_alpha()
self.background = (0, 0, 0)
self.alpha = 255
if posx == None:
self.posx = self.screen.get_width()/2
else:
self.posx = posx
if posy == None:
self.posy = self.screen.get_height()/2
else:
self.posy = posy
self.center = (self.posx, self.posy)
等等,等等
當我將其導入從另一個Python文件像下面
from text_factory import *
tf = Text_Factory()
tf.default() # this is a function in the class to demnstrate the code
the program stops running with the pygame.error: font not initialized message.
顯然Pygame的模塊沒有加載和初始化。當代碼被調用並運行上面所示的方式時,pygame,sys,os和其他import是否應該運行?當我導入一個python模塊text_factory時,它不應該運行內部導入嗎?如果不是如何做到的?