2

我在理解colliderect如何与精灵一起工作时遇到了一些麻烦。我对此有一个好主意,但是每当我尝试将其实现到我的游戏中时,我都会收到错误消息“attributeError:'pygame.surface' object has no attribute 'rect'”

ufo_lvl_1 = pygame.image.load("ufo1.png") 
bullet = pygame.image.load("bullet.png")

class Bullet(pygame.sprite.Sprite):

    def __init__(self):
    # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self) 

        self.image = bullet
        self.damage = 5
        self.rect = self.image.get_rect()

    def update(self):
        if 1 == 1:
            self.rect.x += 15
        if self.rect.x >1360:
            self.kill()

class ufo1(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = ufo_lvl_1
        self.health = 10
        self.rect = self.image.get_rect()
    def update(self):
        if 1==1:
            self.rect.x  -= 10
            if self.rect.colliderect(bullet.rect):
                self.health -= bullet.damage
                if self.health >= 0:
                    self.kill()
                bullet.kill()            

基本上我所有的精灵都可以工作(不包括 ufo1),但是当我创建一个 ufo1 精灵时它崩溃了,我不知道如何修复它。

提前致谢。

4

1 回答 1

0

你调用了你的类 Bullet 和你的 Surface 类的子弹。当您调用 colliderect 时,您正在检查项目符号而不是 Bullet 或任何您命名的类 Bullet 对象

于 2013-03-01T00:11:09.227 回答