AttributeError:元组对象没有属性move(OOP)

时间:2019-11-17 14:12:03

标签: python python-3.x oop pygame attributeerror

我尝试正常运行代码,但是遇到了类似以下的错误

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'

下面是原始玩家的类主要角色的Player类。错误的来源可能是

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()

2 个答案:

答案 0 :(得分:2)

在游戏循环中,您创建的向导只是一个元组。将其创建为wizard = player(25, 420)

此外,强烈建议将类名大写(Player)。有关Python社区通常接受的更多编码风格建议,请参见PEP 8

此外,您不必在否定的语句两边加上括号,就像if not self.standing一样。而且您可能实际上不希望not在那儿,您想在向导站立时移动向导,并在向导不站立时举起他...

答案 1 :(得分:0)

假设您的向导是“玩家”类,则变量wizard的减速度是不正确的。

代码具有:

wizard = (25,420)

这只是使向导成为一对数字。 (称为“元组”。

我认为这应该是player实例,其xy参数分别为25、240〜

wizard = player( 25, 420 )

我假设您将函数player.jump()player.draw()遗漏在了代码之外,但是如果尚未编写函数,则主循环将以与您已经报告的错误相同的错误中断,除了{ {1}}首先。