Python自带一套简单的图形开发工具 Turtle 小乌龟 用来画一些简单的二维图形 通过写代码来画画
- import turtle 导入turtle工具
- showturtle() 显示箭头指示 刚开始运行时 箭头指示默认在中心坐标(0,0)
- hideturtle() 隐藏箭头指示
- pendown() 将笔放置在画布上
- penup() 将笔离开画布
- forward(d) 向笔所指的方向前进距离d
- right(angle) 向笔所指的方向右转angle度
- left(angle) 向笔所指的方向左转angle度
- goto(x,y) 跳转到某一坐标x y
- setx(x) 单独设置x坐标
- sety(y) 单独设置y坐标
- circle(r,ext,steps) 画圈 r 半径 steps阶数
- pensize(n) 调整笔的粗细
- color? 设置颜色
- write(“文本”,font = (“字体” , 字号 , “bold”))
- done() 绘制结束
- speed(0) 设置绘制的速度0最快 10最慢
- setheading(n) 0东 90北 180西 270南
绘制一个五环图
import turtle
turtle.speed(10)
turtle.color("blue")
turtle.pensize(10)
turtle.penup()
turtle.goto(-110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("black")
turtle.pensize(10)
turtle.penup()
turtle.goto(0,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("red")
turtle.pensize(10)
turtle.penup()
turtle.goto(110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("yellow")
turtle.pensize(10)
turtle.penup()
turtle.goto(-55,-75)
turtle.pendown()
turtle.circle(45)
turtle.color("green")
turtle.pensize(10)
turtle.penup()
turtle.goto(55,-75)
turtle.pendown()
turtle.circle(45)
turtle.done()
绘制一个长方体
import turtle
turtle.goto(400, 0)
turtle.goto(400, 200)
turtle.goto(0, 200)
turtle.goto(0, 0)
turtle.penup()
turtle.goto(300, 100)
turtle.pendown()
turtle.goto(300, -100)
turtle.goto(-100, -100)
turtle.goto(-100, 100)
turtle.goto(300, 100)
turtle.goto(400, 200)
turtle.penup()
turtle.goto(300, -100)
turtle.pendown()
turtle.goto(400, 0)
turtle.penup()
turtle.goto(-100, -100)
turtle.pendown()
turtle.goto(0, 0)
turtle.penup()
turtle.goto(-100, 100)
turtle.pendown()
turtle.goto(0, 200)
turtle.done()
绘制填充图形
- fillcolor? 设置填充颜色的
- begin_fill() 在填充开始前调用
- end_fill() 结束填充
import turtle
turtle.speed(10)
turtle.pensize(3)
turtle.penup()
turtle.goto(-200,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("red")
turtle.circle(40,steps=3)
turtle.end_fill()
turtle.penup()
turtle.goto(-100,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color("blue")
turtle.circle(40,steps=4)
turtle.end_fill()
turtle.done()
|