Python学习笔记(二):turtle画图库详解

简介

turtle是Python自带的一个很好玩很好用的库,功能就是画图,我们来简单地了解一下。我们可以通过有趣的turtle库来学习一下Python对库

先贴一个小例子
最终效果

from turtle import *


def jumpto(x, y):
    up()
    goto(x, y)
    down()


setup(800, 800)
left(90)
pensize(5)
pencolor("red")
fillcolor("red")
begin_fill()
circle(50, 200)
goto(0, -120)
jumpto(0, 0)
setheading(90)
circle(-50, 200)
goto(0, -120)
end_fill()
hideturtle()
done()

调用方法

使用turtle之前需要先调用一下turtle库,在这里介绍一下调用Python库的几种方式

1.import <库名>
这种方式调用的是库里的所有函数,但是在使用库里的函数时,需要加上库名
具体使用方法为<库名>.<函数名>(<函数参数>)

这种方式的好处是,如果一个程序里调用了多个库的话,这种方法可以防止函数名重复

如果嫌库名太长还可以用import <库名> as <别名>的方式,这样在使用的时候就只需要<别名>.<函数名>(<函数参数>)就行了

2.from <库名> import <函数名,函数名,....,函数名>
这种方法调用的就只是声明的函数,在使用的时候就不需要加库名
具体的使用方法为<函数名>(<函数参数>)

3.from <库名> import * # 这里*代表的是所有函数
调用的是所有函数,使用时不需要加库名,具体使用方法同第二种方法

常用函数

接下来介绍一下turtle里常用的函数:

常用函数一览表

设置函数 功能
setup() 建立画布
penup() / pu() / up() 抬起画笔
pendown() / pd() / down() 放下画笔
pensize() / width() 设置画笔宽度
pencolor() 设置画笔颜色
speed() 设置画笔速度
hideturtle() 隐藏画笔
showturtle() 显示画笔
画图函数 功能
forward() / fd() 沿当前方向移动
backward() / bd() 沿当前反方向移动
goto() 移动到
home() 回到原点
setx() 沿x轴移动
sety() 沿y轴移动
right() 当前方向右转
left() 当前方向左转
setheading() / seth() 转绝对方向角度
circle() 画圆
dot() 画圆点
fillcolor() 填充颜色
begin_fill() 开始填充
end_fill() 结束填充

函数详解

1.setup(width, height, startx, starty)
功能:新建一个画布

其实就是在所有操作之前必须执行的一个操作

参数:
width/height是窗口宽/高度,值为整数时,表示像素;为小数时,表示和屏幕的比例

startx/starty是窗口与屏幕左侧/顶部的像素距离,若值为None或者未填,则在中央

2.penup()
功能:提起画笔

因为turtle的画笔默认在画布上,直接移动的话,会在画布上留下痕迹。

3.pendown()
功能:放下画笔

将penup抬起的画笔放下

4.pencolor(colorstring) / pencolor((r, g, b))
功能:改变画笔的颜色

参数:
colorstring:表示颜色的字符串,如:“red” 、"blue"等
(r, g, b):颜色对应的RGB数值,详见RGB颜色对照表

5.speed(speed)
功能:调整画笔的移动速度

参数:
speed:范围为[0, 10]。0最快,直接成型;其他的数字,数字越大速度越快

6.hideturtle()
功能:隐藏画笔海龟

画完图后想看图,觉得海龟碍眼的,就一隐了之吧

7.forward(distance)
功能:让画笔沿着当前方向移动

参数:
distance:行进距离的像素值,为负数时,沿相反方向移动

8.goto(x, y)
功能:将画笔移动到给定的位置

参数:
x:位置的横坐标
y:位置的纵坐标

9.right(to_angle)
功能:将画笔的方向从当前方向向右(顺时针)转动to_angle度

参数:
to_angle:要转动的角度

10.seth(to_angle)
功能:将画笔的方向从x正半轴(东方向)逆时针转动to_angle度

参数:
to_angle:要转动的角度

11.circle(radius,extent = None)
功能:根据半径radius绘制extent角度的弧形

参数:
radius:弧形半径。值为正时,逆时针画圆;值为负时,顺时针画圆
extent:弧形的角度。当不输入或者输入为None的时候,绘制整个圆

这里还有一些常用的程序块

每次需要移动画笔的时候都需要先将画笔抬起,移动,再放下。很麻烦,所以,我们用一个函数来完成这一切

def jumpto(x, y):  # 将画笔移动到指定的位置
    up()
    goto(x, y)
    down()

turtle画完是会自动关闭画板的,有时候我们需要欣赏一下自己的画作,就需要把下面代码加在程序的最后面

    s = Screen()  # 单击画板关闭画板
    s.exitonclick()
    # done()  # 画板被挂起

应用

现在我们了解了turtle的大致操作,需要应用一下,熟悉一下操作。
上次我们编写了命令行下的扫雷,这次我们用turtle将每次扫雷之后的结果画出来
贴一下效果
在这里插入图片描述
在这里插入图片描述

函数如下

from turtle import *


def jumpto(x, y):  # 将画笔移动到指定的位置
    up()
    goto(x, y)
    down()


def square(m, n, num, flag):  # 绘制方块
    if num % 2 == 1:
        x = -10 * (num - 1) - 10 + 20 * n
        y = 20 * (num - 1) + 10 - 20 * m
        jumpto(x, y)
    else:
        x = -10 * num + 20 * n
        y = 10 * num - 20 * m
        jumpto(x, y)
    if m == 1:
        jumpto(x + 7, y)
        write(n)
        jumpto(x, y)
    if n == 1:
        jumpto(x - 10, y - 17)
        write(m)
        jumpto(x, y)
    if flag == 1:
        for i in range(4):
            fd(20)
            right(90)
        goto(x + 20, y - 20)
        jumpto(x, y - 20)
        goto(x + 20, y)


def digit(m, n, num, num1):  # 绘制雷周围的数字
    if num % 2 == 1:
        x = -10 * (num - 1) - 10 + 20 * n
        y = 20 * (num - 1) + 10 - 20 * m
        jumpto(x + 7, y - 17)
    else:
        x = -10 * num + 20 * n
        y = 10 * num - 20 * m
        jumpto(x + 8, y - 17)
    color = ("lightgray", "skyblue", "dodgerblue", "gold", "orange", "red", "maroon", "purple")
    pencolor(color[num1])
    write(num1)
    pencolor("black")


def turtle_print(user_list, num):
    setup(800, 800)
    speed(0)
    reset()
    for i in range(num + 2):
        for j in range(num + 2):
            if user_list[i][j] == '#' or user_list[i][j] == '@':
                square(i, j, num, 1)
            elif user_list[i][j] != 0:
                square(i, j, num, 0)
                digit(i, j, num, user_list[i][j])
            elif user_list[i][j] != 0:
                square(i, j, num, 0)
    hideturtle()

最后在上次扫雷文件的头文件加这一句

from turtle1 import *  # 在头文件加这一句

然后在print_mine的函数里加一句

def print_mine(list2, num):             # list2为要输出的数组,num为数组的大小
    for m in range(0, num + 1):
        for n in range(0, num + 1):
            if m == 0:                  # 横坐标
                print(n, end='  ')
                continue
            if n == 0:                  # 纵坐标
                print(m, end='  ')
                continue
            if list2[m][n] == 0:        # 将对应为0的元素用空格代替
                print('  ', end=' ')
            else:
                print(list2[m][n], end='  ')
        print('')
    turtle_print(list2, num)  # 需要加这一句

就可以享受用turtle来画扫雷了,就是turtle画图速度较慢,不太适合扫雷游戏,如果要尝试,请输入一个较小的雷阵行数。

原文地址:https://www.cnblogs.com/cnsec/p/11879409.html