Turtle库是Python中非常常用的绘制图像函数库
画笔状态
penup():抬起画笔;
pendown():落下画笔;
pensize(width):画笔宽度;
画笔运动
forward(d)/fd(d): 向前移动距离d backward(d)/bd(d): 向后移动距离d left(angle):向左转angle度; right(angle):向右转angle度; goto(x,y):移动到绝对位置; home() : 移动到原点; circle(r, extent = None):绘制半径为r,角度为extent的弧形; setheading(angle)/seth(angle):改变前进方向; undo():撤销最后的动作; speed():将速度设置为0..10范围内整数;
eg:
太阳花
from turtle import * color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos()) < 1: break end_fill() done()
平安果
from turtle import * setup(600, 600, 0, 0) pensize(5) up() goto(0, -200) down() # 先画一个圆,并填充为红色 begin_fill() color("red") circle(radius=150) end_fill() # 画苹果把儿 color("brown") penup() goto(-90, 0) pendown() circle(180, 40) penup() setheading(105) goto(-20, 20) pendown() circle(180, 50) pendown() # 画左边叶子 begin_fill() color("green") circle(180, 50) setheading(-30) circle(180, 55) end_fill() # 画右边叶子 begin_fill() color("green") setheading(0) circle(180, 50) setheading(-180) circle(180, 50) end_fill() done()
玫瑰花
from turtle import * # 设置初始位置 penup() left(90) forward(200) pendown() right(90) # 花蕊 fillcolor("blue") begin_fill() circle(10,180) circle(25,110) left(50) circle(60,45) circle(20,170) right(24) forward(30) left(10) circle(30,110) forward(20) left(40) circle(90,70) circle(30,150) right(30) forward(15) circle(80,90) left(15) forward(45) right(165) forward(20) left(155) circle(150,80) left(50) circle(150,90) end_fill() # 花瓣1 left(150) circle(-90,70) left(20) circle(75,105) setheading(60) circle(80,98) circle(-90,40) # 花瓣2 left(180) circle(90,40) circle(-80,98) setheading(-83) # 叶子1 forward(30) left(90) forward(25) left(45) fillcolor("green") begin_fill() circle(-80,90) right(90) circle(-80,90) end_fill() right(135) forward(60) left(180) forward(85) left(90) forward(80) # 叶子2 right(90) right(45) fillcolor("green") begin_fill() circle(80,90) left(90) circle(80,90) end_fill() left(135) forward(60) left(180) forward(60) right(90) circle(200,60) done()
红旗
from turtle import * setup(600, 400, 0, 0) bgcolor("red") fillcolor("yellow") color('yellow') speed(10) # 主星 begin_fill() up() goto(-280, 100) down() for i in range(5): forward(150) right(144) end_fill() # 副星一 begin_fill() up() goto(-100, 180) setheading(305) down() for i in range(5): forward(50) left(144) end_fill() # 副星二 begin_fill() up() goto(-50, 110) setheading(30) down() for i in range(5): forward(50) right(144) end_fill() # 副星三 begin_fill() up() goto(-40, 50) setheading(5) down() for i in range(5): forward(50) right(144) end_fill() # 副星四 begin_fill() up() goto(-100, 10) setheading(300) down() for i in range(5): forward(50) left(144) end_fill() hideturtle() done()
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20453.html