Python 错误属性错误:';区块';对象没有属性';精灵&x27;-皮加梅

Python 错误属性错误:';区块';对象没有属性';精灵&x27;-皮加梅,python,pygame,Python,Pygame,我是pygame的新手,已经开始编写我的第一个游戏, 我现在有个问题,当我试图碰撞球和挡块的时候,我想到了这个 Traceback (most recent call last): File "C:\Users\Robert Hartley\Documents\Python\PYGAME32BIT\Ball Game.py", line 152, in <module> deadblocks = pygame.sprite.spritecollide(ball, b

我是pygame的新手,已经开始编写我的第一个游戏, 我现在有个问题,当我试图碰撞球和挡块的时候,我想到了这个

 Traceback (most recent call last):
   File "C:\Users\Robert Hartley\Documents\Python\PYGAME32BIT\Ball Game.py",      line 152, in <module>
deadblocks = pygame.sprite.spritecollide(ball, block_group, True)
   File "C:\Users\Robert Hartley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pygame\sprite.py", line 1514, in spritecollide
for s in group.sprites():
 AttributeError: 'Block' object has no attribute 'sprites'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Robert Hartley\Documents\Python\PYGAME32BIT\Ball Game.py”,第152行,在
deadblocks=pygame.sprite.spritecollide(球,block_组,真)
SpriteClide中的文件“C:\Users\Robert Hartley\AppData\Local\Programs\Python\Python35-32\lib\site packages\pygame\sprite.py”,第1514行
对于组中的s.sprites():
AttributeError:“块”对象没有属性“精灵”
我不知道我做错了什么,我也见过其他人这样做,并且已经通过调用super()。init()进行了排序,但我已经这样做了,任何帮助都会很乐意接受,谢谢

这里是我的完整代码——它可能不是什么好东西,所以任何关于它的帮助或提示都是很好的:D

import pygame
import random
import math
blue=(0,0,255)
green = (0,255,0)
red = (255,0,0)
black=(0,0,0)
white = (255,255,255)
height = 600
width = 800
block_width = 64
block_height = 64
ball_width = 30
ball_height = 30
angle = 0
mousex = 100
mousey = 400
class Ball(pygame.sprite.Sprite):

    def __init__(self):
        super(Ball,self).__init__()   
        self.velx = 2
        self.vely = -1
        self.x = 370
        self.y = 530
        self.image = pygame.Surface([ball_width,ball_height])

        self.image.fill(red)

        self.rect = self.image.get_rect()



    def set_pos(self, x, y):
        self.rect.x = x
        self.rect.y = y

    def set_angle(self,angle):
        self.velx = math.cos(self.angleradi)
        self.vely = math.sin(self.angleradi)
        self.angleradi = math.radians(self.direction)
    def bounce(self):
        self.direction = math.degrees(angle)
        self.direction = (180 - self.direction) %360


    def update(self):


        self.x = self.x + self.velx
        self.y = self.y + self.vely


        if self.x <= 0 or self.x >= width-ball_width:
            self.velx = self.velx * -1
        if self.y<=0:        
            self.vely = self.vely * -1
        if self.y >= height- ball_height:
            self.x = 370
            self.y = 570
            self.velx = 0
            self.vely = 0

        self.rect.x = self.x
        self.rect.y = self.y

        screen.blit(self.image, (self.rect.x,self.rect.y))
        if self.rect.x == 370 and self.rect.y ==570:
            return False
        else :
            return True





class Block(pygame.sprite.Sprite):
    pygame.init()

    def __init__(self):
        super(Block,self).__init__()

        self.r = random.randint(0,100)
        self.g = random.randint(0,100)
        self.b = random.randint(175,255)

        self.image = pygame.Surface([block_width, block_height])

        self.image.fill((self.r,self.g,self.b))

        self.rect = self.image.get_rect()
    def set_pos(self,x,y):

        self.rect.x = x
        self.rect.y = y



if __name__ == "__main__":

    pygame.init()

    screen = pygame.display.set_mode([width, height])
    pygame.display.set_caption('Ballz')



    clock = pygame.time.Clock()
    fps = 120
    allsprites = pygame.sprite.Group()
    #create the ball
    ball_group = pygame.sprite.Group()

    x = random.randint(0,600)
    y = random.randint(0, 200)
    ball = Ball()
    ball_group.add(ball)
    allsprites.add(ball)
    ball.set_pos(370,570)

    block_group = pygame.sprite.Group()
    #create the blocks   
    for i in range(10):
        x = random.randint(0,600)
        x = x - x % 70 #rounds to the nearest 70
        y = random.randint(0, 200)
        y = y - y % 70
        block = Block()
        allsprites.add(block)
        block_group.add(block)
        block.set_pos(x, y)



while True :

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if ball.update() == False:
                mousex,mousey = pygame.mouse.get_pos()
                angle = math.atan((600 - mousey)/(mousex-370))





    screen.fill(black)     
    clock.tick(fps)
    deadblocks = pygame.sprite.spritecollide(ball, block_group, True)


    for block_group in deadblocks:
        ball.bounce()





    allsprites.draw(screen)
    ball.update()
    pygame.display.flip()
导入pygame
随机输入
输入数学
蓝色=(0,0255)
绿色=(0255,0)
红色=(255,0,0)
黑色=(0,0,0)
白色=(255255)
高度=600
宽度=800
块宽度=64
块体高度=64
球头宽度=30
球头高度=30
角度=0
鼠标=100
老鼠=400
班级舞会(pygame.sprite.sprite):
定义初始化(自):
超级(球,自我)。\uuuuu初始\uuuuuuu()
self.velx=2
self.vely=-1
self.x=370
self.y=530
self.image=pygame.Surface([球的宽度,球的高度])
self.image.fill(红色)
self.rect=self.image.get_rect()
def设置位置(自身、x、y):
self.rect.x=x
自校正y=y
def设置角度(自身、角度):
self.velx=math.cos(self.angelradi)
self.vely=math.sin(self.angelradi)
self.angelradi=数学弧度(self.direction)
def反弹(自我):
self.direction=数学度(角度)
自我方向=(180-自我方向)%360
def更新(自我):
self.x=self.x+self.velx
self.y=self.y+self.y
如果self.x=width-ball\u width:
self.velx=self.velx*-1
如果self.y=高度-球高度:
self.x=370
self.y=570
self.velx=0
self.vely=0
self.rect.x=self.x
self.rect.y=self.y
blit(self.image,(self.rect.x,self.rect.y))
如果self.rect.x==370,self.rect.y==570:
返回错误
其他:
返回真值
类块(pygame.sprite.sprite):
pygame.init()
定义初始化(自):
超级(块,自).\uuu初始化
self.r=random.randint(0100)
self.g=random.randint(0100)
self.b=random.randint(175255)
self.image=pygame.Surface([块宽度,块高度])
self.image.fill((self.r、self.g、self.b))
self.rect=self.image.get_rect()
def设置位置(自身、x、y):
self.rect.x=x
自校正y=y
如果名称=“\uuuuu main\uuuuuuuu”:
pygame.init()
screen=pygame.display.set_模式([宽度,高度])
pygame.display.set_标题('Ballz')
clock=pygame.time.clock()
fps=120
allsprites=pygame.sprite.Group()
#创造球
ball\u group=pygame.sprite.group()
x=random.randint(0600)
y=random.randint(0200)
ball=ball()
球组。添加(球)
所有精灵。添加(球)
球组位置(370570)
block_group=pygame.sprite.group()
#创建块
对于范围(10)内的i:
x=random.randint(0600)
x=x-x%70#四舍五入到最接近的70
y=random.randint(0200)
y=y-y%70
block=block()
allsprites.add(块)
块组。添加(块)
块。设置位置(x,y)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
如果event.type==pygame.MOUSEBUTTONDOWN:
如果ball.update()==False:
mousex,mousey=pygame.mouse.get_pos()
角度=数学坐标((600-mousey)/(mousex-370))
屏幕填充(黑色)
时钟滴答声(fps)
deadblocks=pygame.sprite.spritecollide(球,block_组,真)
对于死区块中的块组:
ball.bounce()
所有精灵。绘制(屏幕)
ball.update()
pygame.display.flip()

导致错误的原因是您覆盖了此行中的
块组
变量:

for block_group in deadblocks:
    ball.bounce()
pygame.sprite.spritecollide
返回一个精灵列表,因此如果它包含精灵并在其上循环,则名称
block_group
将绑定到一个精灵对象,它不再引用组对象。这意味着下次调用
pygame.sprite.spritecollide(ball,block\u group,True)
时,游戏将崩溃,因为第二个参数必须是组而不是精灵


更改for循环中的名称以修复此问题。

导致此错误的原因是您重写了此行中的
block\u group
变量:

for block_group in deadblocks:
    ball.bounce()
pygame.sprite.spritecollide
返回一个精灵列表,因此如果它包含精灵并在其上循环,则名称
block_group
将绑定到一个精灵对象,它不再引用组对象。这意味着下次调用
pygame.sprite.spritecollide(ball,block\u group,True)
时,游戏将崩溃,因为第二个参数必须是组而不是精灵


更改for循环中的名称以解决此问题。

如果需要有关代码结构的更多提示,请确保程序正常运行,然后发布完整的代码。如果需要有关代码结构的更多提示,请确保程序正常运行,然后发布完整的代码。