python turtle绘图


Posted in Python onMay 04, 2022

一、安装

  • 写出许多有趣的可视化东西
  • 也可以画出很多奇妙的图案
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)

python turtle绘图

八、绘制彩色螺旋

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)

python turtle绘图

九、绘制太阳花

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)

python turtle绘图

十、绘制小蟒蛇

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)

python turtle绘图

十一、绘制五角星

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)

python turtle绘图

十二、绘制小猪佩奇

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() #结束

Tags in this post...

Python 相关文章推荐
python搭建简易服务器分析与实现
Dec 15 Python
Python urllib模块urlopen()与urlretrieve()详解
Nov 01 Python
[原创]教女朋友学Python(一)运行环境搭建
Nov 29 Python
python 文件转成16进制数组的实例
Jul 09 Python
Python中变量的输入输出实例代码详解
Jul 28 Python
Django url,从一个页面调到另个页面的方法
Aug 21 Python
pandas中遍历dataframe的每一个元素的实现
Oct 23 Python
推荐8款常用的Python GUI图形界面开发框架
Feb 23 Python
Python实现AI自动抠图实例解析
Mar 05 Python
tensorflow中tf.reduce_mean函数的使用
Apr 19 Python
Python提取视频中图片的示例(按帧、按秒)
Oct 22 Python
Python 列表反转显示的四种方法
Nov 16 Python
python blinker 信号库
May 04 #Python
python三子棋游戏
May 04 #Python
python神经网络 使用Keras构建RNN训练
May 04 #Python
python神经网络学习 使用Keras进行回归运算
May 04 #Python
python神经网络学习 使用Keras进行简单分类
May 04 #Python
python神经网络 tf.name_scope 和 tf.variable_scope 的区别
May 04 #Python
Python3使用Qt5来实现简易的五子棋小游戏
May 02 #Python
You might like
简单的php中文转拼音的实现代码
2014/02/11 PHP
JavaScript入门学习书籍推荐
2008/06/12 Javascript
javascript 语法基础 想学习js的朋友可以看看
2009/12/16 Javascript
js 实现打印网页中定义的部分内容的代码
2010/04/01 Javascript
High Performance JavaScript(高性能JavaScript)读书笔记分析
2011/05/05 Javascript
JS Map 和 List 的简单实现代码
2013/07/08 Javascript
IE下写xml文件的两种方式(fso/saveAs)
2013/08/05 Javascript
JS判断移动端访问设备并加载对应CSS样式
2014/06/13 Javascript
js使用html()或text()方法获取设置p标签的显示的值
2014/08/01 Javascript
jQuery中filter()方法用法实例
2015/01/06 Javascript
Jquery异步提交表单代码分享
2015/03/26 Javascript
JS如何实现文本框随文本的长度而增长
2015/07/30 Javascript
jQuery实现仿百度帖吧头部固定导航效果
2015/08/07 Javascript
asp知识整理笔记3(问答模式)
2015/09/27 Javascript
基于JQuery实现分隔条的功能
2016/06/17 Javascript
利用BootStrap弹出二级对话框的简单实现方法
2016/09/21 Javascript
JavaScript Dom 绑定事件操作实例详解
2019/10/02 Javascript
node.js中对Event Loop事件循环的理解与应用实例分析
2020/02/14 Javascript
Vue自定义render统一项目组弹框功能
2020/06/07 Javascript
Vue使用鼠标在Canvas上绘制矩形
2020/12/24 Vue.js
[04:47]DOTA2-潍坊风行电子俱乐部探秘
2014/08/08 DOTA
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
为Python的Tornado框架配置使用Jinja2模板引擎的方法
2016/06/30 Python
决策树剪枝算法的python实现方法详解
2019/09/18 Python
利用4行Python代码监测每一行程序的运行时间和空间消耗
2020/04/22 Python
如何将无状态会话Bean发布为WEB服务,只有无状态会话Bean可以发布为WEB服务?
2015/12/03 面试题
物理教学随笔感言
2014/02/22 职场文书
我的大学四年规划书范文2014
2014/09/26 职场文书
五好文明家庭事迹材料
2014/12/20 职场文书
求职导师推荐信范文
2015/03/27 职场文书
雷锋的故事观后感
2015/06/10 职场文书
幼儿园2016年感恩节活动总结
2016/04/01 职场文书
2019财务管理制度最新范本!
2019/07/09 职场文书
扩展多台相同的Web服务器
2021/04/01 Servers
详解JSON.parse和JSON.stringify用法
2022/02/18 Javascript
《吸血鬼幸存者》新内容发布 追加多个全新模式
2022/04/07 其他游戏