在我的游戏中,相关代码找到here,并且在本文末尾,在评估时会抛出此错误:
Traceback (most recent call last):
File "C:\Users\Benjamin\Documents\GitHub\Tarbeyon\level.py", line 60, in <module>
level.parseLevel() # Building the level
File "C:\Users\Benjamin\Documents\GitHub\Tarbeyon\level.py", line 47, in parseLevel
self.block[name] = Fountain((x, y), name)
File "C:\Python33\lib\site-packages\pygame\sprite.py", line 124, in __init__
self.add(*groups)
File "C:\Python33\lib\site-packages\pygame\sprite.py", line 142, in add
self.add(*group)
File "C:\Python33\lib\site-packages\pygame\sprite.py", line 142, in add
self.add(*group)
TypeError: add() argument after * must be a sequence, not int
[Finished in 2.8s]
我做错了什么?我可以为喷泉提供代码,这似乎是一个有问题的类。
喷泉类:
class Fountain(pygame.sprite.Sprite):
def init(self, pos, name):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.add(constant.blocks)
self.add(constant.fountains)
self.name = name
self.particles = pygame.sprite.Group()
self.image = pygame.image.load("Images" + os.sep + "blocks" + os.sep + "misc" + os.sep + "fountain1.png").convert()
self.rect = self.image.get_rect()
self.rect.x = pos[0]
self.rect.y = pos[1]
self.wImage = pygame.image.load("Images" + os.sep + "blocks" + os.sep + "misc" + os.sep + "fountain2.png").convert()
self.wBoundingBox = self.wImage.get_rect()
self.wBoundingBox.x = pos[0] + 25
self.wBoundingBox.y = pos[1]
self.waterOrigin = self.wBoundingBox.midbottom
self.particles = {}
create_particles()
def create_particles(self):
for i in range(10):
self.particles[i] = WaterParticle(self.name, self.wBoundingBox, self.waterOrigin)
def draw_particles(self, screen):
for particle in self.particles:
particle.draw(screen)
def update(self):
self.draw_particles()
答案 0 :(得分:2)
通过调用初始函数init
而不是__init__
,Sprite
类将使用((x, y), name)
调用。
这可能会导致内部混淆 - 其中一个“真实”参数可能会在您的参数不匹配的上下文中使用。
答案 1 :(得分:1)
您调用了方法“init
”而不是“__init__
”。