Tk getcoords returns empty list

Viewed 45

I have a python program at https://pastebin.com/x7K2tBTG

My problem is when i run it, i get the following errors:

Traceback (most recent call last):
  File "some/file.py", line 88, in <module>
    clean_up_bubs()
  File "some/file.py", line 76, in clean_up_bubs
    x, y = get_coords(bub_id[i])
  File "some/file.py", line 65, in get_coords
    x = (pos[0] + pos[2]) / 2
IndexError: list index out of range

I've done some investigating and it turns out that at some point when the get_coords function is executed, the c.coords(id_num) gives an empty list. Why does it happen and how can i fix this?

1 Answers

You weren't deleting your bub_id key inside del_bubble() which made it iterate deleted bubbles. Also noticed your reversed loop was missing one bubble, not sure if intentional, but I changed it anyhow:

def del_bubble(i):
    del bub_r[i]
    del bub_speed[i]
    c.delete(bub_id[i])
    del bub_id[i]  # <-- New line

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

I like your game so far though! If you want additional tips then I recommend trying to create a Bubble class, should make it a lot easier to work with

Related