AttributeError in pygame code

Joined
Dec 12, 2022
Messages
8
Reaction score
0
I am getting an error in my code that I am just stumped on.
Here is the code for the main file:
Code:
import sys

import pygame

from settings import Settings

class AlienInvasion:
    """Overall class to manage game assets and behavior"""

    def __init__(self):
        """Initialize the game, and create game resources"""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        # Set the background color
        self.bg_color = (230, 230, 230)

        

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            # 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()

            #Redraw the screen during each pass through the loop
            self.screen.fill(self.settings.bg_color)

if __name__ == '__main__':
    # Make a game instance, and run the game
    ai = AlienInvasion()
    ai.run_game()

And here is the code from the settings class I was trying to import:
Code:
class Settings:
    """A class to store all settings for alien invasion"""

    def __init__(self):
        """Initialize the game's settings"""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color(230, 230, 230)

The error I get is
Code:
AttributeError: 'Settings' object has no attribute 'bg_color'

I don't understand why I am getting this error because i made the attribute self.bg_color in the Settings class.
If anyone could help with the code or explain to me why I am getting this error I would appreciate it.
Thanks!
 
Joined
Dec 10, 2022
Messages
108
Reaction score
26
Maybe this line in your settings class self.bg_color(230, 230, 230). Should be self.bg_color = (230,230,230)
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Hey, thanks
Coding... These little things can be so hard to spot and trip you up pretty badly. That is why I like IDE's tho. I dont know what you use but some newbies insist on using plain text editors for whatever reason. I use Pycharm. Im pretty sure pycharm would've highlighted that line and said hey, this line is problematic. Its not doing the work for you and preventing you from learning which I think is what some people think. Its just giving a helping hand. You still have to correct the problem. Anyway. I think your reading Python Crash Course, correct? I read that book and loved it. Especially this pygame part.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,089
Latest member
Bobby2025b

Latest Threads

Top