class combattant(pygame.sprite.Sprite):
def __init__(self,img,posit):
pygame.sprite.Sprite.__init__(self)
self.image=marche[0]
self.image_pos=posit
self.face=0
def mov(self,direction):
if direction[K_LEFT]:
self.face=(self.face+1)%2
self.image_pos.x -= 1
self.image=marche[0+self.face]
print ('gauche')
if direction[K_RIGHT]:
print ("droit")
self.face=(self.face+1)%2
self.image_pos.x += 1
self.image=marche[2+self.face]
combattant.mov (tkey)
这里是我的问题,当我运行含有这种程序,我得到这个:为什么Python中考虑一类自身作为一个参数我不得不放弃
Traceback (most recent call last):
File "F:\ISN\essai 2.py", line 63, in <module>
combattant.mov (tkey)
TypeError: mov() takes exactly 2 arguments (1 given)
的Python似乎认为'自我'作为我需要付出努力才能发挥作用的论点。我曾尝试使用alpha函数或把自我参数放在空的空间,但当然我得到一个错误说'无效的语法'和alpha函数不会改变任何东西......也许我用这个错误的方式,因为我是一名初学者......如果有人能帮助我,这将是非常有帮助的!提前致谢 !
当你创建*实例*并调用方法时,'self'被绑定。如果你直接在类**上调用方法**,那么没有实例将*绑定到*。 –
您需要首先创建'combattant'的实例,而不是直接访问类中的方法。 –
可以帮助纠正这个错误? – user3154918