2015-08-15 83 views
-3
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 

遊戲主循環:NameError:名稱'clean_up_bubs'沒有定義,即使它是?

while True: 
    if randint(1, BUB_CHANCE) == 1: 
     create_bubble() 
    move_bubbles() 
    clean_up_bubs() 
    window.update() 
    sleep(0.01) 
    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) 
+0

下面有已定義的事: –

+0

DEF clean_up_bubs(): 爲i的範圍(LEN(bub_id)-1,-1,-1): X,Y = get_coords(bub_id [I]) 如果x <-GAP: del_bubble(i) –

+0

但它一直說我沒有定義它 –

回答

1

你打電話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) 
+0

確定il試試吧謝謝:) –

+0

現在我得到這個錯誤:(追蹤(最近呼叫最後): 文件「C:/ Users/anush/Documents/Python Stuff/Bubble Blaster 2 fix.py」,行54, if randint(1,BUB_CHANCE)== 1: NameError:名稱'BUB_CHANCE'沒有定義,你可以告訴我在python中很糟糕 –

+0

'BUB_CHANCE'應該是什麼? – Cyphase