我是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'
我不知道我做错了什么,我已经看到了其他人,并且已经调用了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()
答案 0 :(得分:0)
导致错误是因为您覆盖此行中的block_group
变量:
for block_group in deadblocks:
ball.bounce()
pygame.sprite.spritecollide
返回一个精灵列表,所以如果它包含精灵并且你循环它,名称block_group
将被绑定到一个精灵对象,它不再引用一个组对象。这意味着下次你调用pygame.sprite.spritecollide(ball, block_group, True)
时游戏会崩溃,因为第二个参数必须是一个组而不是一个精灵。
更改for循环中的名称以解决问题。