turtle库的基本使用及绘图

'''
主体窗体
t.turtle(width, height, startx, starty) # 定义主体窗体的大小(像素(整数)或者与屏幕的比例(小数))和位置(默认屏幕居中)

画笔状态
pendown():放下画笔                              别名t.up()/t.pu()
penup():提起画笔与pendown配合使用,开始绘制图形     别名t.down()/t.pd()
pensize(width):设置画笔线条粗细                     别名t.width()
pencolor():设置画笔颜色
color():设置画笔和填充的颜色
begin_fill():填充图形前调用
end_fill():填充结束
filling():返回填充状态,True为填充,False为未填充
clear():清除当前窗口,不改变画笔位置
reset():清除当前窗口,重置位置等状态
screensize():设置画布宽高和背景颜色
hideturtle():隐藏画笔的turtle形状
showturtle():显示画笔的turtle形状
isvisible():如果turtle可见,返回True
write(str,front=):输出font字体的字符串

画笔运动函数
forward(distance):指定向前方向距离                别名t.fd()
backward(distance):与当前方向相反指定距离          别名t.kd()
right(angle):向右旋转指定角度
left(angle):向左旋转指定角度
goto(x,y):移动至(x,y)
setx(x):修改画笔横坐标位置
sety(y):修改画笔纵坐标位置
setheading(to_angle):设置当前朝向绝对角度           别名t.seth(to_angle)
home():画笔回至原点
circle(radius,e):绘制圆或圆弧
dot(size,color):绘制直径为size,颜色的原点
undo():撤销画笔最后一步
speed():设置画笔速度,0~10
'''
import turtle as t


def demo1():
    # 绘制圆
    t.color('red', 'blue')
    t.begin_fill()
    t.circle(100)
    t.end_fill()
def demo2():
    # 绘制三角形、正方形、多边形
    t.pensize(3)
    # 绘制三角形
    t.penup()
    t.goto(-200, -50)
    t.pendown()
    t.begin_fill()
    t.color('red')
    # t.circle(40, extent=90, steps= 2) # extent表示圆的角度,steps表示起点到中点由多少线连接
    t.circle(40, steps=3)
    t.end_fill()

    # 绘制四边形
    t.penup()
    t.goto(-100, -50)
    t.pendown()
    t.begin_fill()
    t.color('blue')
    t.circle(40, steps=4)
    t.end_fill()

    # 绘制五边形
    t.penup()
    t.goto(0, -50)
    t.pendown()
    t.begin_fill()
    t.color('green')
    t.circle(40, steps=5)
    t.end_fill()

    # 绘制六边形
    t.penup()
    t.goto(100, -50)
    t.pendown()
    t.begin_fill()
    t.color('yellow')
    t.circle(40, steps=6)
    t.end_fill()

    # 绘制圆形
    t.penup()
    t.goto(200, -50)
    t.pendown()
    t.begin_fill()
    t.color('purple')
    t.circle(40)
    t.end_fill()

    # 添加文字
    t.color('green')
    t.penup()
    t.goto(-100,150)
    t.pendown()
    t.write('Cool Colorful Shapes', font=('Times', 24, 'bold'))
    t.hideturtle()
    t.done()  # 作用是暂停程序,停止画笔绘制,但绘图窗体不关闭,直到用户关闭Python Turtle图形化窗口为止


if __name__ == "__main__":
    demo1()
    demo2()

效果图如下
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值