在python3中循环拼凑

时间:2012-12-08 22:23:05

标签: python python-3.x

我需要在Python 3中创建拼凑。我要做的就是创建一个循环,使设计边框成为图形窗口。我知道我需要一个for循环但是我不知道该怎么做。

这是我到目前为止所做的:

from graphics import *

def main():
    height = eval(input("What is the height of the window"))
    width = eval(input("What is the width of the window"))
    colour = input("enter the colour of the patch")
    win = GraphWin("Patch", 100*width, 100*height)
    boat_x = 0
    boat_y = 0
    for x in range (4):
         boat(win, boat_x, boat_y, colour)
         boat_x = boat_x + 23
    for i in range(height * 5):
         boat(win, boat_x, boat_y, colour)
         boat_x = boat_x + 24
    for j in range(height * 5):
         boat(win, boat_x, boat_y, colour)
         boat_y = boat_y + 100
    win.getMouse()
    win.close()



def boat(win, x, y, colour):
    body1 = Polygon(Point(1+x,95+y), Point(5+x,100+y),
                Point(20+x,100+y), Point(24+x,95+y))
    body1.draw(win)
    line1 = Line(Point(13+x,95+y), Point(13+x,90+y))
    line1.draw(win)
    sail1 = Polygon(Point(1+x,90+y), Point(24+x,90+y), Point(13+x, 73+y))
    sail1.setFill(colour)
    sail1.draw(win)
    body2 = Polygon(Point(1+x, 63), Point(5+x, 68),
                        Point(20+x,68), Point(24+x,63))
    body2.draw(win)
    line2 = Line(Point(13+x,63), Point(13+x,58))
    line2.draw(win)
    sail2 = Polygon(Point(1+x,58), Point(24+x, 58), Point(13+x,40))
    sail2.setFill(colour)
    sail2.draw(win)
    body3 = Polygon(Point(1+x,28), Point(5+x,33),
                        Point(20+x,33), Point(24+x, 28))
    body3.draw(win)
    line3 = Polygon(Point(13+x,28), Point(13+x,23))
    line3.draw(win)
    sail3 = Polygon(Point(1+x,23), Point(24+x, 23), Point(13+x, 5))
    sail3.setFill(colour)
    sail3.draw(win)

main()

到目前为止,这创造了顶部边界,但没有别的。 我也知道船功能不是最有效的绘图方式

1 个答案:

答案 0 :(得分:0)

当你说你需要“使设计边框成为图形窗口”时,我假设你希望你的boat设计在窗口的每个边缘(即顶部,底部,左右)。

这应该在两个循环中可行。一个将绘制顶部和底部边缘,另外两个将绘制左右边缘。我不太确定你的绘图代码是如何工作的,所以我猜这里有一些补偿:

top = 0
bottom = (height-1) * 100

for x in range(0, width*100, 25):
    boat(x, top, colour)
    boat(x, bottom, colour)

left = 0
right = width * 100 - 25

for y in range(100, (height-1)*100, 100):
    boat(left, y, colour)
    boat(right, y, colour)

这应该在顶部和底部每隔25个像素调用boat子程序,沿左右边缘每100个像素调用一次。调整topbottomleftright值以及循环中range调用中的参数,以使间距适合您的需求(我只是做了)。此代码避免了两次绘制角项目,但取决于绘图例程的工作方式,这可能不是必需的。