我正在尝试学习Python 3.11.6和pygame 2.5.2,当我为“外星人入侵”游戏运行文件alin_invasions.py时,而不是像以前那样,当设置包含在alin_invisions.py文件中,而不是我根据说明制作的新设置.py文件时,我从下面的Python收到一条消息,说文件settings.py不会导入,如下所示:
module settings.py not found
在消息中,它说,即使我将settings.py保存在python_work的同一文件夹中,也找不到用于设置的模块,即文件settings.py,因为alin_invasions.py无法找到和读取设置,所以外星人入侵游戏屏幕不会加载。
以下是我为下面的alin_invasions.py文件以及下面的settings.py文件编写的代码:
alin_invasions.py
import sys
import pygame
from settings import Settings
def run_game():
# Initialize pygame, settings, and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_color)
# Make the most recently drawn screen visible.
pygame.display.flip()
run_game()
设置.py
class Settings():
"""A class to store all settings for Alien invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
我加载了“Alien Invasions”,而不是得到一个屏幕,我得到的是Traceback消息,说找不到设置模块,而不是像我在alin_invasion.py文件中包含设置时那样得到一个“Alien Invasions”屏幕,而不是我根据Python速成课程书中的说明制作的settings.py文件。