2022皆为过往,2023平安健康

2022已经过去,2023正在继续,希望我们都能在平凡的日子里,熠熠生辉。
本文介绍运用Python中的turtle库控制函数画生肖兔,并设置了2023年幸运词,快截屏看看你的幸运词吧。
本文目录
  1. 效果展示

  2. 代码详解

    2.1 导入库

    2.2 播放音乐

    2.3 写2022年文字

    2.4 画兔子的外轮廓

    2.5 画兔子的上半身


一、效果展示

在介绍代码之前,先来看下本文的实现效果。

可以参考Pinstaller(Python打包为exe文件)一文把Python文件转化成exe,发给未安装Python的Ta。

如果喜欢纯享版的,也可以看下如下效果:

二、代码详解
Python绘制生肖兔的原理是:应用turtle库控制函数绘制不同曲线构成效果图。

 1   导入库

首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。

# -*- coding: UTF-8 -*-'''代码用途 :画小白兔作者     :阿黎逸阳博客     :  https://blog.csdn.net/qq_32532663/article/details/106176609'''import osimport timeimport pygameimport random import turtle as t from time import sleep

本文应用到的库较少,只应用了os、time、pygame、randomturtle五个库。

os库可以设置文件读取的位置。

time库可以设置程序休眠的时间,达到动态图的效果。

pygame库是为了绘制过程更有趣,在绘图过程中添加背景音乐。

random库用来生成随机数。

turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。


 2   播放音乐

接着应用pygame库播放背景音乐,本文的音乐是《人间不值得·Rhythm(《逆水寒》大宋新年特制版)

#播放音乐print('播放音乐')pygame.mixer.init()pygame.mixer.music.load(r"F:\公众号\67.2023春节\万象凡音,雪糕超人耶 - 人间不值得·Rhythm(《逆水寒》大宋新年特制版).mp3") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(1, 0)
这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。

如果选择播放音乐,需要在代码music.load函数中把你想放音乐的电脑本地存放地址填进去。

有部分朋友对这一块有疑问,填充格式可参考如下图片:


 3   写2022年文字

然后设置画板的大小,并写开篇的文字。

t.title('阿黎逸阳的代码公众号')t.speed(10)#t.screensize(1000, 800)t.setup(startx=0, starty = 0, width=800, height = 600)t.bgcolor('red')def write_1(x, y,  ss):    t.hideturtle()    t.penup()    t.goto(x, y)    t.pendown()    t.pencolor('white')    t.write(ss, font=('Times New Roman', 70, 'normal'))def write_2(x, y,  ss):    t.hideturtle()    t.penup()    t.goto(x, y)    t.pendown()    t.pencolor('white')    t.write(ss, font=('Times New Roman', 35, 'normal'))def write_3(x, y,  ss):    t.hideturtle()    t.penup()    t.goto(x, y)    t.pendown()    t.pencolor('white')    t.write(ss, font=('Kai Ti', 12, 'normal', 'bold'))def write_4(x, y,  ss):    t.hideturtle()    t.penup()    t.goto(x, y)    t.pendown()    t.pencolor('white')    t.write(ss, font=('Times New Roman', 50, 'normal'))write_1(-100, 100,  '2 0 2 2')time.sleep(1)write_4(-105, 10,  '皆为过往')time.sleep(1)write_4(-55, -80,  '翻篇!')time.sleep(2)t.clear()

关键代码详解:

t.setup():设置画布的尺寸和位置。

t.bgcolor(color):设置画布的背景颜色。

t.penup():抬起画笔,一般用于另起一个地方绘图使用。

t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。

t.pendown():放下画笔,一般和penup组合使用。

t.pencolor(color):设置画笔的颜色。

t.write():设置字体的大小、颜色、类型等。

time.sleep()睡眠一段时间。

t.clear():清屏。


 4   画兔子的外轮廓

接着画兔子的外轮廓。

print('画右耳朵')#画右耳朵t.penup()t.goto(20, -50)t.pendown()     t.pensize(2)t.color('black', 'white')t.begin_fill()t.setheading(80)t.circle(-100, 60)t.circle(-50, 20)t.right(85)t.circle(-100, 65)print('画右上脸')#右上脸t.setheading(-40)t.circle(-80, 40)print('画右下脸')#右下脸t.left(20)t.circle(-50, 90)print('画身子')#手臂t.setheading(-40)t.circle(-100, 69)#横线t.setheading(180)t.forward(170)#手臂t.setheading(110)t.circle(-100, 69)print('画左下脸')#左下脸t.left(115)t.circle(-50, 90)print('画左上脸')#左上脸t.setheading(85)t.circle(-80, 40)print('画左耳朵')#左耳朵t.setheading(140)t.circle(-100, 55)t.circle(-50, 20)t.right(85)t.circle(-100, 66)print('画脑袋上的横线')#脑袋上的横线t.setheading(10)t.circle(-100, 22)t.end_fill()print('画右内耳朵')

关键代码详解:

t.pensize(width):设置画笔的尺寸。

t.color(color1, color2):设置画笔的颜色和填充颜色。

t.left(degree):画笔向左转多少度,括号里表示度数。

t.right(degree):画笔向右转多少度,括号里表示度数。

t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数

画兔子外轮廓的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得兔子的轮廓比较流畅。


 5   画兔子的上半身

最后控制函数画兔子的上半身。

#画右内耳朵t.penup()t.goto(31, -50)t.pendown()     t.pensize(2)t.color('pink')t.begin_fill()t.setheading(77)t.circle(-100, 46)t.circle(-50, 20)t.right(105)t.circle(-100, 55)t.end_fill()print('画左内耳朵')#画左内耳朵t.penup()t.goto(-47, -55)t.pendown()     t.pensize(2)t.color('pink')t.begin_fill()t.setheading(140)t.circle(-100, 42)t.circle(-50, 20)t.right(105)t.circle(-100, 54)t.end_fill()print('画左眼睛')#画左眼睛t.penup()t.goto(-37, -110)t.pendown()     t.pensize(2)t.color('black')t.begin_fill()t.circle(8, 360)t.end_fill()print('画右眼睛')#画右眼睛t.penup()t.goto(23, -110)t.pendown()     t.pensize(2)t.color('black')t.begin_fill()t.circle(8, 360)t.end_fill()print('画左眼珠')#画左眼珠t.penup()t.goto(-34, -105)t.pendown()     t.pensize(2)t.color('white')t.begin_fill()t.circle(2, 360)t.end_fill()print('画右眼珠')#画右眼珠t.penup()t.goto(27, -105)t.pendown()     t.pensize(2)t.color('white')t.begin_fill()t.circle(2, 360)t.end_fill()print('画鼻子')#画鼻子t.penup()t.goto(-5, -125)t.pendown()t.color('black')t.begin_fill()t.setheading(45)t.circle(-10, 100)t.right(82)t.circle(-10, 90)t.end_fill()print('画嘴')#画嘴t.penup()t.color('black', 'red')t.pensize(0.5)t.goto(2, -130)t.pendown()t.begin_fill()t.setheading(-90)t.circle(-7, 95)t.setheading(-80)t.circle(8, 174)t.setheading(193)t.circle(-7, 100)t.end_fill()#画左嘴t.penup()t.goto(2, -130)t.pendown()t.pensize(2)t.color('black')t.setheading(-90)t.circle(-8, 110)#画右嘴t.penup()t.goto(2, -130)t.pendown()t.color('black')t.setheading(-90)t.circle(7122)

至此,在Python中实现画兔子的逻辑已大致讲解完毕,感兴趣的朋友可以尝试自己实现一下。

往期回顾:
520表白代码合集
一文囊括Python中的函数,持续更新。。。
一文囊括Python中的有趣案例,持续更新。。。
让你彻底弄懂Python编程经典案例【考题】之反转一个值
Python编程经典案例【考题】自由落体运动球的运动轨迹
六一儿童节来临,送大朋友小朋友一只最近爆火的“可达鸭”


扫一扫关注我

13162366985

投稿微信号、手机号

请使用浏览器的分享功能分享到微信等