0

我正在尝试使用 pygame 创建一个 pacman 游戏,但遇到了一些问题。它说“食物”没有形象。

这是我的吃豆人游戏[代码已编辑]。

问题是这个区域出了点问题,它告诉我食物没有属性图像

class Food(pygame.sprite.Sprite):
    def __init__(self,x,y,color):
        pygame.sprite.Sprite.__init__(self)

        pygame.image = pygame.Surface([7,7])
        self.image.fill(color)

        self.rect = self.image.get_rect()
        self.rect.top = y
        self.rect.left = x
    def update (self,player):
        collidePlayer = pygame.sprite.spritecollide(self,player,False)
        if collidePlayer:
            food.remove
4

1 回答 1

1

去掉所有不相关的部分,你看到下面 Sprite 子类的__init__方法之间的区别了吗?

class Wall(pygame.sprite.Sprite): 

    def __init__(self,x,y,width,height, color):#For when the walls are set up later
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([width, height]) # <-------------
        self.image.fill(color)

class player(pygame.sprite.Sprite):

    def __init__ (self,x,y):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([13,13])  # <-------------
        self.image.fill(white) 

class Food(pygame.sprite.Sprite)
    def __init__(self,x,y,color):
        pygame.sprite.Sprite.__init__(self)

        pygame.image = pygame.Surface([7,7])  # <----- this one is different!
        self.image.fill(color)

您收到错误提示的原因是self没有image属性是因为您没有设置self.image,您将图像存储在pygame模块本身中。

PS:看起来像的线条

        food.remove

对我来说似乎很可疑。Ifremove是一个方法,它会被 调用food.remove(),并且food.remove不会做任何事情。

于 2012-08-22T02:24:23.480 回答