python-turtle函数

1基础概念

1.1 画布(canvas)

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

常用的画布方法有两个:screensize()和setup()。

(1)turtle.screensize(canvwidth=None, canvheight=None, bg=None)

参数分别为画布的宽(单位像素), 高, 背景颜色

如:

turtle.screensize(800, 600, “green”)

turtle.screensize() #返回默认大小(400, 300)

(2)turtle.setup(width=0.5, height=0.75, startx=None, starty=None)

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

如:

turtle.setup(width=0.6, height=0.6)

turtle.setup(width=800, height=800, startx=100, starty=100)

1.2 画笔

在画布上,默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝x轴正方向小乌龟。

这里我们描述小乌龟时使用了两个词语:标原点(位置),面朝x轴正方向(方向),turtle绘图中, 就是使用位置方向描述小乌龟(画笔)的状态

(1)画笔的属性

画笔有颜色、画线的宽度等属性。

  1. turtle.pensize() :设置画笔的宽度;

  2. turtle.pencolor() :没有参数传入返回当前画笔颜色;传入参数设置画笔颜色,可以是字符串如"green", “red”,也可以是RGB 3元组。

    pencolor(‘brown’)

    tup = (0.2, 0.8, 0.55)

    pencolor(tup)

    pencolor()

    ‘#33cc8c’

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

(2)绘图命令

操纵海龟绘图有着许多的命令,这些命令可以划分为3种:运动命令,画笔控制命令和全局控制命令

画笔运动命令:

命令

说明

turtle.forward(distance)

向当前画笔方向移动distance像素长

turtle.backward(distance)

向当前画笔相反方向移动distance像素长度

turtle.right(degree)

顺时针移动degree°

turtle.left(degree)

逆时针移动degree°

turtle.pendown()

移动时绘制图形,缺省时也为绘制

turtle.goto(x,y)

将画笔移动到坐标为x,y的位置

turtle.penup()

移动时不绘制图形,提起笔,用于另起一个地方绘制时用

turtle.speed(speed)

画笔绘制的速度范围[0,10]整数

turtle.circle()

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

画笔控制命令:

命令

说明

turtle.pensize(width)

绘制图形时的宽度

turtle.pencolor()

画笔颜色

turtle.fillcolor(colorstring)

绘制图形的填充颜色

turtle.color(color1, color2)

同时设置pencolor=color1, fillcolor=color2

turtle.filling()

返回当前是否在填充状态

turtle.begin_fill()

准备开始填充图形

turtle.end_fill()

填充完成;

turtle.hideturtle()

隐藏箭头显示;

turtle.showturtle()

与hideturtle()函数对应

全局控制命令

命令

说明

turtle.clear()

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

turtle.reset()

清空窗口,重置turtle状态为起始状态

turtle.undo()

撤销上一个turtle动作

turtle.isvisible()

返回当前turtle是否可见

stamp()

复制当前图形

turtle.write(s[,font=(“font-name”,font_size,“font_type”)])

写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项, font的参数也是可选项

2、绘图展示

2.1猴子的头(未完成)

代码如下:

import turtle

turtle.screensize(800, 600, “green”)

turtle.pencolor(“brown”)

turtle.pensize(5)

turtle.fillcolor(“brown”)

turtle.begin_fill()

turtle.circle(90)

turtle.circle(90,105)

turtle.right(90)

turtle.circle(40,-360)

turtle.end_fill()

turtle.left(90)

turtle.begin_fill()

turtle.circle(90,145)

turtle.right(90)

turtle.left(10)

turtle.circle(40,-360)

turtle.right(150)

turtle.forward(75)

turtle.right(90)

turtle.forward(50)

turtle.right(90)

turtle.forward(10)

turtle.end_fill()

turtle.fillcolor(“red”)

turtle.begin_fill()

turtle.pencolor(“black”)

turtle.penup

turtle.left(180)

turtle.pencolor(“red”)

turtle.forward(20)

turtle.right(180)

turtle.pencolor(“black”)

turtle.forward(10)

turtle.pendown

turtle.circle(10)

turtle.end_fill()

turtle.right(-90)

turtle.forward(50)

turtle.circle(20,105)

turtle.left(40)

turtle.circle(20,55)

turtle.circle(20,-55)

turtle.right(40)

turtle.circle(20,-105)

turtle.circle(-20,105)

turtle.right(40)

turtle.circle(-20,55)

turtle.pencolor(“brown”)

turtle.forward(40)

turtle.right(-20)

turtle.forward(20)

turtle.left(90)

turtle.forward(20)

turtle.pencolor(“black”)

turtle.fillcolor(“white”)

turtle.begin_fill()

turtle.circle(-20)

turtle.end_fill()

turtle.fillcolor(“black”)

turtle.begin_fill()

turtle.circle(-15)

turtle.end_fill()

turtle.right(180)

turtle.pencolor(“brown”)

turtle.forward(80)

turtle.pencolor(“black”)

turtle.fillcolor(“white”)

turtle.begin_fill()

turtle.circle(20)

turtle.end_fill()

turtle.fillcolor(“black”)

turtle.begin_fill()

turtle.circle(15)

turtle.end_fill()

(以后会将这个猴子完善,探索中)

2.2太阳花

代码如下:

from turtle import *

color(‘red’,‘yellow’)

begin_fill()

while True:

forward(200)

left(170)

if abs(pos()) <1:

    break

end_fill()

done()

2.3五角星

代码如下:

import turtle

turtle.pensize(10)

turtle.pencolor(“red”)

turtle.fillcolor(“yellow”)

turtle.begin_fill()

for i in range(5):

turtle.forward(120)

turtle.right(144)

turtle.forward(120)

turtle.left(72)

turtle.end_fill()

turtle.hideturtle()

turtle.done()

新的改变

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G

合理的创建标题,有助于目录的生成

直接输入1次#,并按下space后,将生成1级标题。
输入2次#

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我会尽力回答你的问题。 首先,你需要导入turtle库,并创建一个画布和画笔。然后,你可以使用turtle库提供的函数来绘制草莓熊。 以下是一个草莓熊的绘制示例代码: ```python import turtle # 创建画布和画笔 canvas = turtle.Screen() pen = turtle.Turtle() # 绘制草莓熊 pen.penup() pen.goto(0, -100) pen.pendown() # 绘制头部 pen.begin_fill() pen.circle(100) pen.end_fill() # 绘制耳朵 pen.penup() pen.goto(-60, 120) pen.pendown() pen.begin_fill() pen.circle(40) pen.end_fill() pen.penup() pen.goto(60, 120) pen.pendown() pen.begin_fill() pen.circle(40) pen.end_fill() # 绘制眼睛 pen.penup() pen.goto(-40, 40) pen.pendown() pen.begin_fill() pen.circle(20) pen.end_fill() pen.penup() pen.goto(40, 40) pen.pendown() pen.begin_fill() pen.circle(20) pen.end_fill() # 绘制鼻子 pen.penup() pen.goto(0, 0) pen.pendown() pen.begin_fill() pen.circle(30) pen.end_fill() # 绘制嘴巴 pen.penup() pen.goto(-60, -40) pen.pendown() pen.right(60) pen.circle(80, 120) # 绘制身体 pen.penup() pen.goto(0, -100) pen.pendown() pen.begin_fill() pen.circle(150) pen.end_fill() # 绘制手臂 pen.penup() pen.goto(-150, -100) pen.pendown() pen.begin_fill() pen.circle(50) pen.end_fill() pen.penup() pen.goto(150, -100) pen.pendown() pen.begin_fill() pen.circle(50) pen.end_fill() # 绘制脚 pen.penup() pen.goto(-80, -250) pen.pendown() pen.begin_fill() pen.circle(80) pen.end_fill() pen.penup() pen.goto(80, -250) pen.pendown() pen.begin_fill() pen.circle(80) pen.end_fill() # 隐藏画笔 pen.hideturtle() # 点击关闭窗口 canvas.exitonclick() ``` 这段代码将绘制一个草莓熊,包括头部、耳朵、眼睛、鼻子、嘴巴、身体、手臂和脚。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值