2 questions: why is 'x' undefined? And is there some standard elegant solution to 2d coordinate lookup that isn't this mess?
I am new to programming and learning on my own, and have weak research skills. Its hard to look something up when you don't know its name.
What I am trying to do: Make a game that uses 2-d coordinates (x,y). To accomplish that, I have created the following function that doesn't work. I know it doesn't work... I don't understand why. I can fix it by removing its flexibility of input, and creating duplicate functions which seems like a bad solution.
My [Python] code
class Wall:
def __init__(self, x="", y=""):
self.x = x
self.y = y
foo = []
foo.append(Wall(x=1, y=3))
def return_index(list_input, x_coord, y_coord, class_x, class_y):
for i in range(len(list_input)):
if list_input.class_x is x_coord:
if list_input.class_y is y_coord:
print("return i = ",i)
return (i)
print(foo[1].x)
return_index(list_input=foo, x_coord=1, y_coord=2, class_x=x, class_y=y)
error code:
Traceback (most recent call last):
File "E:/Programing Python/platformer/test.py", line 19, in <module>
return_index(list_input=foo, x_coord=1, y_coord=2, class_x=x, class_y=y)
NameError: name 'x' is not defined
Psudocode:
list with class instances
each class instance contains x & y coordinates.
(each x,y pair is unique and does not repeat)
go through list, i = current list index:
if list[i].class_x is x_coord:
if list[i].class_y is y_coord:
return [i] # return list index of class instance containing target x,y pair