名称错误:名称“clean_up_bubs”未定义,即使它已定义?

2024-05-28 19:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

Traceback (most recent call last):
  File "C:/Users/anush/Documents/Python Stuff/Bubble Blaster 2.py", line 56, in <module>
    clean_up_bubs()
NameError: name 'clean_up_bubs' is not defined

主游戏循环:

^{pr2}$

Tags: cleanmostcallusersdocumentsfilelastup
1条回答
网友
1楼 · 发布于 2024-05-28 19:20:53

在第一个循环中定义它之前,您正在调用clean_up_bubs()。将函数定义移出while循环的上方。在

def get_coords(id_num):
    pos = c.coords(id_num)
    x = (pos[0] + pos[2])/2
    y = (pos[1] + pos[3])/2
    return x, y


def del_bubble(i):
    del bub_r[i]
    del bub_speed[i]
    c.delete(bub_id[i])
    del_bub_id[i]


def clean_up_bubs():
    for i in range(len(bub_id)-1, -1 , -1):
        x, y = get_coords(bub_id[i])
        if x < -GAP:
            del_bubble(i)


while True:
    if randint(1, BUB_CHANCE) == 1:
        create_bubble()
    move_bubbles()
    clean_up_bubs()
    window.update()
    sleep(0.01)

相关问题 更多 >

    热门问题