Making rectangle move across screen with graphics file

半世苍凉 提交于 2019-12-20 07:06:30

问题


from graphics import *

def main():
    win = GraphWin("Polygon", 500, 500)
    r= Rectangle(Point(10,500),Point(150,450))
    r.draw(win)
    while r.getP1()<=450 is False:
        rectMaker(r)
        time.sleep(1)

def rectMaker(r):
    r.undraw(win)
    r=Rectange(Point(r.getP1()+1),(Point(r.getP2()+1)))
    r.draw(win)
    return r




#win.getMouse()


main()

Why is my rectangle not moving anyone see anything? And I don't think I'm suppose to use sleep in case anyone was going to suggest that.

Any help is appreciated please and thank you


回答1:


It looks like you are having an odd interaction between a Point and an Integer.

Namely this line here:

r=Rectange(Point(r.getP1()+1),(Point(r.getP2()+1)))

You are trying to add 1 to a Point.

This is likely not doing what you expect it to - try re-writing this so that you get the original P1/P2 values and then increment the specific x/y as necessary.

Additionally, Rectangle is spelt incorrectly, you should probably be creating a new Rectangle and returning it rather than overriding the existing one (may also cause issues), and you could've tried debugging this by adding a print(r.getP1() + " " + r.getP2()) before and after assignment.

Another addendum: Your functions should ideally only have one purpose. rectMaker seems to not just make rectangles, but re-draw them, try to change an existing rectangle and then return it. The usual style for games is something like

def main():
     load()
     while(playing):
         input()
         update()
         draw()
     unload()


来源:https://stackoverflow.com/questions/20110062/making-rectangle-move-across-screen-with-graphics-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!