0

我需要在 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()

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

4

1 回答 1

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)

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

于 2012-12-08T22:49:32.200 回答