如何迭代类中的函数定义?
class Big:
def __init__(self):
self.win = GraphWin("Bounce", 500, 500)
self.win.setCoords(-10, -10, 10, 10)
#self.win.getMouse()
def close(self):
self.win.getMouse()
def ball(self):
x = 1
y = 1
#self.win = win
circ = Circle(Point(x, y), 0.25)
circ.draw(self.win)
def move(self):
movement = [0.5, 0.3]
x += movement[0]
y += movement[1]
我想在函数move中更改x和y的值。 我怎样才能做到这一点?然后再次创建对象Circle
答案 0 :(得分:2)
在python中,您必须像在self.win中那样在 init 中声明私有成员。
尝试在其中添加self.x = 0并使用self.x随处访问
答案 1 :(得分:1)
class Ball:
def __init__(self,x,y,rad):
self.root = root
self.x = x
self.y = y
def draw(self,target):
Circle(Point(X,y),rad).draw(target)
然后只是使用..
class Big:
def __init__(self):
self.win = GraphWin("Bounce", 500, 500)
self.win.setCoords(-10, -10, 10, 10)
self.ball = Ball(1,1,0.25)
#self.win.getMouse()
def close(self):
self.win.getMouse()
def draw(self):
self.ball.draw(self.win)
def move(self):
movement = [0.5, 0.3]
self.ball.x += movement[0]
self.ball.y += movement[1]