Python中turtle库常用代码汇总

作者:墨瑶_165 时间:2021-02-24 06:50:38 

一、设置画布

turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置

turtle.screensize(canvwidth=600,canvheight=800,bg='black')
#参数分别代表画布的宽、高、背景色
turtle.screensize()#返回默认大小(400,300)

turtle.setup(width=0.6,height=0.6,startx=100,starty=100)
#输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
#(startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心

二、画笔

1、画笔属性

turtle.pensize(8)    #画笔粗细

turtle.color('red')   #画笔颜色   字符串"green", "red" 或者 RGB 3元组。

turtle.speed(0)    #画笔移动速度   画笔绘制的速度范围[0,10]整数,数字越大越快

2、绘图命令

(1)    画笔运动命令

turtle.forward(8)      #向前移动

turtle.backward(8)  #向后移动

turtle.right(90)  #海龟方向向右转90°

turtle.left(90) #海龟方向向左转90°

turtle.penup()        #提笔

turtle.pendown()   #落笔

turtle.goto(x,y)      海龟移动到(x,y)位置

turtle.setx(x)         海龟的x坐标移动到指定位置

turtle.sety(y)         海龟的y坐标移动到指定位置

turtle.circle()         画圆

turtle.dot()            画一个圆点(实心)

turtle.setheading(angle)     #设置当前朝向为angle角度

turtle.home()                 设置当前画笔位置为原点,朝向东(默认值)

(2)     画笔控制命令

turtle.fillcolor('red')               设置 填充颜色

turtle.color(color1, color2)   设置 画笔颜色为color1,填充颜色为color2

turtle.begin_fill()                   开始填充颜色

turtle.end_fill()                      填充完成

turtle.hideturtle()                  隐藏海龟图标

turtle.showturtle()                显示海龟图标

(3)    全局控制命令

turtle.clear()                   清空turtle窗口,但是turtle的位置和状态不会改变

turtle.reset()                   清空turtle窗口,重置turtle状态为起始状态

turtle.undo()                   撤销上一个turtle动作

turtle.isvisible()               返回当前turtle是否可见

t.write("文本" ,align="center",font=("微软雅黑",20,"normal"))            写文本

align(可选):left,right,center;font(可选):字体名称,字体大小,字体类型(normal,bold,italic)

(4)    其他命令

Python中turtle库常用代码汇总

3. 命令详解

turtle.circle(radius, extent=None, steps=None)

参数:

        radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆;

        extent(弧度) ;

        steps :(做半径为radius的圆的内切正多边形,多边形边数为steps)。

import turtle as t
t.circle(50)#整圆
t.circle(50,steps=3)#内置的三角形

t.penup()
t.goto(100,0)
t.pendown()
t.circle(50,180)#半圆

Python中turtle库常用代码汇总

参考原文链接:https://www.jb51.net/article/130181.htm

三、文字显示为一个圆圈

import turtle as t
text="棉花娃娃很可爱"
t.penup()
x=len(text)
for i in text:
   t.write(i,font='consolas')
   t.right(360/x)
   t.penup()    
   t.forward(30)
t.hideturtle()

Python中turtle库常用代码汇总

四、画朵小花

import turtle as t
t.speed(0)

#花柄
t.penup()
t.goto(0,-150)
t.pendown()
t.pensize(2)
t.setheading(90)
t.color('brown')
t.fd(300)

#花瓣
t.pensize(1)
t.color('black','red')
t.begin_fill()
for i in range(10):
   t.left(45)
   t.circle(80,60)
   t.left(120)
   t.circle(80,60)
t.end_fill()

#叶子
for i in range(2):
   t.penup()
   t.goto(0,10-50*i)
   x=20+80*i
   t.setheading(x)
   t.pendown()
   t.color('brown','green')
   t.begin_fill()
   t.circle(60,60)
   t.left(120)
   t.circle(60,60)
   t.end_fill()
t.hideturtle()

Python中turtle库常用代码汇总

来源:https://blog.csdn.net/Echo_165/article/details/123968150

标签:python,turtle库,代码
0
投稿

猜你喜欢

  • PHP实战之投票系统的实现

    2023-10-24 10:40:39
  • asp使用shotgraph组件生成数字和字母验证码

    2007-09-26 12:26:00
  • 学习ASP.NET八天入门:第一天

    2007-08-07 13:08:00
  • js友好的表单验证程序vform

    2007-08-16 13:32:00
  • Python编程给numpy矩阵添加一列方法示例

    2023-08-29 07:22:30
  • Javascript中的isNaN函数使用说明

    2023-08-27 10:10:02
  • python numpy数组中的复制知识解析

    2023-08-10 14:41:33
  • 我要如何了解用户的需求

    2007-08-26 17:19:00
  • Python将string转换到float的实例方法

    2023-06-13 07:23:47
  • 我的ImageMagick使用心得

    2008-10-21 11:05:00
  • W3C优质网页小贴士(二)

    2008-04-07 12:14:00
  • python中类变量与成员变量的使用注意点总结

    2022-01-08 03:39:51
  • 详细介绍ASP内置对象Response

    2008-06-23 12:42:00
  • 在 SQL Server 数据库开发中的十大问题

    2009-07-02 16:39:00
  • 何时将数据装载到Application 或 Session 对象中去?

    2009-12-03 20:17:00
  • 很快大多数网民将放弃IE浏览器

    2009-02-04 16:43:00
  • C#调用python.exe使用arcpy方式

    2021-03-30 05:41:13
  • Python lxml模块安装教程

    2021-08-26 22:23:43
  • SQL存储过程介绍

    2008-02-13 18:52:00
  • 浏览器右下角弹出提示窗口

    2008-10-30 12:37:00
  • asp之家 网络编程 m.aspxhome.com