I am making a game which requires many x, y, and z coordinates and they all have a function to themselves. I need to use the x or y function inside another function to make it run through that function. I am not trying to def the function inside it, but only to put if in front of it and make that code run. They are both in the same class but different indentations.
Can some tell me why I can't rerun that code inside another function without getting an
unresolved reference.
Here is a snippet of my code.
def within_y(co1,co2):
if (co1.y1 > co2.y1 and co1.y1 < co2.y2) \
or (co1.y2 > co2.y1 and co1.y2 < co2.y2) \
or (co2.y1 > co1.y1 and co2.y1 < co1.y2) \
or (co2.y2 > co1.y1 and co2.y2 < co1.y1):
return True
else:
return False
def collided_left(co1, co2):
if within_y(co1, co2):
if co1.x1 <= co2.x2 and co1.x1 >= co2.x1:
return True
return False
In this case, the
if within_y(co1, co2):returns the error
Thank you guys, I am still learning syntax and basic python.
Here is my new code.
class Coords:
def __init__(self, x1=0,y1=0, x2=0, y2=0):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def within_x(co1, co2):
if (co1.x1 > co2.x1 and co1.x1 < co2.x2) \
or (co1.x2 > co2.x1 and co1.x2 < co2.x2) \
or (co2.x1 > co1.x1 and co2.x1 < co1.x2):
return True
else:
return False
def within_y(co1,co2):
if (co1.y1 > co2.y1 and co1.y1 < co2.y2) \
or (co1.y2 > co2.y1 and co1.y2 < co2.y2) \
or (co2.y1 > co1.y1 and co2.y1 < co1.y2) \
or (co2.y2 > co1.y1 and co2.y2 < co1.y1):
return True
else:
return False
def collided_left(co1, co2):
if within_y(co1, co2):
if co1.x1 <= co2.x2 and co1.x1 >= co2.x1:
return True
return False
def collided_right(co1, co2):
if within_y(co1, co2):
if co1.x2 >= co2.x1 and co1.x2 <= co2.x2:
return True
return False
def collided_top(co1, co2):
if within_y(co1,co2):
if co1.y1 <= co2.y2 and co1.y1 >= co2.y1:
return True
return False
def collided_bottom(y, co1, co2):
if within_x(co1, co2):
y_calc = co1.y2 + y
if y_calc >= co2.y1 and y_calc <= co2.y2:
return True
return False
I have another class that does all the visuals and is unneeded in this question.
I also forgot to say that this is a stickman game where you need to jump the stickman around some platforms to get to a door.
collided_leftby one space, as it is in your posted code? If so, then calling it outside ofwithin_ywill fail.