Python 将精灵添加到组时,“Bullet”对象没有属性“\u Sprite\u\g”

Python 将精灵添加到组时,“Bullet”对象没有属性“\u Sprite\u\g”,python,pygame,Python,Pygame,我用Python编写了一个简单的游戏,但出现了一个错误 import pygame import random BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) RED = ( 255, 0, 0) BLUE = ( 0, 0, 255) class Block(pygame.sprite.Sprite): def __init__(self, color): super().__init__()

我用Python编写了一个简单的游戏,但出现了一个错误

import pygame
import random
BLACK = (   0,   0,   0)
WHITE = ( 255, 255, 255)
RED   = ( 255,   0,   0)
BLUE  = (   0,   0, 255)
class Block(pygame.sprite.Sprite):
    def __init__(self, color):
        super().__init__()
        self.image = pygame.Surface([20, 15])
        self.image.fill(color)
        self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface([15, 20])
        self.image.fill(RED)
        self.rect = self.image.get_rect()
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.x = pos[0]
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.Surface([4, 10])
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
    def update(self):
        self.rect.y -= 3
pygame.init()
screen_size = [700,500]
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Shooting things - Vers 2.0 of Blocks")
all_sprites_list = pygame.sprite.Group()
blocks_list = pygame.sprite.Group()
bullets_list = pygame.sprite.Group()
for i in range(50):
    block = Block(BLUE)
    block.rect.x = random.randrange( 660)
    block.rect.y = random.randrange( 480)

    blocks_list.add(block)
    all_sprites_list.add(block)
player = Player()
all_sprites_list.add(player)
score = 0
player.rect.y = 670
done = False
clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullet = Bullet()
            bullet.rect.x = player.rect.x
            bullet.rect.y = player.rect.y
            all_sprites_list.add(bullet)
            bullets_list.add(bullet)
    all_sprites_list.update()

    for bullet in bullets_list:
        block_hit_list = pygame.sprite.spritecollide(bullet, blocks_list, True)

        for block in block_hit_list:
            bullets_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 1
            print(score)
        if bullet.rect.y <-10:
            bullets_list.remove(bullet)
            all_sprites_list.remove(bullet)
    screen.fill(WHITE)
    all_sprites_list.draw(screen)
    pygame.display.update()
    clock.tick(60)
pygame.quit()

我想你错过了子弹头的超级呼叫

您需要记住,在派生新类时,必须从自己的类调用.initself。因此,在调用Bullet类之后,您需要添加:pygame.sprite.sprite.initself:之后,就像您在其他类中可能做的那样

在代码中,Block、Player和Bullet类继承自pygame.sprite.sprite。但是,您仅在Block和Player中调用super.\uuuuu init\uuuuuu方法,而在Bullet中也没有调用该方法

将行super.\uuu init\uuu添加到类项目符号中

之前:

之后:


顺便说一下,试着检查一下。

你需要做的就是

super().__init__()

在你的构造器之后。

检查版本,也许有不同版本的PyGame你的答案是正确的,但我想你需要重新解释一下。是的,伙计。我这么说的唯一原因是,您没有提到他需要初始化Pygame Sprite类。
all_sprites_list.add(bullet)
Try:
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__() #adding super call to make Bullet a pygame Sprite
        self.image = pygame.Surface([4, 10])
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
    def update(self):
        self.rect.y -= 3
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
##You did not add this line below 
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10,20))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = -10
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.Surface([4, 10])
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()  # Line to be added
        self.image = pygame.Surface([4, 10])
super().__init__()