Skip to main content
 首页 » 编程设计

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

2024年09月07日15langtianya

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

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

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

这是我的代码

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.