目录
一、图形化程序设计——海龟绘图
>>> import turtle #导入 turtle 模块
命令 | 说明 |
turtle.backward(300) | bk() | back() | 向当前画笔的反方向移动300像素 |
turtle.forward(300) | fd() | 向当前画笔方向移动 300 像素 |
turtle.right(30) | rt() | 顺时针旋转30° |
turtle.left(30) | lt() | 顺时针旋转30° |
turtle.pendown() | pd() | down() | 下笔。这样,路径就会画出来 |
turtle.penup() | up() | pu() | 抬笔。这样,路径就不会画出来 |
turtle.goto(0,50) | 将画笔移动到坐标(0,50) |
turtle.speed(n) | 画笔移动的速度n=1(慢)~10(快),0(最快) |
turtle.circle(100) | 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆 |
命令 | 说明 |
turtle.pensize(size) | width(size) | size选择画笔的粗细大小 |
turtle.pencolor() | 画笔颜色 |
turtle.fillcolor(color string) | 绘制图像的填充颜色 |
turtle.color(color1,color2) | 同时设置pencolor=color1,fillcolor=coloe2 |
turtle.filling() | 返回当前是否在填充颜色 |
turtle.begin_fill() | 准备开始填充颜色 |
turtle.end_fill() | 填充完成 |
turtle.hideturtle() | ht() | 隐藏箭头显示 |
turtle.showturtle() | st() | 与hideturtle()函数对应 |
命令 | 说明 |
turtle.clear() | 清空turtle窗口,但是turtle的位置和状态不变 |
turtle.reset() | 清空窗口,重置turtle的状态为初始状态 |
turtle.undo() | 撤销上一个turtle动作 |
turtle.isvisible() | 返回当前turtle是否可见 |
stamp() | 复制当前图像 |
turtle.write(s[,font=("font-name",font_size,"fond_type")]) | 写文本,s为文本内容,fond是字体的参数,里面分别为字体名称、大小和类型 |
二、利用海龟绘图,输出四个矩形的图像
Python代码为:
import turtle
turtle.showturtle()
turtle.color("red")
turtle.pensize(5)
turtle.penup()
turtle.goto(5,5)
turtle.pendown()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.penup()
turtle.goto(-105,5)
turtle.pendown()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.penup()
turtle.goto(-105,-105)
turtle.pendown()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.penup()
turtle.goto(5,-105)
turtle.pendown()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
三、绘制螺旋线
import turtle
t = turtle.Pen()
for x in range(360):
t.forward(x)
t.left(59)
运行结果:
四、绘制太阳花
import turtle as t
import time
t.color("red", "yellow")
t.speed(10)
t.begin_fill()
for _ in range(25):
t.forward(200)
t.left(165)
t.end_fill()
time.sleep(5)
运行结果:
五、绘制蛇
from turtle import *
setup(900, 350, 200, 200)
up()
fd(-350)
down()
pensize(25)
color("purple")
# 控制画笔起始方向
seth(-40)
# 绘制蛇身
for i in range(4):
circle(60, 80)
circle(-60, 80)
circle(60, 80 / 2)
# 绘制蛇头
fd(60)
circle(32, 180)
fd(60 * 2 / 3)
done()
运行结果: