Python AttributeError:“设置”对象没有“屏幕宽度”属性?如何解决这个问题?
问题是,当我在崇高的文本中构建这段代码时;它以AttributeError的形式给我一个输出:“设置”对象没有“屏幕宽度”属性。这是一个错误。我不知道,我怎样才能解决这个问题?虽然我在堆栈溢出中搜索了它,但这些答案并不能解决这个问题。有人能帮我检查一下这个代码并确定如何解决这个问题吗? 以下是我的alieninvasion.py代码: 这是我的setting.py代码 这是我得到的回报 回溯最近一次呼叫上次: 文件AlienInvasion.py,第34行,在 跑步游戏 文件AlienInvasion.py,第15行,在run_游戏中 ai_设置。屏幕宽度,ai_设置。屏幕高度 AttributeError:“设置”对象没有“屏幕宽度”属性Python AttributeError:“设置”对象没有“屏幕宽度”属性?如何解决这个问题?,python,pygame,settings,Python,Pygame,Settings,问题是,当我在崇高的文本中构建这段代码时;它以AttributeError的形式给我一个输出:“设置”对象没有“屏幕宽度”属性。这是一个错误。我不知道,我怎样才能解决这个问题?虽然我在堆栈溢出中搜索了它,但这些答案并不能解决这个问题。有人能帮我检查一下这个代码并确定如何解决这个问题吗? 以下是我的alieninvasion.py代码: 这是我的setting.py代码 这是我得到的回报 回溯最近一次呼叫上次: 文件AlienInvasion.py,第34行,在 跑步游戏 文件AlienInvas
我期待着找出我看不到的简单错误。您的设置类没有屏幕宽度,您应该使用ai.screen\u width。这应该是正确的代码: alieninvasion.py: setting.py: 在alieninvasion.py中,pygame.display.flip的缩进错误。缩进相同数量的while True语句将导致此行无法访问。我在代码中修改了它以供参考,同时也修改了一些拼写错误 在大多数其他编程语言中,缩进仅用于帮助使代码看起来漂亮。但在Python中,它是指示语句属于哪个代码块所必需的 要解决AttributeError:“Settings”对象没有属性“screen\u width”,必须在类设置中输入以下self.ai\u screen\u width=1200,并在AlienInvision.py中放置screen=pygame.display.set\u模式ai\u设置。ai_屏幕宽度,ai_设置。ai_屏幕高度
注意:对不起,我的英语说得不太好。您试图在run\u game函数中访问名为ai\u settings.screen\u width的变量,但名称拼写错误。这就是说,self.ai.screen\u width=1200行in Settings.\uuuu init\uuuuuuuuuu应该给您一个AttributeError:“Settings”对象没有属性“ai”-但您并没有问这个问题,所以我怀疑您发布的代码不是您正在运行的代码。我说得够好了吗?我已经更新了代码。self.ai.screen\u width=1200应该是这个self.screen\u width=1200,您认为代码没有运行是正确的。我尝试了不同的东西,但贴错了剪子。你怎么知道ai_settings.screen_width拼写错误?另外,我最初打算询问属性错误。感谢您花时间查看我的代码。杰伊:事实上,我认为应该将其更改为self.ai_screen_width=1200,这样它与另一个位置匹配,尽管您给出的确切名称并不十分重要,只要您一致地使用它,并且它是一个简单的标识符名称,即不包含嵌入的名称。。当我第一次尝试运行代码来重现问题时,我注意到了这个问题,并得到了一个与您的问题不同的异常。我不确定我是否理解您的意思。我没有使用ai.screen\u width吗?在您的代码中,您使用的是ai\u settings.screen\u width,但您应该使用ai\u settings.ai.screen\u width。我使用了您的设置,但仍然出现属性错误。AttributeError:“设置”对象没有属性“ai”
import sys
import pygame
from settings import Settings
def run_game():
#Intiialize game and create a 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:
#redraw the screen during each pass though the loop
screen.fill(ai_settings.bg_color)
# Watch for Keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# make th emost recently drawn screen visible
pygame.display.flip()
run_game()
class Settings():
"""A class to store all the settings for Aliean Invasion """
def __init__(self):
"""Initialize the game settings"""
#screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
#!/usr/bin/env python
import sys
import pygame
from setting import Settings
def run_game():
# Initialize game and create a 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:
# redraw the screen during each pass though the loop
screen.fill(ai_settings.bg_color)
# Watch for Keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# make the most recently drawn screen visible
pygame.display.flip()
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