你如何让乌龟重叠某些正方形区域?

How would you get turtle to overlap certain areas of squares?

所以我不得不在学校的一个项目中熟悉乌龟。除了重叠正方形之外,我基本上得到了教授要求的一切。

但是他希望正方形像这样重叠

我根本无法复制这个。我想知道我是否需要将它们放入我的代码中才能轻松解决它。

这是我的代码

import turtle #Imports the 'turtle module' which allows intricate shapes and pictures to be drawn
my_turtle_pos = (10 , 10)
def square(my_turtle,x,y,length) : #I set up a function that helps me determine the square

    my_turtle.penup() #Picks 'up' the turtle pen
    my_turtle.setposition(x-length/2,y-length/2) #Helps set positon
    my_turtle.pendown() #Puts 'down' the turtle pen
    my_turtle.color('black','red') #Allows black outline, with red filling
    my_turtle.begin_fill() #Starts the filling of red and helps remember the starting point for a filled area
    my_turtle.forward(length) #Moves the turtle by the specified amount 'length'
    my_turtle.left(90) #Moves the turtle by given amount '90'
    my_turtle.forward(length)
    my_turtle.left(90)
    my_turtle.forward(length)
    my_turtle.left(90)
    my_turtle.forward(length)
    my_turtle.left(90)
    my_turtle.end_fill() #Stops filling with red, which will close with the current color

def graphic_pattern(my_turtle,x,y,length,times): #recursive function
    if times <= 0:  #This just tells us how many 'times' it needs to repeat till given amount
        return
    newSize = length/2.2 #This will grab the new size
    graphic_pattern(my_turtle,x-length/2,y-length/2,newSize,times-1) #Functions to help with writing 'smaller' squares
    graphic_pattern(my_turtle,x-length/2,y+length/2,newSize,times-1)
    graphic_pattern(my_turtle,x+length/2,y-length/2,newSize,times-1)
    graphic_pattern(my_turtle,x+length/2,y+length/2,newSize,times-1)
    square(my_turtle,x,y,length)


my_turtle = turtle.Turtle(shape="arrow") #You can use differen't shapes for the turtle, I chose arrow, though the turtle was cool :)
my_turtle.speed(100) #I am not sure how fast the turtle can go, I just chose 100 cause it went by quicker.

graphic_pattern(my_turtle,3,0,300,4) #Example pattern stated from homework assignment.

我认为这与笔先在何处绘制方块有关。感谢您的任何输入!

听起来这是您的 graphic_pattern()square() 方法的简单排序。看起来你想先画右上角 graphic_pattern(),然后是中间的方块,然后是其余的 graphic_pattern() 调用:

import turtle #Imports the 'turtle module' which allows intricate shapes and pictures to be drawn
my_turtle_pos = (10 , 10)
def square(my_turtle,x,y,length) : #I set up a function that helps me determine the square

    my_turtle.penup() #Picks 'up' the turtle pen
    my_turtle.setposition(x-length/2,y-length/2) #Helps set positon
    my_turtle.pendown() #Puts 'down' the turtle pen
    my_turtle.color('black','red') #Allows black outline, with red filling
    my_turtle.begin_fill() #Starts the filling of red and helps remember the starting point for a filled area
    my_turtle.forward(length) #Moves the turtle by the specified amount 'length'
    my_turtle.left(90) #Moves the turtle by given amount '90'
    my_turtle.forward(length)
    my_turtle.left(90)
    my_turtle.forward(length)
    my_turtle.left(90)
    my_turtle.forward(length)
    my_turtle.left(90)
    my_turtle.end_fill() #Stops filling with red, which will close with the current color

def graphic_pattern(my_turtle,x,y,length,times): #recursive function
    if times <= 0:  #This just tells us how many 'times' it needs to repeat till given amount
        return
    newSize = length/2.2 #This will grab the new size

    graphic_pattern(my_turtle,x+length/2,y+length/2,newSize,times-1)
    square(my_turtle,x,y,length)
    graphic_pattern(my_turtle,x-length/2,y-length/2,newSize,times-1)
    graphic_pattern(my_turtle,x-length/2,y+length/2,newSize,times-1)
    graphic_pattern(my_turtle,x+length/2,y-length/2,newSize,times-1)


my_turtle = turtle.Turtle(shape="arrow") #You can use differen't shapes for the turtle, I chose arrow, though the turtle was cool :)
my_turtle.speed(100) #I am not sure how fast the turtle can go, I just chose 100 cause it went by quicker.

graphic_pattern(my_turtle,3,0,300,4) #Example pattern stated from homework assignment.

您必须更改递归调用的访问顺序。

现在顺序是post-order(访问所有children,然后访问节点)。

按此顺序(访问右上child,访问节点,访问剩余children):

def graphic_pattern(my_turtle,x,y,length,times): #recursive function
    if times <= 0:  #This just tells us how many 'times' it needs to repeat till given amount
        return
    newSize = length/2.2 #This will grab the new size
    graphic_pattern(my_turtle,x+length/2,y+length/2,newSize,times-1)
    square(my_turtle,x,y,length)
    graphic_pattern(my_turtle,x-length/2,y-length/2,newSize,times-1) #Functions to help with writing 'smaller' squares
    graphic_pattern(my_turtle,x-length/2,y+length/2,newSize,times-1)
    graphic_pattern(my_turtle,x+length/2,y-length/2,newSize,times-1)

可以获得想要的图案