用乌龟造树(即右侧朝上)

2024-05-20 23:36:45 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次用乌龟,所以请容忍我。我想用python用turtle绘制一个树图。我做了这棵树,它看起来很完美,除了一个问题,这个问题看起来很简单,但是,当我打印出我的树时,它看起来像这样。在

enter image description here

那么我要加些什么使我的树朝上呢?这是我的密码。提前谢谢!在

import turtle

t = turtle.Turtle()


def tree(length = 100):

    if length < 10:
        return
    t.forward(length)
    t.left(30)
    tree(length *.7)
    t.right(60)
    tree(length * .7)
    t.left(30)
    t.backward(length)
    return


tree()

turtle.done()

Tags: importrighttree密码returnif树图def
1条回答
网友
1楼 · 发布于 2024-05-20 23:36:45

您必须记住,该函数是递归的,因此您需要将turtle从函数中分离出来。您可以在函数中使用函数,但在调用函数之前,我将在全局范围内对turtle进行调整:

t.left(90) # then call tree after with tree()

相关问题 更多 >