错误属性:'block'对象没有属性“ sprites” - pygame



我是pygame的新手,已经开始编码我的第一款游戏,我目前有一个问题,试图碰撞球和障碍物,它提出了。

 Traceback (most recent call last):
   File "C:UsersRobert HartleyDocumentsPythonPYGAME32BITBall Game.py",      line 152, in <module>
deadblocks = pygame.sprite.spritecollide(ball, block_group, True)
   File "C:UsersRobert HartleyAppDataLocalProgramsPythonPython35-32libsite-packagespygamesprite.py", line 1514, in spritecollide
for s in group.sprites():
 AttributeError: 'Block' object has no attribute 'sprites'

我不知道我做错了什么,我已经看到了其他人,并且已经对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()

造成错误是因为您覆盖此行中的block_group变量:

for block_group in deadblocks:
    ball.bounce()

pygame.sprite.spritecollide返回一个精灵列表,因此,如果它包含精灵并且您循环循环,则名称block_group将绑定到Sprite对象,并且不再指组对象。这意味着下次打电话给pygame.sprite.spritecollide(ball, block_group, True)时,游戏将崩溃,因为第二个参数必须是一个小组而不是精灵。

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

最新更新