Python3:矩形之间的距离不断变化

时间:2018-09-17 02:56:15

标签: python python-3.x turtle-graphics

创建矩形,它们之间的距离相等,但高度和宽度随机。但是它们之间的距离并不固定并且会改变。

    import TurtleHelper
    import turtle
    import random as rd


    def drawRectangle(myTurtle, width, height): 
        for i in range(2):
            myTurtle.fd(width)
            myTurtle.left(90)
            myTurtle.fd(height)
            myTurtle.left(90)

    def drawRectangleagain(myTurtle):       
        myTurtle.up()
        myTurtle.forward(100)
        myTurtle.down()





def main():
    window=TurtleHelper.initWindow()
    t=turtle.Turtle()
    t.pencolor("blue")



    for i in range(3):
        drawRectangle(t,rd.randint(20, 60), rd.randint(20, 110))
        drawRectangleagain(t)


    window.exitonclick()
main()

1 个答案:

答案 0 :(得分:0)

Rectangle ABCD

您从顶点D开始,在顶点D结束。因此,当您向前移动100时,一个矩形与下一个矩形之间的距离为100 - DC。由于DC是随机数,因此矩形之间的距离是随机数。

您可以获得以下一致的间距

def drawRectangle(myTurtle, width, height): 
        for i in range(2):
            myTurtle.fd(width)
            myTurtle.left(90)
            myTurtle.fd(height)
            myTurtle.left(90)
        myTurtle.up()
        myTurtle.fd(width)