python - AttributeError:元组对象没有属性移动(OOP)

标签 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'

下面是主角的原始玩家玩家类。错误可能源自何处

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/

相关文章:

python - python中wasserstein距离函数的引用

python - 使用 SQLalchemy RDS 连接到 Python

python - 递归闭包中的作用域错误

python - pip3 install ruamel.yaml[jinja2] 破坏 ruamel.yaml

python - 更改 python 3.3.2 中的 shell 打印颜色

database - 设计模式 : Should a factory update and delete too?

oop - cakephp 覆盖子 Controller 中的构造

python - 为什么 pandas 不允许在 groupby 中使用分类列?

python-2.7 - ImportError : No module named 'skimage' , 但我安装了所有依赖项和 scikit-image

python - 在类内访问 `__attr` 时,名称修改如何工作?