2017-04-22 27 views
1

私は最初にゲームをコーディングし始めました。 ボールとブロックを衝突させようとしている瞬間に、それはこれで実現する。Error AttributeError: 'ブロック'オブジェクトには属性 'スプライト'がありません - 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()を呼び出すことでソートされているかわかりません。 ()ですが、私はすでにこれを行っていますが、助けてくれれば幸いです、ありがとう、

ここで私のフルコード - それは良いことではないので、 :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

コード構造に関するヒントが必要な場合は、プログラムが正しく機能することを確認し、https://codereview.stackexchange.comに完全なコードを投稿してください。 – skrx

答えて

0

あなたはこの行のblock_group変数上書きため、エラーが発生した:

for block_group in deadblocks: 
    ball.bounce() 

pygame.sprite.spritecollideは、スプライトのリストを返すので、それはそれの上にスプライトとあなたのループが含まれている場合は、名前block_groupはスプライトオブジェクトにバインドされ、グループオブジェクトを参照しません。つまり、次回にpygame.sprite.spritecollide(ball, block_group, True)と呼ぶと、2番目の引数はスプライトではないグループでなければならないため、ゲームがクラッシュすることになります。

forループの名前を変更して問題を解決します。

+1

本当にありがとう - 私はそれを完全に忘れてしまった –

関連する問題