海龟绘图是python中的一个很常见的库,它可以实现绘画的功能。今天,小编为大家带来了一些海龟绘图的实例。
1、画一只海豚
源码如下:
import turtle
# 设置画布大小和背景颜色
turtle.setup(600, 400)
turtle.bgcolor("lightblue")
# 设置画笔颜色和粗细
turtle.pensize(3)
turtle.pencolor("black")
# 绘制海豚身体
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(80)
turtle.end_fill()
# 绘制海豚眼睛
turtle.penup()
turtle.goto(-30, 50)
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
# 绘制海豚鼻子
turtle.penup()
turtle.goto(0, 20)
turtle.pendown()
turtle.fillcolor("gray")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 绘制海豚嘴巴
turtle.penup()
turtle.goto(20, 0)
turtle.pendown()
turtle.pencolor("red")
turtle.right(90)
turtle.circle(20, 180)
# 绘制海豚尾巴
turtle.penup()
turtle.goto(80, 0)
turtle.pendown()
turtle.pencolor("black")
turtle.right(180)
turtle.circle(-50, 90)
turtle.circle(-20, 90)
# 隐藏海龟
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
2、画一朵花
源码如下:
import turtle
# 创建画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 设置画笔颜色和粗细
pen.color("green")
pen.pensize(5)
# 绘制龟壳
pen.begin_fill()
pen.circle(100)
pen.end_fill()
# 绘制头部
pen.penup()
pen.goto(0, 150)
pen.pendown()
pen.begin_fill()
pen.circle(50)
pen.end_fill()
# 绘制眼睛
pen.penup()
pen.goto(-30, 180)
pen.pendown()
pen.begin_fill()
pen.circle(10)
pen.end_fill()
pen.penup()
pen.goto(30, 180)
pen.pendown()
pen.begin_fill()
pen.circle(10)
pen.end_fill()
# 绘制嘴巴
pen.penup()
pen.goto(-50, 120)
pen.pendown()
pen.right(90)
pen.circle(50, 180)
# 绘制四肢
pen.penup()
pen.goto(-80, 50)
pen.pendown()
pen.right(45)
pen.forward(100)
pen.penup()
pen.goto(80, 50)
pen.pendown()
pen.left(90)
pen.forward(100)
pen.penup()
pen.goto(-80, -50)
pen.pendown()
pen.right(90)
pen.forward(100)
pen.penup()
pen.goto(80, -50)
pen.pendown()
pen.left(45)
pen.forward(100)
# 隐藏画笔
pen.hideturtle()
# 显示画布
canvas.mainloop()
3、画一只小船
源码如下:
import turtle
# 设置画布大小和背景颜色
turtle.setup(600, 400)
turtle.bgcolor("skyblue")
# 绘制船体
turtle.color("white")
turtle.begin_fill()
turtle.goto(-200, 0)
turtle.goto(-200, -50)
turtle.goto(200, -50)
turtle.goto(200, 0)
turtle.end_fill()
# 绘制船帆
turtle.color("lightblue")
turtle.begin_fill()
turtle.goto(0, 100)
turtle.goto(150, 0)
turtle.goto(0, -100)
turtle.end_fill()
# 绘制船舵
turtle.color("white")
turtle.begin_fill()
turtle.goto(-100, -25)
turtle.goto(-100, -75)
turtle.goto(-150, -50)
turtle.end_fill()
# 隐藏海龟
turtle.hideturtle()
# 显示绘图窗口
turtle.done()
4、画一只小猪佩奇
源码如下:
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor("#F5DEB3")
# 画佩奇的头部
turtle.penup()
turtle.goto(-100, 100)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(50)
turtle.end_fill()
# 画佩奇的眼睛
turtle.penup()
turtle.goto(-70, 130)
turtle.pendown()
turtle.begin_fill()
turtle.color("#FFFFFF")
turtle.circle(10)
turtle.end_fill()
turtle.penup()
turtle.goto(-70, 140)
turtle.pendown()
turtle.begin_fill()
turtle.color("#000000")
turtle.circle(5)
turtle.end_fill()
# 画佩奇的鼻子
turtle.penup()
turtle.goto(-50, 100)
turtle.pendown()
turtle.begin_fill()
turtle.color("#FFC0CB")
turtle.circle(10)
turtle.end_fill()
# 画佩奇的身体
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(50)
turtle.end_fill()
# 画佩奇的手臂
turtle.penup()
turtle.goto(-150, 0)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(-150, -20)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(20)
turtle.end_fill()
# 画佩奇的腿
turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(-80, -70)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(20)
turtle.end_fill()
# 画佩奇的尾巴
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(20, -50)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(40, -50)
turtle.pendown()
turtle.begin_fill()
turtle.color("#F4A460")
turtle.circle(20)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
最后感谢大家的支持!希望大家多多点赞