使用python绘制turtle

    科技2023-10-23  79

    python中使用turtle这个包可以构建一个画板。该包允许使用者直接进行绘画,创建的内容由轨迹而确定。 简单介绍其中的一些语法,以及API对应的效果。

    完整的manual of turtle-python 传送门:https://www.apiref.com/python-zh/library/turtle.html

    import turtle 导入turtle包,这个包在anaconda中应该是直接内置了。 如果提示: No Model Error ,可以使用pip(pip3) install turtle 尝试安装。

    turtle.setup(1000,800) 这里是设置画板大小的对应函数。 共有四个参数,前两个为数字,代表画板的长(1000)和宽(800),单位是像素,不是长度单位。后两个用于控制屏幕的位置。这里为None,即将画板位于屏幕的水平中央,以及竖值中央。

    turtle.speed(6) 输入的值为整数,1-10,用以设置动画的播放速度,即游标的移动速度。

    turtle.colormode(255) 设置颜色的设置模式。这里只要设为255就好,符合传的表示习惯。

    turtle.color((0,0,0),(60,100,30)) 设置填充颜色和画笔颜色为指定的值。

    turtle.penup() 画笔抬起 – 移动时,游标不画线。

    turtle.goto(0,-200) 移动游标的位置。

    turtle.pendown() 画笔落下 – 移动时,游标将画线。

    turtle.begin_fill() turtle.circle(200) turtle.end_fill() 绘制一个圆形并且对其进行填充,填充的颜色在之前已经设置过。

    turtle.pensize(2) 设置笔画的粗细。

    a=1 turtle.speed(0) for i in range(120): if 0<=i<30 or 60<=i<=90: a=a+0.04 turtle.lt(3) 左转 angle=3 个单位。 turtle.fd(a) 向当前的指向前进a个单位。 else: a=a-0.04 turtle.lt(3) turtle.fd(a) turtle.penup() turtle.end_fill()

    turtle.seth(30) 设置游标的朝向。

    turtle.hideturtle() 将游标隐藏

    turtle.mainloop() 开始事件循环 - 调用 Tkinter 的 mainloop 函数。必须作为一个海龟绘图程序的结束语句。

    这个任务是绘制一个乌龟,代码的示例如下:

    import turtle turtle.setup(1000,800) turtle.speed(6) turtle.colormode(255) turtle.color((0,0,0),(60,100,30)) turtle.penup() turtle.goto(0,-200) turtle.pendown() turtle.begin_fill() turtle.circle(200) turtle.end_fill() turtle.pensize(2) turtle.penup() turtle.goto(190,-60) turtle.pendown() turtle.goto(-190,-60) turtle.penup() turtle.goto(190,60) turtle.pendown() turtle.goto(-190,60) turtle.penup() turtle.goto(60,190) turtle.pendown() turtle.goto(60,-190) turtle.penup() turtle.goto(-60,190) turtle.pendown() turtle.goto(-60,-190) turtle.penup() turtle.pensize(1) turtle.goto(20,198) turtle.penup() turtle.goto(0,200) turtle.pendown() turtle.color((0,0,0),(60,80,30)) turtle.begin_fill() a=1 turtle.speed(0) for i in range(120): if 0<=i<30 or 60<=i<=90: a=a+0.04 turtle.lt(3) turtle.fd(a) else: a=a-0.04 turtle.lt(3) turtle.fd(a) turtle.penup() turtle.end_fill() turtle.color((0,0,0),(255,255,255)) turtle.goto(11,240) turtle.begin_fill() turtle.pendown() turtle.circle(5) turtle.end_fill() turtle.penup() turtle.end_fill() turtle.color((0,0,0),(255,255,255)) turtle.goto(-11,240) turtle.begin_fill() turtle.pendown() turtle.circle(5) turtle.end_fill() turtle.penup() turtle.color((0,0,0),(0,0,0)) turtle.goto(10,240) turtle.begin_fill() turtle.pendown() turtle.circle(3) turtle.end_fill() turtle.penup() turtle.end_fill() turtle.color((0,0,0),(0,0,0)) turtle.goto(-10,240) turtle.begin_fill() turtle.pendown() turtle.circle(3) turtle.end_fill() turtle.penup() turtle.color((0,0,0),(60,80,30)) turtle.goto(-120,150) turtle.pendown() turtle.seth(30) turtle.begin_fill() a=0.3 for i in range(120): if 0<=i<30 or 60<=i<=90: a=a+0.06 turtle.lt(3) turtle.fd(a) else: a=a-0.06 turtle.lt(3) turtle.fd(a) turtle.end_fill() turtle.penup() turtle.goto(120,150) turtle.pendown() turtle.seth(-30) a=0.3 turtle.begin_fill() for i in range(120): if 0<=i<30 or 60<=i<=90: a=a+0.06 turtle.lt(3) turtle.fd(a) else: a=a-0.06 turtle.lt(3) turtle.fd(a) turtle.penup() turtle.end_fill() turtle.goto(-120,-160) turtle.pendown() turtle.seth(-210) turtle.begin_fill() a=0.5 for i in range(120): if 0<=i<30 or 60<=i<=90: a=a+0.03 turtle.lt(3) turtle.fd(a) else: a=a-0.03 turtle.lt(3) turtle.fd(a) turtle.penup() turtle.end_fill() turtle.goto(120,-160) turtle.pendown() turtle.seth(210) turtle.begin_fill() a=0.5 for i in range(120): if 0<=i<30 or 60<=i<=90: a=a+0.03 turtle.lt(3) turtle.fd(a) else: a=a-0.03 turtle.lt(3) turtle.fd(a) turtle.end_fill() turtle.penup() turtle.goto(0,-200) turtle.seth(0) turtle.pendown() turtle.begin_fill() turtle.fd(10) turtle.seth(-105) turtle.fd(30) turtle.seth(105) turtle.fd(30) turtle.seth(0) turtle.fd(10) turtle.end_fill() turtle.hideturtle() turtle.mainloop()

    最后使用pyinstaller进行打包输出,import的包会被打包为一个文件。

    1.安装pyinstaller: win+R 打开运行,输入cmd,打开win的命令行。 输入下述指令: pip install pyinstaller

    2.打包.py 进入对应的目录(cd file),然后在包含有py文件的目录下,执行对应的命令。 pyinstaller --onefile --nowindowed draw.py

    3.输出的.exe文件在/dist文件下能够找到。直接运行会打开命令行执行。

    pyinstall教程的传送门:https://www.cnblogs.com/robinunix/p/8426832.html

    Processed: 0.017, SQL: 8