游戏图像属性

2024-10-03 23:30:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的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

Tags: rectimageself游戏initdefpacmanpygame
1条回答
网友
1楼 · 发布于 2024-10-03 23:30:15

除去所有不相关的位,您看到以下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:看起来像的线条

^{pr2}$

我觉得很可疑。如果remove是一个方法,它将被food.remove()调用,food.remove不会做任何事情。在

相关问题 更多 >