我尝试正常运行代码,但是遇到了类似这样的错误
Traceback (most recent call last):
File "H:\Coursework assets\gametest1.py", line 85, in <module>
wizard.move(wizard.x)
AttributeError: 'tuple' object has no attribute 'move'
下面是主角的原始玩家玩家类。错误可能源自何处
class player:
def __init__(self,x,y):
self.x = x
self.y = y
self.standing = True
self.left = False
self.right = True
self.vel = 15
def move(self,x,y):
if not(self.standing):
if k[pygame.K_LEFT] and self.x > 0 - 150:
self.left = True
self.right = False
self.x -= self.vel
elif k[pygame.K_RIGHT] and self.x < 500 - 150 :
self.right = True
self.left = False
self.x += self.vel
else:
self.standing = True
run = True
主游戏循环
wizard = (25,420)
while run:#main game loop
pygame.time.delay(15)
wizard.move(wizard.x,wizard.y)
win.blit(bg,(0,0))
wizard.jump(wizard.y)
wizard.draw(win))
pygame.display.update()
pygame.quit()
最佳答案
在游戏循环中,您创建的向导只是一个元组。将其创建为 wizard = player(25, 420)
此外,强烈建议将类名设为大写(Player
)。请参阅PEP 8更多Python社区普遍接受的编码风格建议。
此外,您不必在否定语句两边加上括号,就像 if not self.standing
一样。而且您可能实际上不希望 not
在那里,您希望在巫师站立时移动他,并在他不站立时抬起他...
关于python - AttributeError:元组对象没有属性移动(OOP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58901317/