Pygame groupcollide无效

时间:2016-02-14 19:19:30

标签: python pygame collision

我正在创建一个Space Invader克隆来练习python和pygame编程。我正在使用pygame.sprite.groupcollide来检查玩家的子弹和敌人之间的碰撞,这是正常的。但是,我尝试使用这种类型的碰撞检查来增加玩家在子弹摧毁敌人后的得分。因此,我创建了两个带有自己图像的分数类,这些分数在子弹击中敌人以保持分数后发生变化。然而,即使这两个类几乎完全相同,一个的碰撞检查工作正常,而另一个没有。最令人困惑的事实是,如果我改变顺序,我将它们添加到一个组或不同的组中,一个将停止工作,另一个将得到修复。这是代码:

...
_image_library = {}
def get_image (path):
    global _image_library
    image = _image_library.get (path)
    if image == None:
        canonicalized_path = path.replace ("/", os.sep).replace ("\\", os.sep)
        image = pygame.image.load (canonicalized_path)
        _image_library [path] = image
    return image
...
score_rect_1 = pygame.Rect (680,540,42,57)
score_rect_2 = pygame.Rect (740,540,42,57)
...    
class Score_cards_1 (pygame.sprite.Sprite):
    def __init__ (self):
        pygame.sprite.Sprite.__init__ (self)
        self.images = []
        self.images.append (get_image ("number_0.1.png").convert_alpha())
        self.images.append (get_image ("number_1.1.png").convert_alpha())
        self.images.append (get_image ("number_2.1.png").convert_alpha())
        self.images.append (get_image ("number_3.1.png").convert_alpha())
        self.images.append (get_image ("number_4.1.png").convert_alpha())
        self.images.append (get_image ("number_5.1.png").convert_alpha())
        self.images.append (get_image ("number_6.1.png").convert_alpha())
        self.images.append (get_image ("number_7.1.png").convert_alpha())
        self.images.append (get_image ("number_8.1.png").convert_alpha())
        self.images.append (get_image ("number_9.1.png").convert_alpha())
        self.score = 0
        self.rect = score_rect_1

    def update (self):
        if pygame.sprite.groupcollide (player_bullet,flying_v,True,True):
            self.score += 1
        if self.score >= len (self.images):
            self.score = 0
        self.image = self.images [self.score]

class Score_cards_2 (pygame.sprite.Sprite):
    def __init__ (self):
        pygame.sprite.Sprite.__init__ (self)
        self.images = []
        self.images.append (get_image ("number_0.png").convert_alpha())
        self.images.append (get_image ("number_1.png").convert_alpha())
        self.images.append (get_image ("number_2.png").convert_alpha())
        self.images.append (get_image ("number_3.png").convert_alpha())
        self.images.append (get_image ("number_4.png").convert_alpha())
        self.images.append (get_image ("number_5.png").convert_alpha())
        self.images.append (get_image ("number_6.png").convert_alpha())
        self.images.append (get_image ("number_7.png").convert_alpha())
        self.images.append (get_image ("number_8.png").convert_alpha())
        self.images.append (get_image ("number_9.png").convert_alpha())
        self.score = 0
        self.rect = score_rect_2

    def update (self):
        if pygame.sprite.groupcollide (player_bullet, flying_v,True,True):
            self.score += 1
        if self.score >= len (self.images):
            self.score = 0
        self.image = self.images [self.score]
...

global score_cards
score_cards = pygame.sprite.Group ()
score_cards.add (Score_cards_1())
score_cards.add (Score_cards_2())

...

while not done:

    ...
    score_cards.update ()
    ...
    score_cards.draw ()
    ...

我知道得分没有被保存或任何东西,但我想让简单的部分先工作。

1 个答案:

答案 0 :(得分:0)

您正在尝试向组添加类,您必须使用这些类创建对象,然后将它们添加到组

score_card1_object = Score_Cards_1()
score_card2_object = Score_Cards_2()
score_cards = pygame.sprite.Group()
score_cards.add(score_card_object1)
score_cards.add(score_card_object2)