JustNinja26
JustNinja26

Reputation: 7

Python Crash course chapter 13 "Object has no attribute '_sprite__g'" -- Sorry if duplicated.. had a look but couldnt find answer

I'm creating the Alien invasion game from the python crash course book. Up until this point i managed but now i have come onto this stage and it has broken everything, the game wont even start let alone run up until the last successful attempt. i am getting an error "Alien has no attribute '_sprite__g'"

I have checked that the Alien class is inheriting properly from sprite, and as far as i can tell it is but i am still new to coding. Used super. function to inherit and it worked until this point now im being told it has no attribute.

This is my code (apologies, there is a fair bit).

main file - alien invasion.py

Alien_invasion.py


import sys

import pygame
from pygame.sprite import Group

from settings import Settings
from ship import Ship
from alien import Alien
import game_functions as gf

def run_game():
    #Initialize pygame, settings and screen object.

    pygame.init()
    ai_settings=Settings()
    screen=pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    #Make a ship, a group of bullets and a group of aliens.
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()

    #Create the fleet of aliens.
    gf.create_fleet(ai_settings, screen, aliens)




    #Set the background colour.
    bg_colour = (230, 230, 230)


    #Start the main loop for the game.
    while True:
        gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        bullets.update()
        gf.update_bullets(bullets)
        gf.update_screen(ai_settings, screen, ship, aliens, bullets)
        
run_game()



Alien.py


#Alien.py

import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    """A class to represent a single alien in the fleet."""

    def __init__(self, ai_settings, screen):
        """Initialize the alien and set its starting position."""
        super(Alien, self).__init__
        self.screen = screen
        self.ai_settings = ai_settings

        #Load the alien image and set its rect attribute.
        self.image = pygame.image.load('images/alien.bmp')
        self.rect = self.image.get_rect()

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

        #Store the alien's exact position.
        self.x = float(self.rect.x)

    def blitme(self):
        """Draw the alien at it's current location."""
        self.screen.blit(self.image, self.rect)



Game functions.py


#Game functions.py

import sys

import pygame
from bullet import Bullet
from alien import Alien




def check_keydown_events(event, ai_settings, screen, ship, bullets):
    """respond to keypresses."""
    if event.key == pygame.K_RIGHT:
        ship.moving_right =True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings, screen, ship, bullets)
    elif event.key == pygame.K_q:
        sys.exit()

def fire_bullet(ai_settings, screen, ship, bullets):
    """Fire a bullet if limit is not reached yet. """
    #Create a new bullet and add it to the bullets group.
    if len(bullets) <ai_settings.bullets_allowed:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)


def check_keyup_events(event, ship):
    """Respond to key releases."""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key ==pygame.K_LEFT:
        ship.moving_left = False

def check_events(ai_settings, screen, ship, bullets):
    """Respond to keypresses and mouse events. """
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)

def update_screen(ai_settings, screen, ship, aliens, bullets):
    """update images on the screen and flip to the new screen."""
    #Redraw the screen during each pass through the loop.
    screen.fill(ai_settings.bg_color)
    #Redraw all bullets behind ship and aliens.
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    alien.draw(screen)

def update_bullets(bullets):
    """Update position of bullets and get rid of old bullets."""
    #Update bullet positions.
    bullets.update()

    #Get rid of bullets that have dissapeared.
    for bullet in bullets.copy():
        if bullet.rect.bottom <=0:
            bullets.remove(bullet)

    #Make the most recently drawn screen visible.
    pygame.display.flip()

def create_fleet(ai_settings, screen, aliens):
    """Create a full fleet of aliens."""
    #Ceate an alien and find the number of aliens in a row.
    #Spacing between each alien is equal to one alien qidth.
    alien = Alien(ai_settings, screen)
    alien_width = alien.rect.width
    available_space_x = ai_settings.screen_width - 2 * alien_width
    number_aliens_x = int(available_space_x / (2 * alien_width))

    #Create the first row of aliens.
    for alien_number in range(number_aliens_x):
        #Create an alien and place in in the row.
        alien = Alien(ai_settings, screen)
        alien.x = alien_width + 2 * alien_width * alien_number
        alien.rect.x = alien.x
        aliens.add(alien)

Up until adding the code for " Create fleet " it was all working fine, i only had a single ship but the game started and displayed and controlled correctly.

There is a couple more files in the project but i didnt as them they did not seem relevant? i havnt been made to make any ajustments to them as per the tutorial. I can add them if needed though.

** Note - in game functions i did try using "pygame.display.flip()" after create fleet but it seemed not to make a difference.

This is my Traceback error..

Traceback (most recent call last):
  File "D:\visual studios\Python\Learning Python\Python crash course\alien_invasion\alien_invasion.py", line 44, in <module>
    run_game()
  File "D:\visual studios\Python\Learning Python\Python crash course\alien_invasion\alien_invasion.py", line 27, in run_game
    gf.create_fleet(ai_settings, screen, aliens)
  File "D:\visual studios\Python\Learning Python\Python crash course\alien_invasion\game_functions.py", line 88, in create_fleet
    aliens.add(alien)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pygame\sprite.py", line 361, in add
    sprite.add_internal(self)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pygame\sprite.py", line 163, in add_internal
    self.__g[group] = 0
AttributeError: 'Alien' object has no attribute '_Sprite__g'

Thank you.

Upvotes: 0

Views: 933

Answers (1)

rachid boufous
rachid boufous

Reputation: 34

I had the same issue while creating a bullet.py, I guess u have to re-write the super function with python 3 standards:

super().__init__()

Upvotes: 1

Related Questions