Python Forum
Attribute Error: object has no attribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attribute Error: object has no attribute
#1
Hi I'm trying to make a basic game. I'm attempting to have rows of stars as background. This is the code I'm using:

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 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.ship.blitme()
            self.stars.draw(self.screen)
            pygame.display.flip()

if __name__ == "__main__":
    # make a game instance and run the game
    ai = StarrySky()
    ai.run_game()
            


However I'm getting this error:

Error:
File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 70, in <module> ai.run_game() AttributeError: 'StarrySky' object has no attribute 'run_game'
I'm not sure why this is occurring as I've used this line before in previous programs with no issue.
Reply
#2
Please post complete, unaltered error traceback.
It contains valuable information about your scripts process.

what's shown is enough to see that there is no method named run_game.
You can't run it if it's not there.
Reply
#3
This is the whole error report:

Error:
pygame 2.0.0 (SDL 2.0.12, python 3.9.0) Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 70, in <module> ai.run_game() AttributeError: 'StarrySky' object has no attribute 'run_game'
Reply
#4
This is a repost of the bottom from post #2

what's shown is enough to see that there is no method named run_game.
You can't run it if it's not there.

---- new ----
To elaborate:
You instantiate ai as StarrySky but StarrySky does not have a method named run_game
so ai.run_game is invalid
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] object has no attribute 'add_internal' djwilson0495 7 658 Feb-26-2021, 05:06 PM
Last Post: nilamo
  [PyGame] Pygame attribute error djwilson0495 3 380 Feb-18-2021, 03:34 PM
Last Post: michael1789
  AttributeError : method has no attribute djwilson0495 2 520 Dec-07-2020, 01:38 PM
Last Post: djwilson0495
  [split] Apparently module has no attribute display? Angelo26 1 578 Mar-11-2020, 07:14 PM
Last Post: nilamo
  beginner - object has no attribute gean 2 1,081 Nov-07-2019, 02:02 PM
Last Post: gean
  Tetris - AttributeError: 'list' object has no attribute 'y' abscorpy 6 3,386 Feb-28-2019, 05:20 PM
Last Post: Larz60+
  [PyGame] Int object not callable error! ghost0fkarma 2 2,978 Nov-14-2018, 03:02 AM
Last Post: ghost0fkarma
  [split] Apparently module has no attribute display? Kolterdyx 1 1,282 Sep-09-2018, 05:14 AM
Last Post: buran
  [PyGame] Apparently module has no attribute display? Klar 29 15,150 Dec-20-2017, 02:46 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

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