gpt4 book ai didi

python - 当我尝试从 Python Crash Course 运行代码时,pygame 出现此错误(发生异常 : AttributeError )

转载 作者:行者123 更新时间:2023-12-04 01:25:54 26 4
gpt4 key购买 nike

我正在尝试根据 Python 速成类(class)一书制作一个项目。我完全按照书中显示的代码片段制作了所有内容,但我不断收到以下错误:

Exception has occurred: AttributeError 'Settings' object has no attribute 'screenWidth' File "D:\Visual Studio Code projects\Sudoku game\window.py", line 12, in run_game (igSettings.screenWidth, igSettings.screenHeight)) File "D:\Visual Studio Code projects\Sudoku game\window.py", line 28, in run_game()

这是我的数独文件的代码:

import sys
import pygame

from settings import Settings

def run_game():
pygame.init()

igSettings = Settings()

screen = pygame.display.set_mode(
(igSettings.screenWidth, igSettings.screenHeight))
pygame.display.set_caption("Sudoku")

while True:

screen.fill(igSettings.bgColor)

for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)



pygame.display.flip()


run_game()

这是我的设置文件中的代码:

class Settings():

def set(self):

self.screenWidth = 1200
self.screenHeight = 800
self.bgColor = (0.235,0)

网上找不到解决办法

最佳答案

您需要在对象igSettings 上调用set 来设置对象的属性。

igSettings = Settings()
igSettings.set()

或者简单地使它们成为类属性,您现在可以直接访问它们。

class Settings:
screenWidth = 1200
screenHeight = 800
bgColor = (0.235,0)

现在您甚至不需要调用set。你可以看看更多关于如何classes在 python 中工作。

关于python - 当我尝试从 Python Crash Course 运行代码时,pygame 出现此错误(发生异常 : AttributeError ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61939772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com