Python Recursive Turtle function that draws capital I's
Meant to draw this with turtle using recursion:
But I'm really bad at this. This is what I'm getting:
Here's the code: https://gyazo.com/24cebddbb111506fd6959bb91dadb481
import turtle
def draw_shape(t, level,size):
if level == 1: #draws an I
t.down()
t.forward(size/2)
t.left(90)
t.forward(size/2)
t.back(size)
t.forward(size/2)
t.left(90)
t.forward(size)
t.left(90)
t.forward(size/2)
t.back(size)
t.up()
t.forward(size/2)
t.left(90)
t.forward(size/2)
else:
draw_shape(t,level - 1,size)
t.back(size/2)
t.right(90)
t.forward(size/2)
t.left(90)
draw_shape(t,level - 1,size/2)
t.left(90)
t.forward(size)
t.right(90)
draw_shape(t,level-1,size/2)
t.right(90)
t.forward(size/2)
t.left(90)
t.forward(size)
t.right(90)
t.forward(size/2)
t.left(90)
draw_shape(t,level-1,size/2)
t.left(90)
t.forward(size)
t.right(90)
draw_shape(t,level-1,size/2)
def q1():
my_win = turtle.Screen()
my_turtle = turtle.Turtle()
my_turtle.speed(0.006)
my_turtle.left(90)
my_turtle.up()
my_turtle.back(200)
n = int(input('Draw shape at level: '))
draw_shape(my_turtle,n,200)
my_win.exitonclick()
q1()
Pretty much only meant to edit the draw_shape() function. I'm getting it right at level 2 but the rest of the levels start to go wonky and draw wrong sized I's in the wrong positions and I think it's because of where my pointer is placed after the I's are drawn. Any help would be greatly appreciated.
I see two issues with your code as you have it now.
The first is that the lower part of the code doesn't put the turtle back at the center point after it's done. This is why the smaller bits get drawn in various random locations, since the previous calls left the turtle somewhere odd.
The second issue is that you're using a recursive call right at the start. That's not really necessary, since you're going to be moving along the I
shape later anyway.
What's I'd suggest is making your base case level < 1
. That doesn't require you to do anything (you can just return
immediately). This replaces your whole if level == 1
block (except for t.down()
which you need to keep).
def draw_shape(t, level, size):
if level < 1: # base case
return
t.down() # put pen down at the start, don't recurse immediately
t.back(size/2)
t.right(90)
t.forward(size/2)
t.left(90)
draw_shape(t, level - 1, size / 2)
t.left(90)
t.forward(size)
t.right(90)
draw_shape(t, level - 1, size / 2)
t.right(90)
t.forward(size / 2)
t.left(90)
t.forward(size)
t.right(90)
t.forward(size / 2)
t.left(90)
draw_shape(t, level - 1, size / 2)
t.left(90)
t.forward(size)
t.right(90)
draw_shape(t, level - 1, size / 2)
t.right(90) # add lines below to get back to start position
t.forward(size / 2)
t.left(90)
t.back(size / 2)
You could probably simplify this slightly by picking up the pen and skipping from one end of the I to another (eg from bottom left to top left) rather than drawing over the middle bar an extra time. I kept most of your code to make the changes that are necessary (rather than just nice) more clear.
链接地址: http://www.djcxy.com/p/80706.html上一篇: Python Turtle:绘制左嵌套正方形的函数
下一篇: Python递归海龟函数,它吸引我的资本