精华内容
下载资源
问答
  • #drawtree.pyfrom turtle importTurtle, mainloopdeftree(plist, l, a, f):"""plist is list of pensl is length of brancha is half of the angle between 2 branchesf is factor by which branch is shortenedfrom...

    #drawtree.py

    from turtle importTurtle, mainloopdeftree(plist, l, a, f):"""plist is list of pens

    l is length of branch

    a is half of the angle between 2 branches

    f is factor by which branch is shortened

    from level to level."""

    if l > 5: # lst =[]for p inplist:

    p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.

    q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.

    p.left(a) #Turn turtle left by angle units

    q.right(a)#turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.

    lst.append(p)#将元素增加到列表的最后

    lst.append(q)

    tree(lst, l*f, a, f)defmain():

    p=Turtle()

    p.color("green")

    p.pensize(5)#p.setundobuffer(None)

    p.hideturtle() #Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing,

    #because hiding the turtle speeds up the drawing observably.

    #p.speed(10)

    #p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.

    p.speed(10)#TurtleScreen methods can then be called for that object.

    p.left(90)#Turn turtle left by angle units. direction 调整画笔

    p.penup()#Pull the pen up – no drawing when moving.

    p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.

    p.pendown()#Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画

    #否则turtle一移动就会自动的把线画出来

    #t = tree([p], 200, 65, 0.6375)

    t = tree([p], 200, 65, 0.6375)

    main()

    展开全文
  • #利用turtle模块画一棵树,包括枝干和树叶,并涂上颜色 import turtle import random turtle.screensize(10000,10000) turtle.Turtle().screen.delay(0) #取消延迟,有奇效 p=turtle.Pen() p.speed(0) #笔尖速度调至...
    #利用turtle模块画一棵树,包括枝干和树叶,并涂上颜色
    import turtle
    import random
    turtle.screensize(10000,10000)
    turtle.Turtle().screen.delay(0)               #取消延迟,有奇效
    p=turtle.Pen()
    p.speed(0)                                    #笔尖速度调至最大
    p.color('Goldenrod4')                         #树干颜色
    p.seth(90)                                    #调整初始角度
    pos=[[],[],[],[],[],[],[],[],[],[]]           #记录枝干节点位置
    pos[0].append(p.pos())                        #将枝干节点位置存储到列表中
    for i in range(6):                            #枝干层数
        for pos_ in  pos[i]:
            for j in range(round((3/2)**i)):      #每个枝干节点的枝干数
                p.penup()                         #提笔并在枝干节点之间移动
                p.goto(pos_)
                p.pendown()
                p.width(60*(4/7)**i)              #枝干宽度
                p.forward(200*(11/12)**i)          #枝干长度
                pos[i+1].append(p.pos())          #在列表中添加枝干接点
                a=random.randint(-75,75)          #设置枝干方向
                p.seth(a+90)
    p.color('green')                              #叶子颜色
    p.seth(90)
    for i in range(4,7):
        for pos_ in  pos[i]:
            for j in range(10):      #每个枝干节点处的叶子数
                p.penup()
                p.goto(pos_)
                p.pendown()
                p.width(20*(4/5)**i)              #叶片宽度
                p.forward(40)                     #叶片长度
                a=random.randint(-90,90)          #叶片方向
                p.seth(a+90)
    turtle.mainloop()                             #显示绘图窗口

    画出的树样例:

    展开全文
  • 点击上方蓝字关注我,我们一起学编程有任何疑问或者想看的内容,欢迎私信很久没有更新了,今天登录到公众平台看,竟然涨了不少粉丝,真是开心!今天天气不错,想必各位秋招的小伙伴们也都收到了个满意的 offer ...

    点击上方蓝字关注我,我们一起学编程有任何疑问或者想看的内容,欢迎私信

    很久没有更新了,今天登录到公众平台一看,竟然涨了不少粉丝,真是开心!

    今天天气不错,想必各位秋招的小伙伴们也都收到了一个满意的 offer 了吧!今天不分享编程,今天我要分享一朵花,会开的一朵花!

    直接上代码:

    from turtle import *
    from random import *
    from math import *

    def tree(brnch, l):
        pd()    #下笔
        t = cos(radians(heading() + 45)) / 8 + 0.25    # 方向越往两边偏颜色越浅
        pencolor(t, t, t)     # 画笔颜色
        pensize(brnch / 3)    # 画笔尺寸
        forward(l)            # 向前画

        if brnch > 0:    # 画树枝
            right_angle = random() * 15 + 10             # 右树枝偏转角度
            left_angle = random() * 15 + 10              # 左树枝偏转角度
            next_length = l * (random() * 0.25 + 0.7)    # 下一段树枝的长度
            #右转一定角度,画右分支
            right(right_angle)
            tree(brnch - 1, next_length)
            #左转一定角度,画左分支
            left(right_angle + left_angle)
            tree(brnch - 1, next_length)
            #转回来
            right(left_angle)
        else:    # 画叶子
            right(90)    # 画笔向右旋转90度
            n=cos(radians(heading() - 45)) / 4 + 0.5
            pencolor(brnch, brnch * 0.8, brnch * 0.8)
            circle(3)    # 绘制半径为3的圆圈
            left(90)     # 画笔向左旋转90度

            # 添加30%飘落的叶子
            if (random() > 0.7):
                pu()    # 抬笔
                t = heading()
                an = -40 + random() * 40    # [-40, 0]
                setheading(an)
                dis = int(random() * 400 + random() * 120 + random() * 40)
                forward(dis)    # 前进dis
                setheading(t)

                #画叶子
                pd()    # 落笔
                right(90)
                brnch = cos(radians(heading() - 45)) / 4 + 0.5
                pencolor(brnch * 0.5 + 0.5, brnch * 0.4 + 0.4, brnch * 0.4 + 0.4)
                circle(2)    # 绘制半径为2的圆圈
                left(90)
                pu()         # 抬笔

                #返回
                t=heading()
                setheading(an)
                backward(dis)
                setheading(t)
        pu()           # 抬笔
        backward(l)    # 后退l

    bgcolor(0.5,0.5,0.5)    # 背景色
    ht()                    # 隐藏画笔
    tracer(0)               # 控制动画的速度
    pu()                    # 抬笔
    backward(100)
    left(90)                # 左转90度
    pu()                    # 抬笔
    backward(300)           # 后退300

    tree(12,100)            # 树枝总分叉数为12,第一层长度为100

    done()

    没错了,下面的花就是用我们熟悉的小乌龟画的哦,快来看一下效果吧!

    16200975480cbe55ffaa4aabc181c903.png

    展开全文
  • 本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码:# drawtree.pyfrom turtle import Turtle, mainloopdef tree(plist, l, a, f):""" plist is list of pensl is length of brancha ...

    本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码:

    # drawtree.py

    from turtle import Turtle, mainloop

    def tree(plist, l, a, f):

    """ plist is list of pens

    l is length of branch

    a is half of the angle between 2 branches

    f is factor by which branch is shortened

    from level to level."""

    if l > 5: #

    lst = []

    for p in plist:

    p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.

    q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.

    p.left(a) #Turn turtle left by angle units

    q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.

    lst.append(p)#将元素增加到列表的最后

    lst.append(q)

    tree(lst, l*f, a, f)

    def main():

    p = Turtle()

    p.color("green")

    p.pensize(5)

    #p.setundobuffer(None)

    p.hideturtle() #Make the turtle invisible. It's a good idea to do this while you're in the middle of doing some complex drawing,

    #because hiding the turtle speeds up the drawing observably.

    #p.speed(10)

    # p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.

    p.speed(10)

    #TurtleScreen methods can then be called for that object.

    p.left(90)# Turn turtle left by angle units. direction 调整画笔

    p.penup() #Pull the pen up – no drawing when moving.

    p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle's orientation.

    p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画

    #否则turtle一移动就会自动的把线画出来

    #t = tree([p], 200, 65, 0.6375)

    t = tree([p], 200, 65, 0.6375)

    main()

    实现效果:

    总结

    以上就是本文关于Python+Turtle动态绘制一棵树实例分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

    展开全文
  • 本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码:# drawtree.pyfrom turtle import Turtle, mainloopdef tree(plist, l, a, f):""" plist is list of pensl is length of brancha ...
  • 今天小编就为大家分享一篇python3实现用turtle模块画一棵随机樱花,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
  • /usr/bin/env python# coding=utf-8# 画一棵樱花import turtleimport randomfrom turtle import *from time import sleep# 画樱花的躯干(60,t)def tree(branchLen,t):sleep(0.0005)if branchLen >3:if 8...
  • python3用turtle模块画一棵随机樱花

    万次阅读 多人点赞 2019-01-05 17:29:01
    #!/usr/bin/env python ...# 画一棵樱花 import turtle import random from turtle import * from time import sleep # 画樱花的躯干(60,t) def tree(branchLen,t): sleep(0.0005) if branchLen >3...
  • Python用turtle模块画一棵漂亮的星星 非常漂亮的星星 每次执行都完全随机 五角星拥有随机颜色和大小 下面是源代码 import turtle , random turtle.screensize(900,600,'papayawhip') turtle.title('星星-羊')...
  • 最近翻到篇知乎,上面有不少用Python(大多是turtle库)绘制的树图,感觉很漂亮,我整理了一下,挑了一些我觉得不错的代码分享给大家(这些我都测试过,确实可以生成) one 樱花 动态生成樱花 效果图(这...
  • python3用turtle模块画一棵随机樱花#!https://mp.weixin.qq.com/s?__biz=MzU2NTc1MTc5MQ==&mid=2247484002&idx=1&sn=16218dc19067a0d97fa7f1233f8e636b&chksm=fcb7b8e2cbc031f413ba4181c71ff1316...
  • 分享给大家一篇文章,教你怎样用Python画了一棵圣诞,快来学习。如何用Python画一个圣诞呢?最简单:height = 5​stars = 1for i in range(height):print((' ' * (height - i)) + ('*' * stars))stars += 2print...
  • /usr/bin/env python# coding=utf-8# 画一棵樱花import turtleimport randomfrom turtle import *from time import sleep# 画樱花的躯干(60,t)def tree(branchLen,t):sleep(0.0005)if branchLen >3:if 8<...
  • 草yang年华来源:CSDN博客最近翻到篇知乎,上面有不少用Python(大多是turtle库)绘制的树图,感觉很漂亮,我整理了一下,挑了一些我觉得不错的代码分享给大家(这些我都测试过,确实可以生成喔~)one 樱花动态...
  • /usr/bin/env python# coding=utf-8# 画一棵樱花import turtleimport randomfrom turtle import *from time import sleep# 画樱花的躯干(60,t)def tree(branchLen,t):sleep(0.0005)if branchLen >3:if 8<...
  • /usr/bin/env python# coding=utf-8# 画一棵樱花import turtleimport randomfrom turtle import *from time import sleep# 画樱花的躯干(60,t)def tree(branchLen,t):sleep(0.0005)if branchLen >3:if 8...
  • 最近不少读者对 python 的 turtle 库比较感兴趣,用python一朵玫瑰给你,之前也写了篇文章讲过如何一朵玫瑰花。今天在知乎上找了个用 turtle 树的 python 程序,起来...我先说下棵树的大致思路,首...
  • 分享给大家一篇文章,教你怎样用Python画了一棵圣诞,快来学习。如何用Python画一个圣诞呢?最简单:height = 5stars = 1for i in range(height): print((' ' * (height - i)) + ('*' * stars)) stars += 2print...
  • 最近不少读者对 python 的 turtle 库比较感兴趣,用python一朵玫瑰给你,之前也写了篇文章讲过如何一朵玫瑰花。今天在知乎上找了个用 turtle 树的 python 程序,起来...我先说下棵树的大致思路,首...
  • 最近不少读者对 python 的 turtle 库比较感兴趣,用python一朵玫瑰给你,之前也写了篇文章讲过如何一朵玫瑰花。今天在知乎上找了个用 turtle 树的 python 程序,起来...我先说下棵树的大致思路,首...
  • python教你画一棵树

    千次阅读 2019-02-14 02:46:34
    最近不少读者对 python 的 turtle 库比较感兴趣,用python一朵玫瑰给你,之前也写了篇文章讲过如何一朵玫瑰花。今天在知乎上找了个用 turtle 树的 python 程序,起来看看...我先说下棵树的大致思路...
  • 如何用Python画一个圣诞呢?最简单:height = 5​stars = 1for i in range(height):print((' ' * (height - i)) + ('*' * stars))stars += 2print((' ' * height) + '|')效果:哈哈哈哈,总有一种骗了大家的感觉。...
  • 最近不少读者对 python 的 turtle 库比较感兴趣,用python一朵玫瑰给你,之前也写了篇文章讲过如何一朵玫瑰花。今天在知乎上找了个用 turtle 树的 python 程序,起来看看。...我先说下棵树的大...

空空如也

空空如也

1 2 3 4 5 ... 8
收藏数 160
精华内容 64
关键字:

turtle画一棵树