python学习笔记7(turtle)

turtle包常用操作

**简介:**是一个简单的绘图工具,提供一个小海龟,可以把它理解为一个机器人,只能听得懂有限的命令。绘图窗口的原点(0,0)在正中间,默认海龟的方向是右侧。

运动命令
forward(d) 向前移动d长度
backward(d) 向后移动d长度
right(d) 向右转动多少度
left(d) 向左转动多少度
goto(x,y) 移动到坐标为(x,y)的位置
speed(speed) 笔画绘制的速度[0,10]

笔画控制命令
up() 笔画抬起,在移动的时候不会绘图
down() 笔画落下,移动会绘图
setheading(d) 改变海龟的朝向
pensize(d) 笔画的宽度
pencolor(colorstr) 笔画颜色
reset() 恢复所有设置,清空窗口,重置turtle状态
clear() 清空窗口,不会重置turtle
circle(r, steps = e) 绘制一个圆形,r为半径,e为次数,e边形

填充命令
begin_fill()
fillcolor(colorstr)
end_fill()

其他命令
done() 程序继续执行
undo() 撤销上一次动作
hideturtle() 隐藏海龟
showturtle() 显示海龟
screensize(x, y)

示例:

#画国际象棋棋牌
import turtle
step = 20
turtle.speed(10)
for i in range(8):
    for j in range(8):
        turtle.up()
        turtle.goto(step * j, step * i)
        turtle.down()
        turtle.begin_fill()
        for m in range(4):
            turtle.forward(step)
            turtle.left(90)
        if (i + j) % 2 != 0:
            turtle.fillcolor("white")
        else:
            turtle.fillcolor("black")
        turtle.end_fill()
turtle.done()

输出:在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42216171/article/details/85339513