我正在尝试将一个类导入到我的python主程序(alien_invaision.py)中,但由于某种原因,它给出了错误: ModuleNotFoundError,event,尽管我遵循了一本书中的说明,这样做是可行的。也许我应该使用不同的IDE。现在我正在使用IDLE。所以我可以试着写一段很棒的文字或者别的什么。
下面是我的代码:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
self.screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bg_color = (230, 230, 245)
def run_game(self):
while True:
for event in pygame.event.get():
if event == pygame.QUIT:
sys.exit()
self.screen.fill(self.bg_color)
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
if __name__=='__main__':
ai = AlienInvasion()
ai.run_game()我的settings.py代码是这样的:
class Settings:
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 245)我的ship.py代码是这样的:
import pygame
class Ship:
def __init__(self, ai_game):
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
self.screen.blit(self.image, self.rect)所以我不知道该怎么做。有人能帮上忙吗?
发布于 2020-12-31 19:52:12
我认为您需要在目录中创建一个名为__init__.py的文件,以便python将其视为一个模块。
您可以在__init__.py中包含代码,也可以将其保留为空。但它肯定就在那里。
像@itprorh66提到的那样发布你的目录结构,这将有助于缩小问题的范围,并检查这是否对你有效。以下是文档的link
https://stackoverflow.com/questions/65497696
复制相似问题