简体   繁体   English

使用递归绘制正方形图案

[英]Using recursion to draw a pattern of squares

So far this is the code I've written with the great help of my tutor.到目前为止,这是我在导师的大力帮助下编写的代码。

def square(t, x, y, side):
    t.up()
    t.goto(x-side/2, y+side/2)
    t.down()
    for i in range(4):
        t.forward(side)
        t.right(90)

def squares(t, x, y, size, n):
    if n == 0:
        return
    if n >= 1: 
        square(t, x, y, size)

        half = size / 2
        squares(t, x - half, y + half, size / 2.2, n - 1)
        squares(t, x + half, y + half, size / 2.2, n - 1)
        squares(t, x + half, y - half, size / 2.2, n - 1)
        squares(t, x - half, y - half, size / 2.2, n - 1)


s = Screen()
t = Turtle()
t.pensize(2)
squares(t, 0, 0, 200, 3)
s.exitonclick()

So this program will draw a square with 4 squares on each point and 4 squares on each of their points.所以这个程序将绘制一个正方形,每个点上有 4 个正方形,每个点上有 4 个正方形。 This was done within 1.5 hours of tutoring and my guy had to leave for family reasons and I couldn't finish this alone.这是在辅导后 1.5 小时内完成的,我的家伙因家庭原因不得不离开,我无法独自完成。 Here is what the assignment wants.这是任务想要的。

在此处输入图片说明

So what I want is a square with a square with a square with a square所以我想要的是一个正方形一个正方形一个正方形一个正方形

I can tell you got alot from your tutoring session...我可以告诉你从你的辅导课程中得到了很多......

just change只是改变

squares(t, 0, 0, 200, 3)

to

squares(t, 0, 0, 200, 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM