I am trying to make a project from the book Python Crash course. I made everything exactly as the code snippets from the book shows but i constantly get the following error :
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()
This is the code for my Sudoku file:
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()
And this is the code from my Settings file:
class Settings():
def set(self):
self.screenWidth = 1200
self.screenHeight = 800
self.bgColor = (0.235,0)
I coulden't find a solution online
You need to call set
on the object igSettings
to set attributes of the object.
igSettings = Settings()
igSettings.set()
Or simply make them class attributes, you access them directly now.
class Settings:
screenWidth = 1200
screenHeight = 800
bgColor = (0.235,0)
Now you don't even need to call set
. You can look more about how classes
work in python.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments