I am making a small game in Python using pythonista on my ipad.
I made a vector class that contains the coordinates and several functions to add, get a length, set a length. I have another class called Game in which I have my game variables and functions. I can define a vector lets say
self.pos=vector(200,200)
But if I want to work out the length, I can't call the getlength function because I'm not in the right class.
Example (I've taken out most of the code):
class vector(objet):
def __init(self,x,y):
self.x=x
self.y=y
def getlength(self):
return sqrt(self.x**2+self.y**2)
def addvec(self,a,b):
return vector(a.x+b.x,a.y,b.y)
class Game(object):
def __init__(self):
self.pos=vector(200,200)
self.pos=vector(200,200)
def loop(self):
## here i want something like d= length of self.pos !!
class MyScene(Scene):
def setup(self):
self.game=Game()
def draw(self):
self.game.loop()
run(MyScene())
Thanks, Nicolas
EDIT : the call
sum=addvec(self.pos,self.pos2)
obviously doesn't work because self is a Game class. How can I do it?
self.pos.getlength? You neither want nor need it to be a global function - it's a useful instance method.getlengthmethod?__initshould be__init__objetshould beobject