使用Python的Turtle模块绘制卡通兔子和“福”字

引言

Python的Turtle模块是一个经典的图形绘制库,非常适合编程初学者用来学习和实践基础的编程概念,如循环、条件语句和函数调用。本文将详细介绍如何使用Turtle模块绘制一个卡通兔子和一个“福”字,包括模块的基本使用、代码详细解析和最终的运行效果展示。

Turtle模块简介

Turtle是Python标准库中的一个模块,它提供了一个简单的绘图界面,通过控制一个“乌龟”(Turtle)的移动和转向,可以在屏幕上绘制出各种形状和图案。Turtle支持多种基本操作,如前进、后退、左转、右转、抬起和放下画笔、改变画笔颜色和大小等。

基本使用
  • import turtle as t: 导入Turtle模块。
  • t.pensize(size): 设置画笔粗细。
  • t.pencolor(color): 设置画笔颜色。
  • t.fillcolor(color): 设置填充颜色。
  • t.begin_fill(): 开始填充。
  • t.end_fill(): 结束填充。
  • t.goto(x, y): 移动到指定坐标。
  • t.seth(angle): 设置方向角度。
  • t.circle(radius, extent=None): 绘制圆弧。
  • t.forward(distance): 向前移动指定距离。
  • t.right(angle): 右转指定角度。
  • t.left(angle): 左转指定角度。
  • t.done(): 结束绘制。
运行效果

在这里插入图片描述

代码详细解释

在本例中,我们首先设置了画布的尺寸和背景色,然后使用一系列的Turtle方法绘制出兔子的头部、嘴巴、鼻子、眼睛、耳朵、身体、手和脚,最后绘制了“福”字。

  • 跳转jump(x, y)函数用于将乌龟移动到新的位置而不留下轨迹。
  • 兔子头部:通过多个circle()调用绘制出兔子头部的轮廓。
  • 兔子嘴巴:使用circle()forward()结合绘制出兔子的嘴巴和鼻翼。
  • 兔子眼睛:使用嵌套的circle()来绘制眼睛的白色部分和黑色瞳孔。
  • 兔子耳朵:使用数学公式计算耳朵的位置,再通过circle()goto()绘制。
  • 兔子身体:通过两个半圆和直线构成的身体轮廓。
  • 兔子手和脚:使用circle()forward()绘制。
  • 福字:先绘制一个正方形作为“福”字的背景,然后使用write()方法在正方形内写上“福”字。
完整代码

import turtle as t
import math


def jump(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()


# 设置画布尺寸
t.screensize(400, 300, "#FFE4C4")
t.pensize(1)
t.pencolor("black")
t.speed(3)
t.hideturtle()

# 画兔头
t.fillcolor("white")
jump(0, 80)
t.seth(0)
t.begin_fill()
t.circle(-60, 90)
t.left(90)
t.circle(-25, 160)
t.circle(-200, 20)
t.goto(0, -40)
jump(0, 80)
t.seth(180)
t.circle(60, 90)
t.right(90)
t.circle(25, 160)
t.circle(200, 20)
t.goto(0, -40)
t.end_fill()

# 画兔嘴巴
jump(0, 0)
t.right(60)
t.circle(10, 135)
jump(0, 0)
t.seth(0)
t.right(120)
t.circle(-10, 135)
jump(-10, -5)
t.seth(0)
t.right(90)
t.fillcolor("pink")
t.begin_fill()
t.forward(5)
t.circle(10, 180)
t.forward(5)

# 画兔鼻子
jump(0, 0)
t.seth(0)
t.circle(3)
t.end_fill()

# 画兔眼睛
# 左眼
jump(-35, 5)
t.seth(0)
t.fillcolor("white")
t.begin_fill()
t.circle(12)
t.end_fill()
t.fillcolor("black")
t.begin_fill()
t.circle(10)
t.end_fill()
# 右眼
jump(35, 5)
t.seth(0)
t.fillcolor("white")
t.begin_fill()
t.circle(12)
t.end_fill()
t.fillcolor("black")
t.begin_fill()
t.circle(10)
t.end_fill()

# 画兔耳朵
x1 = -5
y1 = math.sqrt(80 ** 2 - x1 ** 2)
x2 = -20
y2 = math.sqrt(80 ** 2 - x2 ** 2)
x3 = -10
y3 = math.sqrt(80 ** 2 - x3 ** 2)
x4 = -15
y4 = math.sqrt(80 ** 2 - x4 ** 2)
jump(x1, y1)
t.seth(90)
t.fillcolor("white")
t.begin_fill()
t.circle(100, 30)
t.circle(15, 180)
t.end_fill()
t.fillcolor("pink")  # 左耳上色
jump(x3, y4)
t.seth(90)
t.begin_fill()
t.circle(100, 20)
t.circle(8, 180)
t.goto(x4, y4)
t.end_fill()
x5 = 5
y5 = math.sqrt(80 ** 2 - x5 ** 2)
x6 = 20
y6 = math.sqrt(80 ** 2 - x6 ** 2)
x7 = 10
y7 = math.sqrt(80 ** 2 - x7 ** 2)
x8 = 15
y8 = math.sqrt(80 ** 2 - x8 ** 2)
jump(x5, y5)
t.seth(90)
t.fillcolor("white")
t.begin_fill()
t.circle(-100, 30)
t.circle(-15, 180)
t.goto(x6, y6)
t.end_fill()
t.fillcolor("pink")  # 右耳上色
jump(x7, y7)
t.seth(90)
t.begin_fill()
t.circle(-100, 20)
t.circle(-8, 180)
t.goto(x8, y8)
t.end_fill()

# 画兔身
t.fillcolor("white")
jump(0, -40)
t.seth(180)
t.begin_fill()
t.circle(-200, 15)
t.seth(-135)
t.circle(100, 25)
t.circle(60, 90)
t.goto(0, -150)
jump(0, -40)
t.seth(0)
t.circle(200, 15)
t.seth(-45)
t.circle(-100, 25)
t.circle(-60, 90)
t.goto(0, -150)
t.end_fill()

# 画兔手和脚
t.fillcolor("white")
jump(-50, -40)
t.seth(0)
t.begin_fill()
t.forward(8)
t.circle(-15, 180)
t.forward(8)
t.circle(-15, 180)
jump(50, -40)
t.seth(180)
t.forward(8)
t.circle(15, 180)
t.forward(8)
t.circle(15, 180)
t.end_fill()
t.fillcolor("white")
jump(-50, -150)
t.seth(135)
t.begin_fill()
t.forward(12)
t.circle(-15, 180)
t.forward(12)
t.circle(-15, 180)
jump(50, -150)
t.seth(45)
t.forward(12)
t.circle(15, 180)
t.forward(12)
t.circle(15, 180)
t.end_fill()

# 福字
t.fillcolor("#CD4F39")  # 红纸
jump(0, -30)
t.seth(-45)
t.begin_fill()
for i in range(4):
    t.forward(90)
    t.right(90)
t.end_fill()

jump(-28, -125)
t.write("福", font=("Arial", 40, "normal"))

t.done()

结语

通过本教程,我们不仅学习了如何使用Turtle模块绘制复杂的图形,还掌握了如何利用基本的数学知识来精确控制图形的位置和大小。希望这篇博客能激发你对Python图形编程的兴趣,并鼓励你尝试创作自己的作品!


这段代码展示了Turtle模块的强大功能和灵活性,通过组合简单的图形和控制指令,可以创造出丰富多样的视觉效果。试着运行代码,欣赏一下你亲手绘制的卡通兔子和“福”字吧!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱写代码的小朋友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值