小海龟画图
小海龟:import turtle 是python种很流行的绘制图像的函数库,通过x轴y轴进行坐标移动,绘制图形。
常用语法
常用命令 说明 pen=turtle.Pen() 获取笔 pen.forward(100) backward() 向前走/后(像素长度) pen.left(90) right() 向左转/右转(角度) pen.circle(50) 画圆(半径)
画笔属性 说明 pen.speed(5) 画笔速度(1-10) pen.pencolor(‘red’) 画笔颜色(颜色) pen.pensize(5) 画笔粗细
画笔动作 说明 pen.penup() 画笔抬起 pen.pendown() 画笔落下 pen.goto(x,y) 将画笔移动到(x,y)
颜色填充 说明 pen.begin_fill() 开始填充 pen.fillcolor(‘red’) 填充颜色 pen.end_fill() 结束填充 pen.color(color1,color2) 同时设置笔的颜色,填充颜色
案例1:画一个正方形
import turtle
t = turtle.Pen() #获取笔
t.speed(1) #画笔速度
t.forward(100) #向前画
t.left(90) #左转
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
turtle.done()
案例2:圆形填充
import turtle
t = turtle.Pen()
t.pencolor('red')
t.forward(100)
t.begin_fill()
t.fillcolor('green')
t.circle(50)
t.end_fill()
turtle.done()
案例3:阶梯
import turtle
pen=turtle.Pen()
pen.speed(1)
pen.forward(100)
pen.right(90)
pen.forward(100)
pen.left(90)
pen.forward(100)
pen.right(90)
pen.forward(100)
pen.left(90)
pen.forward(100)
pen.right(90)
pen.forward(100)
turtle.done()
案例4:五连环
import turtle
pen=turtle.Pen()
pen.speed(10)
pen.pencolor('blue')
pen.pensize(5)
pen.circle(50)
pen.penup()
pen.forward(80)
pen.pendown()
pen.pencolor('black')
pen.circle(50)
pen.penup()
pen.forward(80)
pen.pendown()
pen.pencolor('red')
pen.circle(50)
pen.penup()
pen.left(90)
pen.forward(50)
pen.left(90)
pen.forward(50)
pen.pendown()
pen.pencolor('green')
pen.circle(50)
pen.penup()
pen.forward(80)
pen.pendown()
pen.pencolor('yellow')
pen.circle(50)
turtle.done()
案例6:糖葫芦
import turtle
t=turtle.Pen()
t.speed(10)
t.penup()
t.pencolor('red')
t.forward(100)
t.pendown()
t.begin_fill()
t.fillcolor('red')
t.circle(50)
t.end_fill()
t.goto(150,50)
t.begin_fill()
t.fillcolor('green')
t.circle(50)
t.end_fill()
t.penup()
t.goto(200,100)
t.pendown()
t.begin_fill()
t.fillcolor('yellow')
t.circle(50)
t.end_fill()
t.penup()
t.goto(250,150)
t.pendown()
t.begin_fill()
t.fillcolor('purple')
t.circle(50)
t.end_fill()
t.penup()
t.pencolor('orange')
t.pensize(7)
t.forward(29)
t.left(90)
t.forward(85)
t.left(137)
t.pendown()
t.forward(380)
turtle.done()
案例7:小汽车
import turtle
t=turtle.Pen()
t.pencolor('red')
t.begin_fill()
t.fillcolor('red')
t.left(90)
t.goto(0,200)
t.right(90)
t.goto(200,200)
t.left(90)
t.goto(200,100)
t.right(90)
t.goto(400,100)
t.right(90)
t.goto(400,0)
t.goto(0,0)
t.end_fill()
t.begin_fill()
t.fillcolor('black')
t.goto(50,0)
t.circle(50)
t.end_fill()
案例8:用不同的颜色的笔画方框
import turtle
t=turtle.Pen()
color = ['red','yellow','blue','green']
for x in range(100):
t.pencolor(color[x%4])
t.forward(x)
t.left(90)
案例9:用for画螺旋线
import turtle
t=turtle.Pen()
color = ['red','yellow','blue','green']
for x in range(100):
t.pencolor(color[x%4])
t.forward(x)
t.left(91)
案例10:for画五角星
import turtle
t=turtle.Pen()
t.begin_fill()
t.fillcolor('red')
for x in range(5):
t.forward(100)
t.right(144)
t.end_fill()
turtle.done()
案例11:max画图
import turtle
t=turtle.Pen()
for i in range(0,100):
t.write("max",font=("Arial",int((i+4)/4),"bold"))
t.penup()
t.forward(i*4)
t.pendown()
t.left(92)
案例12:max不同颜色
import turtle
t=turtle.Pen()
colors=['red','yellow','blue','green','purple']
for i in range(0,50):
t.write("max",font=("Arial",int(i+5/5),"bold"))
t.pencolor(colors[i%5])
t.penup()
t.forward(i*8)
t.pendown()
t.left(74)
标签: turtle ,python ,pen ,小海龟 ,forward ,90 ,100 ,fill
来源: https://blog.csdn.net/weixin_45969078/article/details/112135562