I have this error with pygame , when i try to run code from Python Crash Course (Exception has occurred: AttributeError )

nic mic :

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

Ch3steR :

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.

edited at
0

Comments

0 comments
Login to comment

Related

Pygame window not responding error when i try to run a code

Error occurred when I try to compile my code

I receive this error 'str' object has no attribute 'text' when I try to run the following code

AttributeError: object has no attribute (Exmple from book "Python CRASH COURSE")

When I try IntroductionScreen(), an error occurred

I have a problem when I try to run python command in bash

I have an array in a json and when I try to access it with the following code it has an error for multiple words. Can anybody help fix the code

I get this error when I try to convert a list from a Snowpark df to a Pandas df: AttributeError: 'list' object has no attribute 'to_pandas'

Keep getting this error when i try to load an image asset "Exception has occurred. FlutterError (Unable to load asset: assests/1.png)"

AttributeError Python Crash Course

I can't seem to troubleshoot this code( python crash course)

When I run the code chunks everything works but when I try to Knit I get an error

Error occuring when I try to run decorator with @ - Python

attribute error in Flask when I run (AttributeError: 'NoneType' object has no attribute 'run' )

Why I get exception when I try to fetch data from tables that has many to many relationship

AttributeError: 'list' object has no attribute 'split' when i try to split a row from csv file

```AttributeError: 'module' object has no attribute 'set_random_seed'``` when i run ```python2 ./train.py``` from the terminal

Error on jest when I try to run the test

Getting Error and App crash When I try to Access current location

When I try to run my code with the "java" command in the cmd window, I get an error

pygames not working for python when I try and run it

Why do I get AttributeError when I run this code?

"My App has stopped" error when I try to run my app on a real phone

I have got a decode error when I try to print a string got from va_list of C

Why am I getting a syntax error whenever I try to run VS code python terminal?

Why does Python crash when I try to sum this numpy array?

[cocos2d-x]I have an error when I try to run 'python android-build.py -p 19 cpp-tests' in Windows 10

I'm getting an error when I try to run PowerShell commands from a batch file

When i try to refresh my page i have this error