Pygame组更新不接受参数

1 投票
2 回答
1012 浏览
提问于 2025-04-17 03:31

我在其他地方也看过类似的问题,大家都说只要在函数定义里加上'self'就可以了。但是我检查了一下文件,发现它的确已经在最前面有'self'这个关键词了!这是错误信息:

Traceback (most recent call last):
  File "C:\Users\Brenda\Documents\The Nick Folder\Mobile Fortress War 3\MFWRT3 - TileClass test\Title.pyw", line 142, in <module>
    SelectServer.main()
  File "C:\Users\Brenda\Documents\The Nick Folder\Mobile Fortress War 3\MFWRT3 - TileClass test\SelectServer.pyw", line 44, in main
    Main.mainloop()
  File "C:\Users\Brenda\Documents\The Nick Folder\Mobile Fortress War 3\MFWRT3 - TileClass test\Main.pyw", line 72, in mainloop
    globals.alltiles.update()
  File "C:\Python32\lib\site-packages\pygame\sprite.py", line 462, in update
    s.update(*args)
TypeError: update() takes no arguments (1 given)

我调用它是这样做的:

globals.alltiles.update()

有没有人能帮帮我?

2 个回答

1

我来得有点晚,但可能是因为每张卡片的更新方法声明不正确(我猜这些卡片是从pygame.sprite.Sprite这个类派生出来的)。

这个方法应该写成“def update(self)”,而不是“def update()”。

0

没有代码可以调试的话,就只能猜了。不过为了将来参考,精灵组是这样创建的:

#first create a sprite class
class Card(pygame.sprite.Sprite):
    def __init__(self,img,pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join('img','cards', img))
        self.rect = self.image.get_rect()
        self.rect.center = pos

#then create a group
mycards = pygame.sprite.Group()

#then add a sprite to the group
holders.add(Card('ace_spade.jpg',coord))

撰写回答