【问题标题】:using sprite object classes from different files pygame使用来自不同文件pygame的精灵对象类
【发布时间】:2020-05-05 23:31:50
【问题描述】:

尝试将不同 python 文件中的类导入到主 python 文件中。我想将一个老板的类导入到主游戏的 python 文件中。包含boss类的文件名为bosscode.py,主游戏名为shmup.py。

现在,bosscode.py 是这样的

import pygame
from shmup import *

class Boss(pygame.sprite.Sprite):  #creates the boss class as a sprite object
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 70))
        self.image.fill(white)
        self.rect.y = WIDTH / 2  
        self.rect.y = 80    

我尝试通过说将此文件导入主文件

from bosscode import *

boss = Boss()

但它会出现错误提示

NameError: name 'Boss' is not defined

不知道该怎么做。我试过说 import bosscodefrom bosscode import boss 但它似乎不起作用。我做错了什么?

【问题讨论】:

    标签: python file class pygame


    【解决方案1】:

    原来我只是愚蠢,忘记为 Boss 类定义一个矩形。在我将self.rect = self.image.get_rect() 添加到boss 类后,它运行良好。我什至不需要 bosscode.py 中的 from shmup import *

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2018-10-26
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      相关资源
      最近更新 更多