基于turtle的Python作画

简介: 2018年6月12日笔记按win+q键换出搜索界面,输入path,进入系统属性,选择高级,选择环境变量。在系统变量中的PATHEXT这个变量中文本内容为.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC。

2018年6月12日笔记

  1. 按win+q键换出搜索界面,输入path,进入系统属性,选择高级,选择环境变量。在系统变量中的PATHEXT这个变量中文本内容为.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC。如果这个文本内容中没有.EXE,在cmd中输入命令的时候则不能省略.exe的后缀,即原本pip install xlwt要写成pip.exe install xlwt
  2. 学习用python作画:首先进入python的shell界面,方法是在安装好python的情况下在cmd中运行python命令,就可以进入python的shell界面。
    进入以后,导入turtle库。方法是在python的shell中运行命令:from turtle import *。文章后面运行命令的环境都是python的shell。
  3. 画一条直线,执行下面的两行命令可以实现。
pendown()
forward(100)

pendown()的作用是落笔,只有落笔才能作画。
当不作画却想移动画笔的时候要提笔,用函数penup()
forward是画笔向前移动,函数当中参数为移动距离。
forward(100)的意思是画笔向前移动100。

  1. 画一个边长为200的正方形。
for i in range(4):
    forward(200)
    right(90)
  1. 画一个复杂图形。
def draw1():
    reset()
    speed(10)
    for i in range(36):
        forward(200)
        left(170)
reset()
speed(10)
draw1()

speed()中的参数1-10画图速度递增,但是有一个反例参数为0时速度最快。
reset()会重置画笔,画布,作画速度。

  1. 顺时针方向画一个200半径的圆:circle(-200)
    逆时针方向画一个200半径的圆:circle(200)
    顺时针画一个100半径的半圆:circle(-100,180)
    顺时针画一个边长为150的正方形:circle(-150,360,4)
  2. 将图形涂色示例,画一个红色的半圆。
reset()
fillcolor('red')
begin_fill()
circle(100,180)
end_fill()

8.复杂图形涂色示例,画一个“太极”图案。

reset()
speed(10)
pendown()
circle(100,180)
circle(200,180)
circle(100,-180)
fillcolor('black')
begin_fill()
circle(100,180)
circle(200,180)
circle(100,-180)
end_fill()
penup()
goto(0,100)
dot(50)
goto(0,-100)
pencolor('white')
dot(50)
hideturtle()

circle(100)与circle(100,360)两条命令效果相同。
撤回一步:undo(),清空画布:clear()。


img_571e239b0c1902b6db392db86983710f.png
画出的太极图形.png
  1. 画一段曲线
for i in range(8):
    circle(20,100)
    circle(-20,100)
  1. 画一个复杂图形,利用循环嵌套方法
from turtle import *
reset()
speed(0)
pendown()
for i in range(6):
    fd(150)
    for j in range(10):
        circle(40)
        lt(36)
    lt(60)
img_dc1b1a493e8587ff1f04da0b8401ce37.png
复杂图形1.png
  1. 画一个复杂图形,利用循环嵌套方法
from turtle import *
reset()
speed(0)
for i in range(6):
    pendown()
    fd(150)
    for j in range(10):
        circle(40)
        lt(36)
    lt(60)
    penup()
    goto(0,0)
img_d6679a7818ac94d381610716db367e2f.png
复杂图形2.png
  1. 获取画笔当前位置:position() pos() 两个函数用处一样
    设置画笔位置:setposition() setpos()
    获取角度:heading()
    设置角度setheading() seth()
  2. 画一个椭圆
reset()
setheading(45)
circle(10,90)
circle(90,90)
circle(10,90)
circle(90,90)

14.画一个笑脸。下面的代码作为一个单独py文件可以运行。

from turtle import *
def go(x,y):
    penup()
    goto(x,y)
    pendown()
def arc(radius):
    circle(radius,90)
reset()
speed(0)
go(0,-150)
circle(200)
go(50,100)
seth(225)
arc(10)
arc(50)
arc(10)
arc(50)
go(-50,100)
seth(-45)
arc(-10)
arc(-50)
arc(-10)
arc(-50)
go(-70,-50)
arc(100)
hideturtle()
img_139b00c4197d2dd649e3338b69c626ba.png
笑脸.png

直接在cmd中可能无法运行,需要先定义函数,再调用函数,如下图所示,。


img_e3d88bf7a41851fcc89f92eab7b00cfc.png
cmd中运行示例.png
  1. 画一个酷炫图形。
from turtle import *
reset()
bgcolor('black')
speed(0)
colors = ['red','orange','green','cyan','blue','purple']
for i in range(360):
    pencolor(colors[i%6])
    fd(i*3/6+i)
    left(61)
    pensize(i*6/200)
img_c2046ede3a39b8e1bdfc0e7c8509a633.png
炫酷图案.png
目录
相关文章
|
9月前
|
Python
【python】——turtle动态画
【python】——turtle动态画
【python】——turtle动态画
|
9月前
|
Python
python turtle库
python turtle库
146 0
|
5月前
|
Python
turtle库的几个案例进阶,代码可直接运行(python经典编程案例)
该文章展示了使用Python的turtle库进行绘图的进阶案例,包括绘制彩色圆形和复杂图案的代码示例。
1324 6
turtle库的几个案例进阶,代码可直接运行(python经典编程案例)
|
5月前
|
Python
turtle库的几个简单案例,代码可直接运行(python经典编程案例)
该文章提供了多个使用Python的turtle库绘制不同图形的简单示例代码,如画三角形、正方形、多边形等,展示了如何通过turtle进行基本的绘图操作。
549 5
|
8月前
|
Python
【python绘图库turtle实战】使用python绘图库turtle绘制:太阳花、彩虹线与小黄人【含完整源码】
【python绘图库turtle实战】使用python绘图库turtle绘制:太阳花、彩虹线与小黄人【含完整源码】
|
9月前
|
存储 Python
【python】——超市管理系统和用turtle动态画图(爱心和魔幻曲线)
【python】——超市管理系统和用turtle动态画图(爱心和魔幻曲线)
【python】——超市管理系统和用turtle动态画图(爱心和魔幻曲线)
|
9月前
|
弹性计算 数据安全/隐私保护 Python
快速搭建python turtle画布,画出专属你的冬日浪漫
turtle库是Python语言中自带的一个用于绘制图像的函数库。turtle库为使用者提供一个或多个小乌龟作为画笔,使用者可通过turtle库提供的各种方法去控制小乌龟在一个平面直角坐标系中移动并绘制移动轨迹以画出想要的图案。
快速搭建python turtle画布,画出专属你的冬日浪漫
|
9月前
|
Python
基于python的turtle实现圣诞树的绘制
基于python的turtle实现圣诞树的绘制
|
Python
10分钟轻松学会 Python turtle 绘图(下)
10分钟轻松学会 Python turtle 绘图
129 0
|
前端开发 图形学 Python
10分钟轻松学会 Python turtle 绘图(上)
10分钟轻松学会 Python turtle 绘图
239 0

热门文章

最新文章