Pygame 零:TypeError:无法创建对“NoneType”对象的弱引用

问题描述

你好,我目前正在编写一个小 2D 游戏。我已经有运动等想通了。现在我想使用我在这里找到的东西添加一个简单的动画:

def anim_right():
    global counter_right
    imagesright = ["tesbold_right1_x1000.png","tesbold_right2_x1000.png"]
    Tesbold.image = imagesright[counter_right]
    counter_right = (counter_right + 1) % len(imagesright)
    print("anim_right ausgeführt")

我对每个主要方向都有这个。这也可以正常工作,唯一的例外是它引起癫痫发作,因为我在我的 update() 函数调用了这个函数。基本上每一帧都改变图像。

我正在考虑添加一个 clock.schedule_unique 但这似乎不起作用。

如果我像这样在动画函数本身中添加它:

def anim_right():
    global counter_right
    imagesright = ["tesbold_right1_x1000.png","tesbold_right2_x1000.png"]
    Tesbold.image = imagesright[counter_right]
    counter_right = (counter_right + 1) % len(imagesright)
    print("anim_right ausgeführt")
    clock.schedule_unique(anim_right(),0.5)

我收到以下错误代码

RecursionError: maximum recursion depth exceeded

如果我在像这样调用 anim_right() 函数函数中尝试它:

def update():               #Updates per Frame
    global walking
    if walking == True:
        if Tesbold.direction == 0:
            clock.schedule_unique(anim_right(),0.5)

我收到以下错误代码

TypeError: cannot create weak reference to 'nonetype' object

我已经搜索了两个错误代码,但没有找到对我的案例有用的内容

解决方法

您收到错误 “RecursionError: maximum recursion depth exceeded”,因为 import bcrypt code_hash = b'$2b$12$7H/JM5RD3dx1sQo3kICV3.ubESHcl3ZM5TbVwcGly9Nw0Rl1j4f6O' if bcrypt.checkpw(code.encode(),code_hash): print('Congrats you got it that is the end good job thanks for playing!!!') else: print('Nope go back a couple files to get the right answer.') 是一个函数调用语句。
anim_right() 实际上调用函数clock.schedule_unique(anim_right(),0.5) 并将返回值传递给anim_right。这会导致无限递归。

您必须传递函数本身(clock.schedule_unique 而不是 anim_right):

anim_right()

clock.schedule_unique(anim_right(),0.5)
,

您的代码可能依赖于从并不总是存在的对象创建存储信息。无论您要创建弱引用的对象是什么,要么弄清楚如何确保它始终被定义,要么以即使未定义对象时也能正常工作的方式编写程序。