你怎么会让乌龟重叠某些方块的区域?

时间:2018-04-30 18:32:18

标签: python recursion draw

所以我必须熟悉乌龟与学校的项目。除了重叠方块之外,我基本上得到了教授所要求的一切。 My squares

然而,他希望广场像这样重叠

Profsquares

我根本无法复制这个。我想知道他们是否需要投入我的代码以便轻松解决它。

这是我的代码

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.

我认为这与笔首先绘制正方形的位置有关。感谢您的任何意见!

2 个答案:

答案 0 :(得分:1)

听起来像是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.

答案 1 :(得分:1)

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

现在订单是下订单(访问所有孩子,然后访问节点)。

使用此特定订单(访问右上角的孩子,访问节点,访问剩余的孩子):

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)

您可以获得所需的模式。

相关问题