当前位置 博文首页 > AiY..的博客:Python—海龟作图

    AiY..的博客:Python—海龟作图

    作者:[db:作者] 时间:2021-07-15 09:48

    1、因为海龟作图需要用到"turtle"库,所以先介绍库的三种引用方法:
    (1):from 库名 import 函数名/ * ;
    (2):import 库名 ——>使用时:库名.函数名
    (3):import 库名 as 函数名
    2、turtle的使用方法:
    (1)Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
    在这里插入图片描述(2)画布:就是海龟作图是大小区域

    turtle.setup(width=800,height=800, startx=100, starty=100)
    

    (3)画笔:在画布中,画笔原始在画布中心位置,海龟面朝x轴的正半轴(如上图)
    turtle.pensize():设置画笔的宽度;
    turtle.pencolor():设置海龟的颜色
    turtle.speed():设置海龟爬行的速度
    (4)接下来是海龟作图的一些常见命令(在其他大佬博客看到的):

    画笔运动的命令:
    在这里插入图片描述 画笔控制的命令:
    在这里插入图片描述全局控制的命令:
    在这里插入图片描述海龟作图的例子:
    代码:

    import turtle as t
    t.shape("turtle")
    t.pencolor("red")
    t.circle(50)
    t.penup()
    
    t.pencolor("green")
    t.goto(120,0)
    t.pendown()
    t.circle(50)
    t.penup()
    
    t.pencolor("black")
    t.goto(240,0)
    t.pendown()
    t.circle(50)
    t.penup()
    
    t.pencolor("yellow")
    t.goto(60,-50)
    t.pendown()
    t.circle(50)
    t.penup()
    
    t.pencolor("blue")
    t.goto(180,-50)
    t.pendown()
    t.circle(50)
    

    结果:
    在这里插入图片描述

    cs