Turtle 简易教程 |
Turtle:海龟库 Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。 |
常用方法
|
举例
turtle.circle(radius, extent=None, steps=None) 描述:以给定半径画圆 参数: radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆; extent(弧度) (optional); steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)。 举例: circle(50) # 整圆; circle(50,steps=3) # 三角形; circle(120, 180) # 半圆 1) turtle.pensize():设置画笔的宽度; 2) turtle.pencolor():没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如”green”, “red”,也可以是RGB 3元组。 3) turtle.speed(speed):设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。 |
实战代码
太阳花
import turtle as timport time
t.color(“red”, “yellow”)
t.speed(10)
t.begin_fill()for _ in range(50): t.forward(200) t.left(170)
end_fill()
time.sleep(1)
五角星
import turtleimport time
turtle.pensize(5)
turtle.pencolor(“yellow”)
turtle.fillcolor(“red”)
turtle.begin_fill()for _ in range(5): turtle.forward(200) turtle.right(144)
turtle.end_fill()
time.sleep(2)
turtle.penup()
turtle.goto(-150, -120)
turtle.color(“violet”)
turtle.write(“Done”, font=(‘Arial’, 40, ‘normal’))
time.sleep(1)
机器猫(半身)
from turtle import *def go_to(x,y): penup() goto(x,y) pendown()def eyes(): fillcolor() begin_fill() tracer(False) a = 2.5 for i in range(120): if 0 < i < 30 or 60 <= i < 90: a -= 0.05 left(3) forward(a) else: a += 0.05 left(3) forward(a) tracer(True) end_fill()def bolack_eres(): go_to(-20,215) fillcolor(‘#000000’) begin_fill() circle(13) end_fill() pensize(6) go_to(20,200) seth(75) circle(-10,150) pensize(3) go_to(-30,198) fillcolor(‘#ffffff’) begin_fill() circle(5) end_fill()def face(): left(40) go_to(-90,35) fillcolor(‘#ffffff’) begin_fill() forward(180) left(45) circle(120,110) left(25) forward(117) left(35) circle(133,100) end_fill() go_to(63.56, 218.24) seth(90) eyes() seth(180) penup() fd(60) pendown() seth(90) eyes() penup() seth(180) fd(64)def beard(): go_to(-32,135) seth(165) forward(60) go_to(-32,125) seth(180) forward(60) go_to(-32,115) seth(195) forward(60) go_to(37,135) seth(15) forward(60) go_to(37,125) seth(0) forward(60) go_to(37,115) seth(-15) forward(60)def mouth(): go_to(5,148) seth(270) forward(100) seth(0) circle(120,50) seth(230) circle(-120,100)def nose(): go_to(20,185) fillcolor(‘#e70010’) begin_fill() circle(20) end_fill()def head(): penup() circle(160, 40) pendown() fillcolor(‘#00a0de’) begin_fill() circle(160, 280) end_fill()def body(): fillcolor(‘#00a0de’) begin_fill() go_to(100,31) seth(45) forward(60) right(10) forward(50) right(90) forward(40) right(65) forward(85) left(30) forward(130) right(2) forward(30) right(3) forward(120) seth(180) forward(100) seth(90) circle(50,180) seth(180) forward(90) seth(95) forward(150) right(4) forward(150) forward(100)def ling(): go_to(-100,30) 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() go_to(0,20) fillcolor(‘#ffd200’) begin_fill() circle(-20) end_fill() go_to(-12.5,10) forward(30) go_to(-20,5) forward(43.5) fillcolor(‘#000000’) begin_fill() go_to(0,-10) circle(5) end_fill() go_to(0,-20) seth(-90) backward(10)def koudai(): go_to(-88,10) seth(225) circle(123,270) go_to(-88,-70) seth(180) backward(180) seth(-90) circle(-90,180)def end(): head() face() bolack_eres() beard() mouth() nose() # body() ling() koudai()if __name__ == ‘__main__’: end() speed(9999) mainloop()