zoukankan      html  css  js  c++  java
  • Turtle库

    下列turtle库的简单常用指令

    forward(distance) #将箭头移到某一指定坐标 

    left(angel)  right(angel) 

    penup() #提起笔,用于另起一个地方绘制时用,与pendown()配对使用 

    goto(x,y) 

    home() 

    circle(radius) 

    speed() 

     1 #五角星图形
     2 from turtle import Turtle
     3 
     4 p = Turtle()
     5 p.speed(3)
     6 p.pensize(5)
     7 p.color("black", 'yellow')
     8 #p.fillcolor("red")
     9 p.begin_fill()
    10 for i in range(5):
    11     p.forward(200)  #将箭头移到某一指定坐标 
    12     p.right(144)    #当前方向上向右转动角度
    13 p.end_fill()

    树的绘制 

    观察:对称树, 从主杆出发以一定角度向左向右生成对称的枝丫, 且每一棵枝杈上以相同的角度生成更小的左右枝杈,如此往复。

        联系:所学内容,易想到利用递归程序实现; 

     

     1 # drawtree.py
     2 
     3 from turtle import Turtle, mainloop
     4 
     5 def tree(plist, l, a, f):
     6     """ plist is list of pens
     7     l is length of branch
     8     a is half of the angle between 2 branches
     9     f is factor by which branch is shortened
    10     from level to level."""
    11     if l > 5: #
    12         lst = []
    13         for p in plist:
    14             p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
    15             q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
    16             p.left(a) #Turn turtle left by angle units
    17             q.right(a)#Turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
    18             lst.append(p)#将元素增加到列表的最后
    19             lst.append(q)
    20         tree(lst, l*f, a, f)
    21   
    22            
    23 
    24 def main():
    25     p = Turtle()
    26     p.color("green")
    27     p.pensize(5)
    28     #p.setundobuffer(None)
    29     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,
    30                    #because hiding the turtle speeds up the drawing observably.
    31     
    32     p.getscreen().tracer(10,0)
    33         #Return the TurtleScreen object the turtle is drawing on.
    34         #TurtleScreen methods can then be called for that object.
    35     #p.speed(10)
    36     p.left(90)  #Turn turtle left by angle units. direction 调整画笔
    37 
    38     p.penup() #Pull the pen up – no drawing when moving.
    39     p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
    40     p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
    41     #否则turtle一移动就会自动的把线画出来
    42 
    43     #t = tree([p], 200, 65, 0.6375)
    44     t = tree([p], 200, 65, 0.6375)
    45     
    46 main()

     

    森林的绘制 

    如何画出多棵树,甚至整片森林呢? 

    答案很简单,只要在画每棵树之前调整画笔的位置,调用画树程序,就可以从新位置生成一颗新树了。 

    利用模块化的函数思想,调整代码: 

    将每棵树的绘制以maketree函数封装,参数x,y为画树的起点位置即树根位置。在main函数中只要以

     不同的参数设置来调用maketree函数就可以完成多棵树的绘制了 

     1 # drawtree.py
     2 from turtle import Turtle, mainloop
     3 
     4 def tree(plist, l, a, f):
     5     """ plist is list of pens
     6     l is length of branch
     7     a is half of the angle between 2 branches
     8     f is factor by which branch is shortened
     9     from level to level."""
    10     if l > 5: #
    11         lst = []
    12         for p in plist:
    13             p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
    14             q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
    15             p.left(a) #Turn turtle left by angle units
    16             q.right(a)#Turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
    17             lst.append(p)#将元素增加到列表的最后
    18             lst.append(q)
    19         tree(lst, l*f, a, f)
    20 
    21 def maketree(x, y):
    22     p = Turtle()
    23     p.color("green")
    24     p.pensize(5)
    25     p.hideturtle()
    26     p.getscreen().tracer(30, 0)
    27     p.left(90)
    28 
    29     p.penup()
    30     p.goto(x, y)
    31     p.pendown()
    32 
    33     t = tree([p], 110, 65, 0.6375)
    34     print(len(p.getscreen().turtles())) #用了多少个turtle绘制
    35 
    36 def main():
    37     maketree(-200, -200)
    38     maketree(0, 0)
    39     maketree(200, -200)
    40 
    41 main()

      图像:

         

  • 相关阅读:
    去掉DevExpress gridControl控件表头上的的默认英文字母
    【555】folium 更换底图
    github图片不显示问题解决
    厉害了,自己动手实现 LRU 缓存机制!
    一文搞懂 Netty 的整体流程,还有谁不会?
    PageHelper 分页一直有性能问题?
    left join 后用 on 还是 where,区别大了!
    不推荐别的了,IDEA 自带的数据库工具就很牛逼!
    王炸!!IDEA 2021.1 推出语音、视频功能,边写代码边聊天,我真的服了…
    别再面向 for 循环编程了,Spring 自带的观察者模式就很香!
  • 原文地址:https://www.cnblogs.com/aze-003/p/5127843.html
Copyright © 2011-2022 走看看