Reputation: 571
I don't get what's wrong. I'll post the part of the code that's relevant.
Error:
Traceback (most recent call last):
File "C:\Python\pygame\hygy.py", line 104, in <module>
check_action()
File "C:\Python\pygame\hygy.py", line 71, in check_action
check_portal()
File "C:\Python\pygame\hygy.py", line 75, in check_portal
if [actor.x - 16, actor.y - 16] > portal[i][0] and [actor.x + 16, actor.y + 16] < portal[i][0]:
TypeError: tuple indices must be integers
function:
def check_portal():
for i in portal:
if [actor.x - 16, actor.y - 16] > portal[i][0] and [actor.x + 16, actor.y + 16] < portal[i][0]:
if in_portal == False:
actor.x,actor.y=portal[i][1]
in_portal = True
elif [actor.x - 16, actor.y - 16] > portal[i][1] and [actor.x + 16, actor.y + 16] < portal[i][1]:
if in_portal == False:
actor.x,actor.y=portal[i][1]
in_portal = True
else:
in_portal = False
initializing actor:
class xy:
def __init__(self):
self.x = 0
self.y = 0
actor = xy()
initializing portal:
portal = [[100,100],[200,200]],[[300,300],[200,100]]
Upvotes: 3
Views: 15412
Reputation: 9323
Within the for loop, i = ([100, 100], [200, 200])
, which is not a valid index for a list.
Given the comparison in the if statements, it looks like your intention was more like:
for coords in portal:
if [actor.x - 16, actor.y - 16] > coords[0] and [actor.x + 16, actor.y + 16] < coords[0]:
where coords[0] == [100, 100]
on the first iteration of the loop.
Upvotes: 0
Reputation: 2362
When you said for i in portal
, in each iteration, instead of the indices in portal
which you may think of, i
is actually elements of portal
. So it is not integer and causes error in portal[i][0]
.
So a quick fix is just replace that with for i in xrange(len(portal))
, in which i
is indices.
Upvotes: 1
Reputation: 601679
Given the initialisation of portal
, the loop
for i in portal:
...
will only do two iterations. In the first iteration, i
will be [[100,100],[200,200]]
. Trying to do portal[i]
will be equivalent to portal[[[100,100],[200,200]]]
, and this doesn't make sense. You probably just want to use i
instead of portal[i]
. (You probably want to rename it to something more meaningful than i
, too.)
Upvotes: 1