2014-04-14 167 views
0

我一直致力於在pygame中創建一個小型的太空侵略者風格的遊戲。但是我已經接近尾聲了,我想這樣做,如果敵方的艦船與我的船相撞,就會發現碰撞並且遊戲結束。Pygame Sprite碰撞

到目前爲止,我已經有了用於檢測子彈和敵方船隻發生碰撞的代碼,但是當我嘗試將其重寫爲敵人/玩家碰撞時,它並不像預期的那樣工作,所以我認爲我會做一些不正確的事情。

代碼中的問題:

for block in block_list: 

    player_hit_list = pygame.sprite.spritecollide(block, player_list, True) 

    for player in player_hit_list: 
     explosion.play() 
     block_list.remove(block) 
     player_list.remove(player) 
     all_sprites_list.remove(block) 
     all_sprites_list.remove(player) 

    if block.rect.y < +10: 
     block_list.remove(block) 
     all_sprites_list.remove(block) 

全碼:http://pastebin.com/FShPuR6A

有沒有人能告訴我爲什麼我的心不是代碼運行?

非常感謝

+0

實際發生了什麼?遊戲永不退出,玩家消失了嗎? –

+0

@BartlomiejLewandowski敵艦隻是正常通過玩家,沒有消失,遊戲不退出:( – Oscar

回答

0
class Block(pygame.sprite.Sprite): 
    def __init__(self, color): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load("spaceship.png") 
     self.rect = self.image.get_rect() 

    def update(self): 
     self.rect.y += 1 

class Player(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.x=0 
     self.y=0 
     self.image = pygame.image.load("alien.png") 
     self.rect = self.image.get_rect() 

    def update(self): 
     pos = pygame.mouse.get_pos() 
     self.rect.x = pos[0] 

乍一看,你有一個塊類,與移動像外星人飛船的圖片。播放器類有一個外星人的圖像,並隨着鼠標移動。

我的建議是重命名類,以便你知道什麼是什麼。發現錯誤會更容易。

編輯:

它也像你的球員根本不會動:

def render(self): 
     if (self.currentImage==0): 
      screen.blit(self.image, (self.x, self.y)) 

你可以通過self.rect.x更新的球員,但你畫使用self.xself.y