美文网首页
4.初识少儿编程之turtle库的使用

4.初识少儿编程之turtle库的使用

作者: 为啥要创业 | 来源:发表于2018-07-30 16:39 被阅读0次

    turtle模块:它可以让你使用海龟图形(turtle graphics)绘制图像

    其中的函数:

    1)turtle.pensize():设置线条的粗细;

    2)turtle.speed():设置绘制的速度,1-10,1最慢,10最快;

    3)turtle.begin_fill():准备开始填充图形;

    4)turtle.circle(50,steps=3):circle函数在之前用到过,是画一个半径为radius的圆,这里是扩展,steps表示在半径为50的圆内的内置steps多边形;

    5)turtle.end_fill():填充完成;

    6)turtle.write(s,font=(“font-name”,font_size,”font_type”)):写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;

    7)turtle.hideturtle():隐藏箭头显示;

    另外,还有其他一些turtle函数,如:

    8)turtle.backward(d):与forward()函数对应,这里是从尾部绘制线条和箭头到头部;

    9)turtle.left(angle):逆时针转动箭头方向;

    10)turtle.undo():撤销上一个turtle动作;

    11)turtle.screensize(w,h):设置turtle窗口的长和宽;

    12)turtle.clear():清空turtle窗口,但是turtle的位置和状态不会改变;

    13)turtle.reset():清空窗口,重置turtle状态为起始状态;

    14)turtle.showturtle():与hideturtle()函数对应;

    15)turtle.filling():返回当前是否在填充状态;true为filling,false为not filling;

    16)turtle.isvisible():返回当前turtle是否可见。

    打开Python解释器,输入一下代码,检查你是否安装了turtle模块:

    >>> import turtle
    >>> bob = turtle.Turtle()
    
    image.png

    turtle 模块(小写的t)提供了一个叫作 Turtle 的函数(大写的T),这个函数会创建一个 Turtle 对象。输出的结果,意味着指向一个类型为Turtle的对象,这个类型是由 turtle 模块定义的。

    turtle.mainloop()告诉窗口等待用户操作,窗口不自动关闭

    创建了一个 Turtle 对象之后,你可以调用 方法(method) 来在窗口中移动该对象。方法与函数类似,但是其语法略有不同。例如,要让海龟向前走:

    bob.fd(100) #向前走
    bob.bk(100) #向后走
    
    bob.lt(90) #向左转
    bob.rt(90) #向右转
    
    bob.pu() #(pen up)抬笔
    bob.pd() #(pen down)落笔
    

    fd 方法的实参是像素距离,所以实际前进的距离取决于你的屏幕。

    Turtle 对象中你能调用的其他方法还包括:让它向后走的 bk ,向左转的 lt ,向右转的 rt 。 lt 和 rt 这两个方法接受的实参是角度。

    另外,每个 Turtle 都握着一支笔,不是落笔就是抬笔;如果落笔了,Turtle 就会在移动时留下痕迹。pu 和 pd 这两个方法分别代表“抬笔(pen up)”和“落笔(pen down)”。

    比如画正方形和圆的代码如下:(文件名 turtle_example)

    import math
    import turtle
    
    def square(t, length):
        """Draws a square with sides of the given length.
    
        Returns the Turtle to the starting position and location.
        """
        for i in range(4):
            t.fd(length)
            t.lt(90)
    
    def polyline(t, n, length, angle):
        """Draws n line segments.
    
        t: Turtle object
        n: number of line segments
        length: length of each segment
        angle: degrees between segments
        """
        for i in range(n):
            t.fd(length)
            t.lt(angle)
    
    def polygon(t, n, length):
        """Draws a polygon with n sides.
    
        t: Turtle
        n: number of sides
        length: length of each side.
        """
        angle = 360.0/n
        polyline(t, n, length, angle)
    
    def arc(t, r, angle):
        """Draws an arc with the given radius and angle.
    
        t: Turtle
        r: radius
        angle: angle subtended by the arc, in degrees
        """
        arc_length = 2 * math.pi * r * abs(angle) / 360
        n = int(arc_length / 4) + 1
        step_length = arc_length / n
        step_angle = float(angle) / n
    
        # making a slight left turn before starting reduces
        # the error caused by the linear approximation of the arc
        t.lt(step_angle/2)
        polyline(t, n, step_length, step_angle)
        t.rt(step_angle/2)
    
    def circle(t, r):
        """Draws a circle with the given radius.
    
        t: Turtle
        r: radius
        """
        arc(t, r, 360)
    
    # the following condition checks whether we are
    # running as a script, in which case run the test code,
    # or being imported, in which case don't.
    
    if __name__ == '__main__':
        bob = turtle.Turtle()
    
        # draw a circle centered on the origin
        radius = 100
        bob.pu()
        bob.fd(radius)
        bob.lt(90)
        bob.pd()
        circle(bob, radius)
    
        # wait for the user to close the window
        turtle.mainloop()
    

    此程序运行出来的效果图如下:

    image.png

    使用Turtle绘制的花朵,代码如下:

    import turtle
    from turtle_example import arc #用到上一个代码
    
    def petal(t, r, angle):
        """Draws a petal using two arcs.
    
        t: Turtle
        r: radius of the arcs
        angle: angle (degrees) that subtends the arcs
        """
        for i in range(2):
            arc(t, r, angle)
            t.lt(180-angle)
    
    def flower(t, n, r, angle):
        """Draws a flower with n petals.
    
        t: Turtle
        n: number of petals
        r: radius of the arcs
        angle: angle (degrees) that subtends the arcs
        """
        for i in range(n):
            petal(t, r, angle)
            t.lt(360.0/n)
    
    def move(t, length):
        """Move Turtle (t) forward (length) units without leaving a trail.
        Leaves the pen down.
        """
        t.pu()
        t.fd(length)
        t.pd()
    
    bob = turtle.Turtle()
    
    # draw a sequence of three flowers, as shown in the book.
    move(bob, -100)
    flower(bob, 7, 60.0, 60.0)
    
    move(bob, 100)
    flower(bob, 10, 40.0, 80.0)
    
    move(bob, 100)
    flower(bob, 20, 240.0, 20.0)
    
    # bob.hideturtle()
    turtle.mainloop()
    

    程序运行出来结果图如下:

    image.png

    使用Turtle画的饼状图,代码如下:

    import math
    import turtle
    
    def draw_pie(t, n, r):
        """Draws a pie, then moves into position to the right.
    
        t: Turtle
        n: number of segments
        r: length of the radial spokes
        """
        polypie(t, n, r)
        t.pu()
        t.fd(r*2 + 10)
        t.pd()
    
    def polypie(t, n, r):
        """Draws a pie divided into radial segments.
    
        t: Turtle
        n: number of segments
        r: length of the radial spokes
        """
        angle = 360.0 / n
        for i in range(n):
            isosceles(t, r, angle/2)
            t.lt(angle)
    
    def isosceles(t, r, angle):
        """Draws an icosceles triangle.
    
        The turtle starts and ends at the peak, facing the middle of the base.
    
        t: Turtle
        r: length of the equal legs
        angle: peak angle in degrees
        """
        y = r * math.sin(angle * math.pi / 180)
    
        t.rt(angle)
        t.fd(r)
        t.lt(90+angle)
        t.fd(2*y)
        t.lt(90+angle)
        t.fd(r)
        t.lt(180-angle)
    
    bob = turtle.Turtle()
    
    bob.pu()
    bob.bk(130)
    bob.pd()
    
    # draw polypies with various number of sides
    size = 40
    draw_pie(bob, 5, size)
    draw_pie(bob, 6, size)
    draw_pie(bob, 7, size)
    draw_pie(bob, 8, size)
    
    bob.hideturtle()
    turtle.mainloop()
    

    程序运行结果图:

    image.png

    相关文章

      网友评论

          本文标题:4.初识少儿编程之turtle库的使用

          本文链接:https://www.haomeiwen.com/subject/nwbfvftx.html