I'm making a snake game, and for my collision detection, I have an if statement checking if the list of lists that contains coordinates of each square contains identical coordinates. If it finds a duplicate, the snake has overlapped itself.
The way I tried to check this is with the following:
if len(test_data) >= 2:
print('snake is',len(test_data))
if len(test_data) != len(set(test_data)):
print('within oneself')
I get the following:
[[300, 120], [300, 100], [300, 80], [300, 60], [300, 40], [300, 60], [300, 80], [300, 100], [300, 120], [300, 140]]
---
if len(test_data) != len(set(test_data)):
TypeError: unhashable type: 'list'
I've seen this work with ordinary values in a list however, my game uses lists. Furthermore, if this works with tuples, I can't convert to tuples in a list as my whole program is centered around the list in lists. I've seen some solutions like using in
but I don't understand that in python yet. Any help would be appreciated.
if len(test_data) >= 2:
no_head = test_data
no_head.pop(length-1)
n=0
for x in range(len(no_head)):
if test_data[length-1] == no_head[n]:
print('within oneself')
else:
pass
n += 1
this is another way I did it, where it checks if the head of the snake (last item in the list) is equal to an item of the list, then it cycles through the list checking. but it didn't work