【小白学Python】Python基本图形的turtle(海龟)绘制法

第二章:
turtle库概述:

turtle(海龟)库是turtle绘图体系的Python实现
-turtle绘图体系:1969年诞生,主要用于程序设计入门 
-Python语言的标准库之一 - 入门级的图形绘制函数库

标准库

Python计算生态 = 标准库 + 第三方库
- 标准库:随解释器直接安装到操作系统中的功能模块 
- 第三方库:需要经过安装才能使用的功能模块 
- 库Library、包Package、模块Module,统称模块

库引用

扩充Python程序功能的方式
- 使用import保留字完成,采用<a>.<b>()编码风格 import <库名> <库名>.<函数名>(<函数参数>)
import更多用法
使用from和import保留字共同完成
from <库名> import <函数名> 
from <库名> import * 
<函数名>(<函数参数>)
两种方法比较 ,前者不会出现函数名同名。
我们通常这样做:import <库名> as <库别名> :给调用的外部库关联一个更短、更适合自己的名字

turtle的原(wan)理(fa)

turtle(海龟)是一种真实的存在
- 有一只海龟,其实在窗体正中心,在画布上游走 
- 走过的轨迹形成了绘制的图形 
- 海龟由程序控制,可以变换颜色、改变宽度等

画笔控制函数

画笔设置后一直有效,直至下次重新设置
- turtle.penup() 别名 turtle.pu() 抬起画笔,海龟在飞行
- turtle.pendown() 别名 turtle.pd() 落下画笔,海龟在爬行
- turtle.pensize(width) 别名 turtle.width(width) 画笔宽度,海龟的腰围
- turtle.pencolor(color) color为颜色字符串或r,g,b值 画笔颜色,海龟在涂装
pencolor(color)的color可以有三种形式
- 颜色字符串 :turtle.pencolor("purple")    此处是字符串
- RGB的小数值:turtle.pencolor(0.63, 0.13, 0.94) 
- RGB的元组值:turtle.pencolor((0.63,0.13,0.94))

运动控制函数

控制海龟行进:走直线 & 走曲线
控制海龟行进:走直线 & 走曲线
- turtle.forward(d) 别名 turtle.fd(d) 向前行进,海龟走直线
- d: 行进距离,可以为负数
- turtle.circle(r, extent=None) 根据半径r绘制extent角度的弧形
- r: 默认圆心在海龟左侧r距离的位置 - extent: 绘制角度,默认是360度整圆

方向控制函数

控制海龟面对方向: 绝对角度 & 海龟角度
绝对角度:
- turtle.setheading(angle) 别名 turtle.seth(angle) 改变行进方向,海龟走角度
- angle: 行进方向的绝对角度
海龟角度:
- turtle.left(angle) 海龟向左转 - turtle.right(angle) 海龟向右转 - angle: 在海龟当前行进方向上旋转的角度

循环语句

按照一定次数循环执行一组语句
for <变量> in range(<次数>): <被循环执行的语句>
- <变量>表示每次循环的计数,0到<次数>-1
range()函数 产生循环计数序列
- range(N) 产生 0 到 N-1的整数序列,共N个
- range(M,N) 产生M 到 N-1的整数序列,共N-M个

下面给大家这段画蟒蛇的代码,请大家结合上面所述知识理解代码:

#PythonDraw.py
import turtle     #程序关键,import(保留字 ) 引入了一个绘图库
turtle.setup(650, 350, 200, 200) #窗体的高度,宽度,已经窗体左上角对于屏幕左上角的坐标x,y;若没有x,y参数,则窗体显示在屏幕正中间
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)
turtle.done()

补充:代码末的turtle.done()函数的作用是在画图结束后不立即结束窗口。好奇的小伙伴可以把最后一行去掉看看效果。
附上几张能用到的图:
在这里插入图片描述在这里插入图片描述在这里插入图片描述
同时我还搜罗了两个好玩的话题代码:玫瑰花和机器猫。
玫瑰花:

import turtle

# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)

# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill()

# 花瓣1
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)
turtle.circle(80, 98)
turtle.circle(-90, 40)

# 花瓣2
turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)

# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()

turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)

# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()

turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)
turtle.pendown()
turtle.done()

机器猫:

#encoding=utf-8
#Drawcat 画多啦A梦
from turtle import *


# 无轨迹跳跃
def my_goto(x, y):
    penup()
    goto(x, y)
    pendown()

# 眼睛
def eyes():
    tracer(False)
    a = 2.5
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a -= 0.05
            lt(3)
            fd(a)
        else:
            a += 0.05
            lt(3)
            fd(a)
    tracer(True)

# 胡须
def beard():
    my_goto(-37, 135)
    seth(165)
    fd(60)

    my_goto(-37, 125)
    seth(180)
    fd(60)

    my_goto(-37, 115)
    seth(193)
    fd(60)

    my_goto(37, 135)
    seth(15)
    fd(60)

    my_goto(37, 125)
    seth(0)
    fd(60)

    my_goto(37, 115)
    seth(-13)
    fd(60)

# 嘴巴
def mouth():
    my_goto(5, 148)
    seth(270)
    fd(100)
    seth(0)
    circle(120, 50)
    seth(230)
    circle(-120, 100)

# 围巾
def scarf():
    fillcolor('#e70010')
    begin_fill()
    seth(0)
    fd(200)
    circle(-5, 90)
    fd(10)
    circle(-5, 90)
    fd(207)
    circle(-5, 90)
    fd(10)
    circle(-5, 90)
    end_fill()

# 鼻子
def nose():
    my_goto(-10, 158)
    fillcolor('#e70010')
    begin_fill()
    circle(20)
    end_fill()

# 黑眼睛
def black_eyes():
    seth(0)
    my_goto(-20, 195)
    fillcolor('#000000')
    begin_fill()
    circle(13)
    end_fill()

    pensize(6)
    my_goto(20, 205)
    seth(75)
    circle(-10, 150)
    pensize(3)

    my_goto(-17, 200)
    seth(0)
    fillcolor('#ffffff')
    begin_fill()
    circle(5)
    end_fill()
    my_goto(0, 0)

 

# 脸
def face():
    fd(183)
    fillcolor('#ffffff')
    begin_fill()
    lt(45)
    circle(120, 100)

    seth(90)
    eyes()
    seth(180)
    penup()
    fd(60)
    pendown()
    seth(90)
    eyes()
    penup()
    seth(180)
    fd(64)
    pendown()
    seth(215)
    circle(120, 100)
    end_fill()

# 头型
def head():
    penup()
    circle(150, 40)
    pendown()
    fillcolor('#00a0de')
    begin_fill()
    circle(150, 280)
    end_fill()

# 画哆啦A梦
def Doraemon():
    # 头部
    head()

    # 围脖
    scarf()

    # 脸
    face()

    # 红鼻子
    nose()

    # 嘴巴
    mouth()

    # 胡须
    beard()

    # 身体
    my_goto(0, 0)
    seth(0)
    penup()
    circle(150, 50)
    pendown()
    seth(30)
    fd(40)
    seth(70)
    circle(-30, 270)


    fillcolor('#00a0de')
    begin_fill()

    seth(230)
    fd(80)
    seth(90)
    circle(1000, 1)
    seth(-89)
    circle(-1000, 10)

    # print(pos())

    seth(180)
    fd(70)
    seth(90)
    circle(30, 180)
    seth(180)
    fd(70)

    # print(pos())
    seth(100)
    circle(-1000, 9)

    seth(-86)
    circle(1000, 2)
    seth(230)
    fd(40)

    # print(pos())


    circle(-30, 230)
    seth(45)
    fd(81)
    seth(0)
    fd(203)
    circle(5, 90)
    fd(10)
    circle(5, 90)
    fd(7)
    seth(40)
    circle(150, 10)
    seth(30)
    fd(40)
    end_fill()

    # 左手
    seth(70)
    fillcolor('#ffffff')
    begin_fill()
    circle(-30)
    end_fill()

    # 脚
    my_goto(103.74, -182.59)
    seth(0)
    fillcolor('#ffffff')
    begin_fill()
    fd(15)
    circle(-15, 180)
    fd(90)
    circle(-15, 180)
    fd(10)
    end_fill()

    my_goto(-96.26, -182.59)
    seth(180)
    fillcolor('#ffffff')
    begin_fill()
    fd(15)
    circle(15, 180)
    fd(90)
    circle(15, 180)
    fd(10)
    end_fill()

    # 右手
    my_goto(-133.97, -91.81)
    seth(50)
    fillcolor('#ffffff')
    begin_fill()
    circle(30)
    end_fill()

    # 口袋
    my_goto(-103.42, 15.09)
    seth(0)
    fd(38)
    seth(230)
    begin_fill()
    circle(90, 260)
    end_fill()

    my_goto(5, -40)
    seth(0)
    fd(70)
    seth(-90)
    circle(-70, 180)
    seth(0)
    fd(70)

    #铃铛
    my_goto(-103.42, 15.09)
    fd(90)
    seth(70)
    fillcolor('#ffd200')
    # print(pos())
    begin_fill()
    circle(-20)
    end_fill()
    seth(170)
    fillcolor('#ffd200')
    begin_fill()
    circle(-2, 180)
    seth(10)
    circle(-100, 22)
    circle(-2, 180)
    seth(180-10)
    circle(100, 22)
    end_fill()
    goto(-13.42, 15.09)
    seth(250)
    circle(20, 110)
    seth(90)
    fd(15)
    dot(10)
    my_goto(0, -150)

    # 画眼睛
    black_eyes()

if __name__ == '__main__':
    screensize(800,600, "#f0f0f0")
    pensize(3)  # 画笔宽度
    speed(9)    # 画笔速度
    Doraemon()
    my_goto(230, -300)
    write('by 王保保', font=("Bradley Hand ITC", 30, "bold"))
    my_goto(230,200)
    mainloop()

大家一起加油哇!奥利给!

发布了14 篇原创文章 · 获赞 0 · 访问量 444

猜你喜欢

转载自blog.csdn.net/qq_45627679/article/details/104339247