简体   繁体   English

如何使用Zelle的'graphics.py'程序正确创建移动形状的函数?

[英]How to properly create function that moves a shape using Zelle's 'graphics.py' program?

Question 17 of Ch. 章的问题17 6 in Zelle's 'Programming Python' books asks for a function that takes two arguments: 'shape', and 'newCenter', which will then move, or re-draw, an existing object based on the new points provided by a mouse-click. Zelle的“ Programming Python”(编程Python)书籍中的第6条要求一个带有两个参数的函数:“ shape”和“ newCenter”,然后它们将根据鼠标单击提供的新点移动或重画现有对象。 。

I've been able to figure out how to do it if I add two more parameters to the function, "myX", and "myY", so I can then subtract the difference for the move method. 如果我在函数“ myX”和“ myY”中添加了两个以上的参数,我就能弄清楚该怎么做,因此我可以减去move方法的差值。 What is eluding me is how to perform the same calculations using just the two parameters specified above. 让我难以理解的是如何仅使用上面指定的两个参数来执行相同的计算。 Here is my code: 这是我的代码:

def moveTo(shape, newCenter, myX, myY):
    myShape = shape
    myNewX = newCenter.getX()
    myNewY = newCenter.getY()
    myXUpd = myNewX - myX
    myYUpd = myNewY - myY
    myShape.move(myXUpd, myYUpd)
    return myNewX, myNewY

def main():
    win = GraphWin("My Graph Win", 500, 500)
    win.setBackground("white")
    win.setCoords(0, 0, 10, 10)
    Text(Point(5, 8.5), "Please click 10 times.").draw(win)
    myPoint = win.getMouse()
    myX = myPoint.getX()
    myY = myPoint.getY()
    myCircle = Circle(myPoint, 2)
    myCircle.draw(win)

    for x in range(1, 10):
        myNewPoint = win.getMouse()
        myX, myY = moveTo(myCircle, myNewPoint, myX, myY)

    win.close()

Also, any general tips for streamlining, style, or structure are also appreciated as I'm a rather new Python developer. 此外,由于我是一位相当新的Python开发人员,因此也对任何有关简化,样式或结构的常规技巧都表示赞赏。

Thanks! 谢谢!

I figured this out on my own, so anyone going through the Zelle "Python Programming" book can learn from my experience. 我自己解决了这个问题,因此,凡是通过Zelle“ Python编程”一书的人都可以从我的经验中学到东西。 I just pulled the X and Y out of the shape object I was passing in, thus saving myself having to pass them in again separately. 我只是将X和Y从要传递的形状对象中拉出,因此省去了自己不得不分别再次传递它们的麻烦。 Here is the updated code with some of the fat trimmed down. 这是更新后的代码,其中一些内容已精简。

def moveTo(shape, newCenter):
    oldCenter = shape.getCenter()
    myOldX, myOldY = oldCenter.getX(), oldCenter.getY()
    myNewX, myNewY = newCenter.getX(), newCenter.getY()
    moveX = myNewX - myOldX
    moveY = myNewY - myOldY
    shape.move(moveX, moveY)
    return shape

def main():
    win = GraphWin("My Graph Win", 500, 500)
    win.setBackground("white")
    win.setCoords(0, 0, 10, 10)
    Text(Point(5, 8.5), "Please click 10 times.").draw(win)
    myPoint = win.getMouse()
    myX, myY = myPoint.getX(), myPoint.getY()
    myShape = Circle(myPoint, 2)
    myShape.draw(win)

    for x in range(1, 10):
        newCenter = win.getMouse()
        myShape = moveTo(myShape, newCenter)


    win.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM