【问题标题】:AttributeError: Tuple object has no attribute move(OOP)AttributeError:元组对象没有属性移动(OOP)
【发布时间】:2020-03-13 00:58:45
【问题描述】:

我尝试正常运行代码,但是遇到了一个看起来像这样的错误

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(45,420)'

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


【解决方案1】:

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

此外,强烈建议将类名设为大写 ​​(Player)。有关 Python 社区普遍接受的更多编码风格建议,请参阅PEP 8

此外,您不必在否定语句周围加上括号,就像if not self.standing 一样。而且您可能实际上不希望出现not,您想在他站立时移动巫师,并在他不站立时将他举起......

【讨论】:

    【解决方案2】:

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

    代码有:

    wizard = (25,420)
    

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

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

    wizard = player( 25, 420 )
    

    我假设您将函数 player.jump()player.draw() 留在了代码之外,但如果尚未编写它们,您的主循环将中断,并出现您已经报告的几乎相同的错误,除了首先是 .jump()

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 2021-07-30
      • 2013-07-29
      • 2020-08-29
      • 2015-04-22
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多