使用图形文件使矩形在屏幕上移动 [英] Making rectangle move across screen with graphics file

查看:47
本文介绍了使用图形文件使矩形在屏幕上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

感谢任何帮助,谢谢

推荐答案

看起来您在 Point 和 Integer 之间进行了奇怪的交互.

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

即这里的这一行:

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

您正在尝试将 1 添加到 Point.

You are trying to add 1 to a Point.

这可能没有达到您的预期 - 尝试重新编写它,以便您获得原始 P1/P2 值,然后根据需要增加特定的 x/y.

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.

此外,Rectangle 拼写错误,您可能应该创建一个新的 Rectangle 并返回它而不是覆盖现有的(也可能导致问题),并且您可以尝试通过添加一个 print(r.getP1() + " " + r.getP2()) 赋值前后.

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.

另一个附录:理想情况下,您的函数应该只有一个用途.rectMaker 似乎不只是制作矩形,而是重新绘制它们,尝试更改现有的矩形然后返回它.游戏的通常风格是这样的

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()

这篇关于使用图形文件使矩形在屏幕上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆