Python Forum
[PyGame] Pygame attribute error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Pygame attribute error
#1
I'm trying to get a pygame window with a grid of stars. Here's my code:

import sys 

import pygame  # contains functionality to make a game

from settings import Settings
from star import Star

class StarrySky:
    """Initialise a grid of stars on the screen."""
    def __init__(self):
        """Initialise the game and create game resources."""
        pygame.init() # initialise the background settings 
        # which the game needs to work properly
        self.settings = Settings()

        self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
        # tells pygame to figure out a window size to fill the screen
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        # updates the screen settings after fullscreen has been created
        pygame.display.set_caption("Starry Sky")

        self.star = Star(self)

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self.check_events()         
            self.update_screen()

    def check_events(self):
    # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.K_q:
                sys.exit()

    def create_grid(self):
        """Create the grid of stars."""
        # create a star and find the number of stars in a row.
        # spacing between stars equal to 1 star width.
        star = Star(self)
        star_width, star_height = star.rect.size
        available_space_x = self.settings.screen_width - (2 * alien_width)
        number_stars_x = available_space_x // (2 * star_width)

        # Determine the number of rows of aliens that fit on the screen.
        available_space_y = (self.settings.screen_height - 
        (3 * alien_height) )
        number_rows = available_space_y // (2 * star_height)

        # Create the full sky of stars. 
        for row_number in range(number_rows):
        # Create the first row of stars.
            for star_number in range(number_stars_x):
                # Create a star and place it in the row.
                self._create_star(star_number,row_number) 
        
    def create_star(self, row_number, star_number):
        star = Star(self)
        star.x = star_width + 2 * star_width * star_number
        star.rect.x = star.x
        star.rect.y = star.rect.height + 2 * star.rect.height * row_number
        self.stars.add(star)

    def update_screen(self):
        """Update images on the screenand flip to the new screen."""
        # redraw the screen during each pass through the loop 
        self.screen.fill(self.settings.bg_color)
        self.stars.draw(self.screen)
        pygame.display.flip()

if __name__ == "__main__":
    # make a game instance and run the game
    ai = StarrySky()
    ai.run_game()
            
But I'm getting this error:

Error:
File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 75, in <module> ai.run_game() File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 29, in run_game self.update_screen() File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 69, in update_screen self.stars.draw(self.screen) AttributeError: 'StarrySky' object has no attribute 'stars'
Can anyone help? Thanks
Reply
#2
What line do you set self.stars to something? I see where you add, and draw, but I don't see where it starts existing.
Reply
#3
Does it not start existing here?

self.star = Star(self)
Reply
#4
(Feb-18-2021, 09:53 AM)djwilson0495 Wrote: Does it not start existing here?

self.star = Star(self)

no. That is self.star, while the error is for self.stars.
nilamo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Attribute Error: object has no attribute djwilson0495 3 4,518 Jan-14-2021, 08:29 PM
Last Post: Larz60+
Big Grin Error installing Pygame-Zero dpa2007 1 3,138 Dec-26-2020, 03:50 PM
Last Post: MK_CodingSpace
  pygame error in my clicker game CrazyMakes 2 2,511 Apr-19-2020, 03:04 AM
Last Post: Windspar
  Error to install pygame skp 1 3,457 Apr-14-2020, 05:17 PM
Last Post: joe_momma
  installing pygame error pylab 1 4,190 Dec-31-2019, 05:44 PM
Last Post: pylab
  [PyGame] pygame image loading error BlueClaw 6 6,325 Dec-10-2019, 08:50 PM
Last Post: BlueClaw
  Problem with music - Pygame.error GaseBall 1 3,150 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU
  Pygame to exe error Clunk_Head 0 2,926 Oct-19-2019, 01:34 PM
Last Post: Clunk_Head
  [PyGame] Error setting up pygame wazee 10 13,174 Jun-14-2018, 11:19 PM
Last Post: wazee
  [pyGame] My First Test, Error Fount ! JamieVanCadsand 3 5,252 Sep-25-2017, 04:13 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020