matlab海龟作图,Python学习之turtle绘图篇

2024-01-26 10:10

本文主要是介绍matlab海龟作图,Python学习之turtle绘图篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

画一个红色的五角星

from turtle import *color('red','red')

begin_fill()for i in range(5):

fd(200)

rt(144)

end_fill()

done()

效果图:

f802c5ace5fe369a5a64e2d5fa8a7ed6.png

画一条蟒蛇

#PythonDraw.py

importturtle#turtle.setup(650,350,200,200)

turtle.penup()

turtle.fd(-250)

turtle.pendown()

turtle.pensize(25)

turtle.pencolor("purple")

turtle.seth(-40)for i in range(4):

turtle.circle(40,80)

turtle.circle(-40,80)

turtle.circle(40,80/2)

turtle.fd(40)

turtle.circle(16,180)

turtle.fd(40*2/3)

效果图:

34b185170eeede90ebbfc017a362c4d0.png

画一组内切圆

importturtle

turtle.pensize(2)

turtle.circle(10)

turtle.circle(40)

turtle.circle(80)

turtle.circle(120)

效果图:

d2154cba8d5d9c76e3cdcddc8b72286b.png

画一个社会人小猪佩奇,代码是借鉴某位大佬的

from turtle import *

defnose(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.08lt(3)

fd(a)else:

a=a-0.08lt(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()defhead(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.08lt(3)

fd(a)else:

a=a-0.08lt(3)

fd(a)

end_fill()defears(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()defeyes(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()defcheek(x,y):

color((255,155,192))

pu()

goto(x,y)

pd()

seth(0)

begin_fill()

circle(30)

end_fill()defmouth(x,y):

color(239,69,19)

pu()

goto(x,y)

pd()

seth(-80)

circle(30,40)

circle(40,80)defbody(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()defhands(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)deffoot(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)deftail(x,y):

pensize(4)

color((255,155,192))

pu()

goto(x,y)

pd()

seth(0)

circle(70,20)

circle(10,330)

circle(70,30)defsetting():

pensize(4)

hideturtle()

colormode(255)

color((255,155,192),"pink")

setup(840,500)

speed(10)defmain():

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()

main()

效果极佳:

3cbe3aafc994f6b7f3bad04000e29ac9.png

简单总结一下好了,其实turtle库还是非常有用的,以后可以画张很有创意的图片去表白女神了,讲不好那天就用上了呢:

显示器和turtle窗体的左上角都是原点

turtle.setup(width,height,startx,starty)设置窗体大小及位置,后面两个参数可选,非必需

空间坐标:绝对坐标:右方向x轴,上方向为y轴,正中心为原点,turtle.goto(x,y)让在任何位置的海龟到达指定的位置

海龟坐标:turtle.fd(d)海龟向前行多少像素

turtle.bk(d)向海龟的反方向运行

turtle.circle(r,angle)以海龟左边的某一点为圆心进行曲线运行

角度坐标:绝对角度:turtle.seth(angle)angle为绝对度数,表示改变海龟的运行方向

海龟角度:turtle.right(angle),turtle.left(angle)改变海龟的运行方向

RGB体系:turtle.colormode(mode)mode=1.0则改用0和1表示,mode=255则改用255和0表示,默认为小数

库引用:import ,from import *或者from import ,import as

画笔控制函数:turtle.penup()别名:turtle.pu()抬起画笔,turtle.pendown()别名:turtle.pd()落下画笔,turtle.pensize(width)别名:turtle.width(width)设置画笔宽度,turtle.pencolor(color)设置画笔颜色,color有三种方式,颜色字符串,或者RGB小数值,或者RGB元组值,例如:turtle.pencolor((0.63,0.13,0.94))

运动控制函数:turtle.circle(r,angle),r(海龟左方向)表示半径,angle表示绘制的角度,默认是360度

方向控制函数:turtle.seth(angle)改变海龟的行进方向,顺时针转向

原文出处:https://www.cnblogs.com/andrew3/p/10604218.html

这篇关于matlab海龟作图,Python学习之turtle绘图篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/646468

相关文章

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.