python 教程实现 turtle海龟绘图
作者:autofelix 发布时间:2022-03-19 10:45:35
标签:python,turtle,绘制,海龟,绘图
一、安装
写出许多有趣的可视化东西
也可以画出很多奇妙的图案
pip install turtule
二、画布
画布就是turtle为我们展开用于绘图区域
我们可以设置它的大小和初始位置
import turtle
# 返回默认大小(400, 300)
turtle.screensize()
# 设置画布方法一,设置宽、高、背景色
turtle.screensize(800, 600, "green")
# 设置画布方法二, 宽高为小数时候为占据电脑屏幕比例, 宽高为整数时候为像素
turtle.setup(width=0.6, height=0.6)
# startx,starty表示矩形窗口左上角顶点的位置, 如果为空, 则窗口位于屏幕中心
turtle.setup(width=800, height=800, startx=100, starty=100)
三、画笔
可以设置画笔的属性,颜色、画线的宽度等
import turtle
# 设置画笔的宽度
turtle.pensize()
# 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色
turtle.pencolor()
# 设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快
turtle.speed(speed)
四、绘图命令
import turtle
# 向当前画笔方向移动distance像素长
turtle.forward(distance)
# 向当前画笔相反方向移动distance像素长度
turtle.backward(distance)
# 顺时针移动degree°
turtle.right(degree)
# 逆时针移动degree°
turtle.left(degree)
# 移动时绘制图形,缺省时也为绘制
turtle.pendown()
# 将画笔移动到坐标为x,y的位置
turtle.goto(x,y)
# 移动时不绘制图形,提起笔,用于另起一个地方绘制时用
turtle.penup()
# 画笔绘制的速度范围[0,10]整数
turtle.speed(speed)
# 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
turtle.circle()
五、画笔控制命令
import turtle
# 绘制图形时的宽度
turtle.pensize(width)
# 画笔颜色
turtle.pencolor()
# 绘制图形的填充颜色
turtle.fillcolor(colorstring)
# 同时设置pencolor=color1, fillcolor=color2
turtle.color(color1, color2)
# 返回当前是否在填充状态
turtle.filling()
# 准备开始填充图形
turtle.begin_fill()
# 填充完成
turtle.end_fill()
# 隐藏箭头显示
turtle.hideturtle()
# 与hideturtle()函数对应
turtle.showturtle()
六、全局控制命令
import turtle
# 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.clear()
# 清空窗口,重置turtle状态为起始状态
turtle.reset()
# 撤销上一个turtle动作
turtle.undo()
# 返回当前turtle是否可见
turtle.isvisible()
# 复制当前图形
stamp()
# 写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型
turtle.write(s[,font=("font-name",font_size,"font_type")])
七、绘制方形螺旋
from turtle import *
for i in range(500):
forward(i)
left(91)
八、绘制彩色螺旋
from turtle import *
colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange']
for x in range(360):
pencolor(colors[x % 6])
width(x / 100 + 1)
forward(x)
left(59)
九、绘制太阳花
import turtle as t
import time
t.color("red", "yellow")
t.speed(10)
t.begin_fill()
for _ in range(50):
t.forward(200)
t.left(170)
end_fill()
time.sleep(1)
十、绘制小蟒蛇
import turtle
def drawSnake(rad, angle, len, neckrad):
for _ in range(len):
turtle.circle(rad, angle)
turtle.circle(-rad, angle)
turtle.circle(rad, angle/2)
turtle.forward(rad/2) # 直线前进
turtle.circle(neckrad, 180)
turtle.forward(rad/4)
if __name__ == "__main__":
turtle.setup(1500, 1400, 0, 0)
turtle.pensize(30) # 画笔尺寸
turtle.pencolor("green")
turtle.seth(-40) # 前进的方向
drawSnake(70, 80, 2, 15)
十一、绘制五角星
import turtle
import time
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")
turtle.begin_fill()
for _ in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
time.sleep(2)
turtle.penup()
turtle.goto(-150,-120)
turtle.color("violet")
turtle.write("Done", font=('Arial', 40, 'normal'))
time.sleep(1)
十二、绘制小猪佩奇
from turtle import*
# 绘制鼻子
def nose(x,y):
pu()
goto(x,y)
pd()
seth(-30)
begin_fill()
a=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
lt(3) #向左转3度
fd(a) #向前走a的步长
else:
a=a-0.08
lt(3)
fd(a)
end_fill()
pu()
seth(90)
fd(25)
seth(0)
fd(10)
pd()
pencolor(255,155,192)
seth(10)
begin_fill()
circle(5)
color(160,82,45)
end_fill()
pu()
seth(0)
fd(20)
pd()
pencolor(255,155,192)
seth(10)
begin_fill()
circle(5)
color(160,82,45)
end_fill()
# 绘制头部
def head(x,y):
color((255,155,192),"pink")
pu()
goto(x,y)
seth(0)
pd()
begin_fill()
seth(180)
circle(300,-30)
circle(100,-60)
circle(80,-100)
circle(150,-20)
circle(60,-95)
seth(161)
circle(-300,15)
pu()
goto(-100,100)
pd()
seth(-30)
a=0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
a=a+0.08
lt(3) #向左转3度
fd(a) #向前走a的步长
else:
a=a-0.08
lt(3)
fd(a)
end_fill()
# 绘制耳朵
def ears(x,y):
color((255,155,192),"pink")
pu()
goto(x,y)
pd()
begin_fill()
seth(100)
circle(-50,50)
circle(-10,120)
circle(-50,54)
end_fill()
pu()
seth(90)
fd(-12)
seth(0)
fd(30)
pd()
begin_fill()
seth(100)
circle(-50,50)
circle(-10,120)
circle(-50,56)
end_fill()
# 绘制眼睛
def eyes(x,y):
color((255,155,192),"white")
pu()
seth(90)
fd(-20)
seth(0)
fd(-95)
pd()
begin_fill()
circle(15)
end_fill()
color("black")
pu()
seth(90)
fd(12)
seth(0)
fd(-3)
pd()
begin_fill()
circle(3)
end_fill()
color((255,155,192),"white")
pu()
seth(90)
fd(-25)
seth(0)
fd(40)
pd()
begin_fill()
circle(15)
end_fill()
color("black")
pu()
seth(90)
fd(12)
seth(0)
fd(-3)
pd()
begin_fill()
circle(3)
end_fill()
# 绘制腮
def cheek(x,y):
color((255,155,192))
pu()
goto(x,y)
pd()
seth(0)
begin_fill()
circle(30)
end_fill()
# 绘制嘴巴
def mouth(x,y):
color(239,69,19)
pu()
goto(x,y)
pd()
seth(-80)
circle(30,40)
circle(40,80)
# 绘制身体
def body(x,y):
color("red",(255,99,71))
pu()
goto(x,y)
pd()
begin_fill()
seth(-130)
circle(100,10)
circle(300,30)
seth(0)
fd(230)
seth(90)
circle(300,30)
circle(100,3)
color((255,155,192),(255,100,100))
seth(-135)
circle(-80,63)
circle(-150,24)
end_fill()
# 绘制手
def hands(x,y):
color((255,155,192))
pu()
goto(x,y)
pd()
seth(-160)
circle(300,15)
pu()
seth(90)
fd(15)
seth(0)
fd(0)
pd()
seth(-10)
circle(-20,90)
pu()
seth(90)
fd(30)
seth(0)
fd(237)
pd()
seth(-20)
circle(-300,15)
pu()
seth(90)
fd(20)
seth(0)
fd(0)
pd()
seth(-170)
circle(20,90)
# 绘制脚
def foot(x,y):
pensize(10)
color((240,128,128))
pu()
goto(x,y)
pd()
seth(-90)
fd(40)
seth(-180)
color("black")
pensize(15)
fd(20)
pensize(10)
color((240,128,128))
pu()
seth(90)
fd(40)
seth(0)
fd(90)
pd()
seth(-90)
fd(40)
seth(-180)
color("black")
pensize(15)
fd(20)
# 绘制尾巴
def tail(x,y):
pensize(4)
color((255,155,192))
pu()
goto(x,y)
pd()
seth(0)
circle(70,20)
circle(10,330)
circle(70,30)
# 参数设置
def setting():
pensize(4)
hideturtle()
colormode(255)
color((255,155,192),"pink")
setup(840,500)
speed(10)
if __name__ == "__main__":
setting() #画布、画笔设置
nose(-100,100) #鼻子
head(-69,167) #头
ears(0,160) #耳朵
eyes(0,140) #眼睛
cheek(80,10) #腮
mouth(-20,30) #嘴
body(-32,-8) #身体
hands(-56,-45) #手
foot(2,-177) #脚
tail(148,-155) #尾巴
done() #结束
来源:https://blog.51cto.com/autofelix/5269246
0
投稿
猜你喜欢
- MySQL数据库中文问题的解决方法:1.my.ini MySQL配置文件设置[client]default-character-set=gb
- 本文实例分析了JavaScript事件委托技术。分享给大家供大家参考。具体分析如下:如果一个整体页面里有大量的按钮.我们就要为每一个按钮绑定
- python函数一、函数定义函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数能提高应用的模块性,和代码的重复利用率。你
- 为什么传输几千个表的数据时向导会失败?当前的向导体系结构会创建一个 DTS 软件包数据流以传输数据。但如果要处理几千个表,则会遇到可伸缩性限
- 计算字符串中所有数字的和,字符串中有数字和字母组合而成如果出现连续数字,按照一个数操作具体解释在代码行里:def sum_str(str1)
- Django功能强大不单在于他先进的编程理念,很多现有的功能模块更是可以直接拿来使用,比如这个牛掰的admin模块,可以作为一个很好的信息登
- 平行坐标图,一种数据可视化的方式。以多个垂直平行的坐标轴表示多个维度,以维度上的刻度表示在该属性上对应值,相连而得的一个折线表示一个样本,以
- 大家已经从实际使用中了解了jquery这个javascript框架的强大,其实jquery更加强大的是可扩展。你可以编写自己的基于jquer
- 第一,两者的功能不同。global关键字修饰变量后标识该变量是全局变量,对该变量进行修改就是修改全局变量,而nonlocal关键字修饰变量后
- 首先将ORACLE 10g的安装光盘放入光驱,如果自动运行,一般会出现如图1安装界面: 图1 单击“开始安装”,就可以安装ORACLE 10
- 插入一条记录后,如何得到最新的自动增加ID?我们要用到SQL Server的@@IDENTITY。它能够记录下系统最近使用的一个IDENTI
- 在ASP中使用FSO组件生成HTML静态页面,共有两个页面index.htm是首页.程序比较简单,主要是用了ASP里的文件操作
- 问题:希望仅仅允许某个指定IP的计算机连接到SQL Server服务器,但不允许其他的客户端进行连接。解决方法如下:你可以直接在防火墙中做限
- 我要说的内容都是非常基础的内容,高手就免看了,如果看了欢迎给点意见啊。新手或者对低层还不是很了解的人可以看看,帮助理解与记忆。XMLHttp
- 八卦是种优良品质,特别是用在技术上时。来看几个Reset CSS的八卦问题吧:你知道世界上第一份reset.css在哪么?*&nb
- 前言项目中要实现多选,就想到用插件,选择了bootstrap-select。附上官网api链接,http://silviomoreto.gi
- 自打 Lokesh Dhakar 创造了第一个lightbox应用以来, 相册和弹出窗口方式便跨进了新的时代, 甚至那些lightbox应用
- 希腊Web 设计师Christos Chiotis 发表在 CssGlobe 的一篇文章,讲述了黄金分割率在 CSS 中的应用。黄金分割率是
- 这篇文章主要是想说,“引用只能指向具体对象而不能指向引用”//创建变量testArray 并引用数组 ["1&
- 前言本文主要介绍了关于Python+selenium自动化环境搭建的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧