Python 3.x Python速成班第13章“;对象没有属性'_雪碧'&“——”对不起,如果复制了。。看了一眼,却找不到答案

Python 3.x Python速成班第13章“;对象没有属性'_雪碧'&“——”对不起,如果复制了。。看了一眼,却找不到答案,python-3.x,attributes,super,Python 3.x,Attributes,Super,我正在根据python速成教程创建外星人入侵游戏。到目前为止我一直在努力,但现在我已经走到了这个阶段,它打破了一切,比赛甚至不会开始,更不用说直到最后一次成功的尝试。我收到一个错误“外星人没有属性'\u精灵'' 我已经检查过外星人类是否正确地从sprite继承,据我所知是这样,但我对编码还是新手。使用超级。函数继承,它一直工作到现在,我被告知它没有属性 这是我的代码(抱歉,有一点) 主文件-外星人入侵.py 外星人入侵 外星人 #Alien.py import pygame from pyg

我正在根据python速成教程创建外星人入侵游戏。到目前为止我一直在努力,但现在我已经走到了这个阶段,它打破了一切,比赛甚至不会开始,更不用说直到最后一次成功的尝试。我收到一个错误“外星人没有属性'\u精灵''

我已经检查过外星人类是否正确地从sprite继承,据我所知是这样,但我对编码还是新手。使用超级。函数继承,它一直工作到现在,我被告知它没有属性

这是我的代码(抱歉,有一点)

主文件-外星人入侵.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)



游戏功能.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)


#游戏功能.py
导入系统
导入pygame
从bullet导入bullet
从外国人进口外国人
def check_keydown_事件(事件、ai_设置、屏幕、发货、项目符号):
“”“响应按键。”“”
如果event.key==pygame.K_RIGHT:
ship.moving_right=真
elif event.key==pygame.K_左:
ship.moving_left=真
elif event.key==pygame.K_空间:
发射子弹(ai设置、屏幕、飞船、子弹)
elif event.key==pygame.K_q:
sys.exit()
def fire_子弹(ai_设置、屏幕、装运、子弹):
“”“如果尚未达到限制,则发射子弹。”“”
#创建新项目符号并将其添加到项目符号组。

如果len(bullets)我在创建bullet.py时遇到了同样的问题,我想你必须用python 3标准重新编写超级函数:

super().__init__()

你能公布全部错误吗?在python中,错误回溯使得查找问题发生的位置更加容易。我可以看到
sprite
类中发生的错误,它的私有变量
\uu g
缩小了它的范围,但是筛选代码将花费比查看错误回溯更多的时间。请将此问题放在注释中,而不是放在注释中:)完成,谢谢。如果还有什么事,请尽管告诉我。我还不熟悉堆栈溢出,有人告诉我它是一个很好的资源和社区,所以我想我可以试试。这行代码看起来像是一个问题。尝试执行
super()。\uuuu init\uuuu()
。如果这不能解决问题,也可能会对您有所帮助。不走运,回溯错误更改为命名错误alien未定义。
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'
super().__init__()