我正在尝试 Eric Mattes 的 python 课程,并复制了提供的代码和其他人的在线代码,但不断收到 AttributeError: type object 'Settings' has no attribute 'screen_width' and cannot figure what is wrong someone please help.
我的代码如下:
alien.py:
import sys
import pygame
from settings import Settings
from ship import Ship
def run_game():
# Initialize game and create a screen object.
#initilise 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")
# make ship
ship = Ship(screen)
# Start the main loop for the game.
while True:
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Make the most recently drawn screen visible.
pygame.display.flip()
ship.blitme()
run_game()
我的设置脚本如下:
class Settings():
"""A class to store all the settings for Alien Invasion """
def __init__(self):
"""Initialize the game settings"""
# screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
self.ship_speed_factor = 8
我还在单独的单元格中的 alien.py 脚本中运行:
x = Settings()
x.screen_width
输出: 1200 哪个是正确的,我在找什么
enter code here
我知道它无法从 Settings 类调用 screen_width,这意味着拼写错误或类不工作,但我在设置文件本身中有 screen_width 工作,所以我很困惑。任何帮助将不胜感激!!!!!!!!!!!!
(我知道这个问题已经被问过 AttributeError: 'Settings' object has no attribute 'screen_width'?如何解决这个问题? 但它没有给出答案)
尝试执行代码并出现错误:AttributeError: type object 'Settings' has no attribute 'screen_width'