我希望在屏幕上移动一个矩形
我想更改制作矩形相对点的x坐标 如果你有更好的方式我愿意接受建议
我在没有做任何事情的坐标前面添加了'Point'这个词
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=Rectangle((r.getP1.getY(),r.getP1().getX()+1),(r.getP2.getY,r.getP2().getX()+1))
r.draw(win)
return r
main()
新代码
from graphics import *
def main():
win = GraphWin("Polygon", 500, 500)
r= Rectangle(Point(10,500),Point(150,450))
r.draw(win)
for i in range (0,345,1):
r.undraw()
r= Rectangle(Point(10+i,500),Point(150+i,450))
r.draw(win)
main()
好的新代码再次对不起我有完整的帆船。我希望这条线从船的顶部开始,而不是在底部,三角形有点混乱。我认为我的坐标是正确的,但我认为它们一定是错的。
from graphics import *
def main():
win = GraphWin("Polygon", 500, 500)
r= Rectangle(Point(10,500),Point(150,450))
r.draw(win)
l=Line(Point(70,450),Point(70,400))
l.draw(win)
p=Polygon(Point(10,450),Point(130,400),Point(70,300))
p.draw(win)
for i in range (0,345,1):
r.undraw()
l.undraw()
p.undraw()
l=Line(Point(70+i,500),Point(70+i,400))
r= Rectangle(Point(10+i,500),Point(150+i,450))
p= Polygon(Point(10+i,450),Point(130+i,400),Point(70+i,300))
r.draw(win)
l.draw(win)
p.draw(win)
main()
答案 0 :(得分:0)
现在矩形正在移动:
from graphics import *
def main():
win = GraphWin("Polygon", 500, 500)
r = Rectangle(Point(10,500), Point(150,450))
r.draw(win)
while r.getP1().getY() > 150:
r = rectMaker(win, r)
time.sleep(1)
def rectMaker(win, r):
r.undraw() # win
r = Rectangle( Point(r.getP1().getX(), r.getP1().getY()-10), Point(r.getP2().getX(),r.getP2().getY()-10) )
r.draw(win)
return r
main()
win
,但它不是全局变量undraw()
不需要参数getP1
和getY
,但getP1()
,getY()
Point
Rectangle
Point(Y,X)
,但应Point(X,Y)
rectMarker
不使用来自r
的原始main
,而是其克隆,因此您忘记将rectMaker()
的结果分配给oryginal r
({ {1}})更好的版本:
r = rectMaker(win, r)