2016-06-13 303 views
-2

我在Python中編寫了一個簡單的遊戲,並且出現錯誤。當將Sprite添加到組時,'Bullet'對象沒有屬性'_Sprite__g'

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() 

這裏的錯誤:

Traceback (most recent call last): 
    File "C:\Documents and Settings\KHAI NHANH PC\Desktop\My Stuff\Python\Shooting things.py", line 57, in <module> 
    all_sprites_list.add(bullet) 
    File "C:\Python34\lib\site-packages\pygame\sprite.py", line 360, in add 
    sprite.add_internal(self) 
    File "C:\Python34\lib\site-packages\pygame\sprite.py", line 163, in add_internal 
    self.__g[group] = 0 
AttributeError: 'Bullet' object has no attribute '_Sprite__g' 

我檢查線57,我仍然不知道哪裏出了問題。 57 行:

all_sprites_list.add(bullet) 
+0

檢查版本,也許有不同的版本pygame的 – TankorSmash

回答

2

我認爲你缺少你的子彈初始化超級調用。

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 
2
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 

你需要記住,當你得到一個新的類,你必須調用。 init(個人)來自你的班級。所以在你調用你的Bullet類之後,你需要添加:pygame.sprite.Sprite。 init(self):在那之後,你可能已經爲你的其他課程做過了。

http://www.pygame.org/docs/tut/SpriteIntro.html

+0

你的答案是正確的,但我需要重新字呢。 –

+0

更好嗎? – Neeks

+0

是的男人。我說的唯一原因是你沒有提到他需要初始化Pygame Sprite類。 –

相關問題