Why do I keep getting an AttributeError?

Hi everyone, basically im just trying to make this grid of stars for something and I just cant see a logical reason for an AttributeError…

import sys
import pygame
from pygame.sprite import Sprite
from sett import Settings

class Stars(Sprite):
    """A class to represent a sky full of rotating stars"""

    def __init__(self):
        """Configure program attributes"""

        self.screen = pygame.display.set_mode(self.sett.screen_width, self.sett.screen_height)

        self.image = pygame.image.load("star1(1).bmp")
        self.rect = self.image.get_rect()

        self.stars = pygame.sprite.Group()


    def _create_sky(self):
        """configure space for stars"""
        star_width, star_height = self.rect.size
        available_space_x = self.screen_width - (2 * star_width)
        available_space_y = self.screen_height - (2 * star_height)
        number_rows = available_space_y // (2 * star_height)
        number_stars_x = available_space_x // (2 * star_width)

        #Create the full fleet of aliens.
        for row_number in range(number_rows):
            for star_number in range(number_stars_x):
                self._create_star(star_number, row_number)

    def _create_star(self, star_number, row_number):
        """create stars"""
        star_width, star_height = self.rect.size
        self.x = star_width + 2 * star_width * star_number
        self.rect.x = star.x
        self.rect.y = star_height + 2 * self.rect.height * row_number

if __name__ == '__main__':
    si = Stars()

other than this I just have one module named sett.py which contains the settings but i cant import the sett module…

python just comes back with:

Traceback (most recent call last):
  File "C:\Users\jacob\OneDrive\Desktop\New folder (3)\star.py", line 48, in <module>
    si = Stars()
  File "C:\Users\jacob\OneDrive\Desktop\New folder (3)\star.py", line 13, in __init__
    self.screen = pygame.display.set_mode(self.sett.screen_width, self.sett.screen_height)
AttributeError: 'Stars' object has no attribute 'sett'

Within class method self refers to the class instance. So self.sett.screen_width looks for the sett attribute in the among attributes of the specific Stars instance (and the class).

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.