Python Forum
[PyGame] object has no attribute 'add_internal'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] object has no attribute 'add_internal'
#1
I'm trying to produce a grid of stars in a pygame window but when I run the 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)
        self.stars = pygame.sprite.Group()
        self.create_grid()

    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 * star_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 * star_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_width = star.rect.width
        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()
            
I get this error:

Error:
File "C:\Users\djwil\Documents\python\python crash course\lib\site-packages\pygame\sprite.py", line 441, in add self.add(*sprite) TypeError: pygame.sprite.AbstractGroup.add() argument after * must be an iterable, not Star During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 77, in <module> ai = StarrySky() File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 25, in __init__ self.create_grid() File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 58, in create_grid self._create_star(star_number,row_number) File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 66, in _create_star self.stars.add(star) File "C:\Users\djwil\Documents\python\python crash course\lib\site-packages\pygame\sprite.py", line 454, in add sprite.add_internal(self) AttributeError: 'Star' object has no attribute 'add_internal'
Can someone help me with this error, I'm confused as I don't have 454 lines of code but that is where the error is reported.
Reply
#2
(Feb-23-2021, 02:11 PM)djwilson0495 Wrote: Can someone help me with this error, I'm confused as I don't have 454 lines of code but that is where the error is reported.

Look and see how the error
Error:
ite-packages\pygame\sprite.py", line 454, in add sprite.add_internal(self)
is from "pygame\sprite.py" not your game. You try to add an object to a sprite group, which can only accept pygame sprite objects. Make sure your sprites are initialized:

class Star(pygame.sprite.Sprite):
    pygame.sprite.Sprite.__init__(self):
Reply
#3
Thanks for your response. I edited my code for the Star class to this:
import pygame
from pygame.sprite import Sprite

class Star(pygame.sprite.Sprite):
    pygame.sprite.Sprite.__init__(self):

        def __init__(self,ai_game):
            """Initialise the star and its starting position."""
            super().__init__()
            self.screen = ai_game.screen
            
            # Load the star image and set its rect attribute 
            self.image = pygame.image.load(r'C:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\Images\star.bmp')
            self.rect = self.image.get_rect()


            # Start each star near the top left of the screen.
            self.rect.x = self.rect.width
            self.rect.y = self.rect.height

            # Store the stars exact horizontal position
            self.x = float(self.rect.x)
    
    
I then got this error:

Error:
File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 6, in <module> from star import Star File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star.py", line 5 pygame.sprite.Sprite.__init__(self): ^ SyntaxError: invalid syntax
I then tried this :

import pygame
from pygame.sprite import Sprite

class Star(pygame.sprite.Sprite):
    pygame.sprite.Sprite.__init__(self)

    def __init__(self,ai_game):
        """Initialise the star and its starting position."""
        super().__init__()
        self.screen = ai_game.screen
        
        # Load the star image and set its rect attribute 
        self.image = pygame.image.load(r'C:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\Images\star.bmp')
        self.rect = self.image.get_rect()


        # Start each star near the top left of the screen.
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # Store the stars exact horizontal position
        self.x = float(self.rect.x)
    
    
I then got this error:

Error:
File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star_sky.py", line 6, in <module> from star import Star File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star.py", line 4, in <module> class Star(pygame.sprite.Sprite): File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\star.py", line 5, in Star pygame.sprite.Sprite.__init__(self) NameError: name 'self' is not defined
Could you offer any further advice?
Reply
#4
It's pretty self-explanatory, you try to use a variable called self that isn't defined. But really, what are you trying to do with line 5? You're already calling the superclass constructor on line 9.
Reply
#5
My bad.

class Star(pygame.sprite.Sprite):
   
        def __init__(self,ai_game):
            super().__init__()  OR  pygame.sprite.Sprite.__init__(self)
 
You can also get a two in one with:
def __init__(self, game):
        self.groups = game.your_sprite_group#, another group, another group........
        pg.sprite.Sprite.__init__(self, self.groups)
game is wherever you create the sprite from.
Reply
#6
I have the lines:

def __init__(self,ai_game):
        """Initialise the star and its starting position."""
        super().__init__()
In my code above so I'm not sure why this is happening.
Reply
#7
I've never used super__init__, but you maybe have two sets of empty brackets. Maybe super(self, ai_game).__init__(self, ai_game) or something like that. One of those needs the self and/or ai_game as arguments.... I think.
Reply
#8
We don't need to guess:
>>> class Base:
...   def __init__(self, arg):
...     print(f"Base class __init__: {arg}")
...
>>> class Child(Base):
...   def __init__(self):
...     print("child __init__")
...     super().__init__(42)
...
>>> x = Child()
child __init__
Base class __init__: 42
Anyway, what's your code currently look like? What's the error you're currently getting? There's been a few changes so far, and I've lost track of where we are.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Attribute Error: object has no attribute djwilson0495 3 4,585 Jan-14-2021, 08:29 PM
Last Post: Larz60+
  beginner - object has no attribute gean 2 3,691 Nov-07-2019, 02:02 PM
Last Post: gean
  Tetris - AttributeError: 'list' object has no attribute 'y' abscorpy 6 6,564 Feb-28-2019, 05:20 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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