导入的Python模块没有初始化内部导入

时间:2017-03-14 18:53:56

标签: python-2.7 pygame

我有一个Python / Pygame模块,其结构如下所示。它保存为名为text_factory.py的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
  

程序停止运行pygame.error:font not initialized message。

显然,pygame模块未加载并初始化。 Aren的pygame,sys,os和其他导入应该在调用代码时运行并按上面显示的方式运行?当我导入text_factory这是一个python模块时,它不应该运行内部导入吗?如果不是这样做的?

1 个答案:

答案 0 :(得分:0)

我的坏! 我根本没有在课堂上完成pygame.init()。插入后,一切正常。