Python打造虎年祝福神器的示例代码
作者:是Dream呀 发布时间:2021-01-08 17:23:43
标签:Python,虎年,祝福神器
背景故事
2022虎年将至,值此新春佳节之际,各大社区更是你争我赶纷纷发起春节征文活动正当我一筹莫展之际,几位粉丝朋友们的小请求点醒了我:
对呀,我何不用Python画一个老虎出来呢,加之增添几个功能,打造成一款虎年祝福神器!我瞬间灵感爆发,话不多说,先看成品:
首先是刚打开时的倒数界面,神秘感十足:
倒数结束后,来到我们的展示环节:
最后,是我们的成果,一直可爱的小老虎以及满屏的弹窗祝福:
制作过程
一、Python Turtle模块画小老虎
在这里,我们使用了Python中的一个非常好玩的库:Turtle,也就是我们常说的海龟画图!不懂的同学可以自行参考学习这篇文章,在这里不做过多的讲解:海龟画图全解–值得你一看!
1. 定义库以及初始化界面
def laohu():
import turtle as t
# 设置幕布大小及颜色
t.screensize(50, 50, bg='yellow')
t.title("老虎宝宝")
t.shape("classic")
t.pensize(10)
t.color("orange")
t.fillcolor("pink")
t.speed(100)
t.hideturtle()
2. 画出左右两只耳朵
# 左耳
t.penup()
t.goto(-105, 97)
t.setheading(160)
t.begin_fill()
t.pendown()
t.circle(-30, 230)
t.setheading(180)
t.circle(37, 90)
t.end_fill()
# 右耳
t.penup()
t.goto(105, 97)
t.setheading(20)
t.begin_fill()
t.pendown()
t.circle(30, 230)
t.setheading(0)
t.circle(-37, 90)
t.end_fill()
3. 画出小老虎头部轮廓
# 头部轮廓
t.penup()
t.goto(-67, 140)
t.setheading(30)
t.pendown()
t.circle(-134, 60)
t.penup()
t.goto(-50, -25)
t.setheading(180)
t.pendown()
t.circle(-100, 30)
t.circle(-30, 90)
t.setheading(100)
t.circle(-200, 20)
t.penup()
t.goto(50, -25)
t.setheading(0)
t.pendown()
t.circle(100, 30)
t.circle(30, 90)
t.setheading(80)
t.circle(200, 20)
4. 画出老虎的两只眼睛
# 两虎眼
# 左眼
t.penup()
t.goto(-90, 25)
t.setheading(-45)
t.fillcolor("orange")
t.begin_fill()
t.pendown()
# 椭圆绘制技巧
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.1
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.1
t.lt(3)
t.fd(a)
t.end_fill()
t.fillcolor("pink")
t.penup()
t.goto(-53, 43)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(19, 360)
t.end_fill()
t.penup()
t.pensize(4)
t.goto(-60, 57)
t.setheading(30)
t.pendown()
t.circle(-12, 60)
# 右眼
t.penup()
t.goto(90, 25)
t.setheading(45)
t.pensize(2)
t.fillcolor("orange")
t.begin_fill()
t.pendown()
# 椭圆绘制技巧
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.1
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.1
t.lt(3)
t.fd(a)
t.end_fill()
t.fillcolor("pink")
t.penup()
t.goto(53, 43)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(13, 360)
t.end_fill()
t.penup()
t.pensize(4)
t.goto(60, 57)
t.setheading(150)
t.pendown()
t.circle(12, 60)
5. 画出老虎的鼻子和嘴巴
# 鼻子和嘴吧
t.penup()
t.goto(-16, 20)
t.setheading(-90)
t.fillcolor("pink")
t.begin_fill()
t.pendown()
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3)
t.fd(a)
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
t.penup()
t.goto(-24, 0)
t.setheading(-60)
t.pendown()
t.circle(28, 120)
6. 画出小老虎的左右肢体和脚趾
# 小老虎肢体
# 左肢
t.color("orange")
t.penup()
t.goto(-65, -24)
t.setheading(-140)
t.begin_fill()
t.pendown()
t.circle(100, 40)
t.setheading(180)
t.circle(30, 40)
t.setheading(-40)
t.circle(40, 40)
t.setheading(-150)
a = 0.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.05
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
elif 30 <= i < 60 or 90 <= i < 100:
a = a - 0.05
t.lt(3)
t.fd(a)
t.setheading(93)
t.circle(-150, 30)
t.end_fill()
t.penup()
t.goto(-85, -115)
t.setheading(-150)
t.color("pink", "pink")
t.begin_fill()
t.pendown()
a = 0.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
# 每个脚趾绘制函数
def toe(x, y):
t.begin_fill()
t.goto(x, y)
t.circle(3, 360)
t.end_fill()
t.penup()
toe(-98, -120)
toe(-96, -110)
toe(-88, -105)
toe(-80, -105)
# 右肢
t.color("orange")
t.penup()
t.goto(65, -24)
t.setheading(-40)
t.begin_fill()
t.pendown()
t.circle(-100, 40)
t.setheading(0)
t.circle(-30, 40)
t.setheading(-140)
t.circle(-40, 40)
t.setheading(-30)
a = 0.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.05
t.rt(3) # 向左转3度
t.fd(a) # 向前走a的步长
elif 30 <= i < 60 or 90 <= i < 100:
a = a - 0.05
t.rt(3)
t.fd(a)
t.setheading(87)
t.circle(150, 30)
t.end_fill()
t.penup()
t.goto(85, -115)
t.setheading(150)
t.color("pink", "pink")
t.begin_fill()
t.pendown()
a = 0.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
t.penup()
toe(98, -120)
toe(96, -110)
toe(88, -105)
toe(80, -105)
7. 在需要的位置写上我们的新年祝福
t.goto(-57, -140)
t.color("orange")
t.setheading(-20)
t.pendown()
t.circle(165, 40)
t.penup()
t.goto(0, 180)
t.write("祝大家虎年快乐,虎虎生威!",
align="center", font=("Times", 28, "bold"))
t.color("black")
t.penup()
t.goto(0, 80)
t.write("王",
align="center", font=("Times", 38, "bold"))
t.penup()
t.goto(0, -5)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
t.goto(0, -15)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
t.goto(0, -25)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
看到这,我们的小老虎部分就已经大功告成了,大家可以先欣赏一下我们的小老虎:
二、弹窗设置
在必要处修改我们的数据就可以啦,大家以后都可以拿这个去用!
# 弹窗设置
def dow():
window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
a = random.randrange(0, width)
b = random.randrange(0, height)
window.title('虎来喽!')
window.geometry("200x50" + "+" + str(a) + "+" + str(b))
tk.Label(window,
text='虎年快乐虎虎生威', # 标签的文字
bg='red', # 背景颜色
font=('..', 17), # 字体和字体大小
width=18, height=2 # 标签长宽
).pack() # 固定窗口位置
window.mainloop()
三、倒计时页面设计
1. 实现清屏功能以及初始化位置
import turtle
import time
import random
import tkinter as tk
import threading
# 实现清屏
def clear_screen():
turtle.screensize(50, 50, bg='yellow')
turtle.penup() #画笔抬起
turtle.goto(0,0) #定位到(0,0)
turtle.color('white')
turtle.pensize(800) #画笔粗细
turtle.pendown() #画笔落下
turtle.setheading(0) #设置朝向
turtle.fd(300) #前进
turtle.bk(600) #后退
# 初始化海龟的位置
def go_start(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
#画线,state为真时海龟回到原点,为假时不回到原来的出发点
def draw_line(length, angle, state):
turtle.pensize(1)
turtle.pendown()
turtle.setheading(angle)
turtle.fd(length)
turtle.bk(length) if state else turtle.penup()
turtle.penup()
2. 显示倒数3,2,1
#显示倒数3,2,1
def draw_0(i):
turtle.screensize(50, 50, bg='yellow')
turtle.speed(0)
turtle.penup()
turtle.hideturtle() # 隐藏箭头显示
turtle.goto(-50, -100)
turtle.color('red')
write = turtle.write(i, font=('宋体', 200, 'normal'))
time.sleep(1)
3. 显示我们需要的文字
# 显示文字
def draw_1():
turtle.penup()
turtle.hideturtle() #隐藏箭头显示
turtle.goto(-410, 0)
turtle.color('red')
write = turtle.write('叮咚~新年礼物到啦💕', font=('宋体', 60, 'normal'))
time.sleep(2)
4. 设定代码运行入口,调用目标函数
number=[3,2,1] #储存显示界面倒数数字1,2,3
if __name__ == '__main__':
turtle.setup(900, 500) #调画布的尺寸
for i in number:
turtle.screensize(50, 50, bg='yellow')
draw_0(i)
clear_screen()
turtle.screensize(50, 50, bg='yellow')
draw_1()
clear_screen()
turtle.screensize(50, 50, bg='yellow')
laohu()
time.sleep(5)
threads = []
for i in range(100): # 需要的弹框数量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.01)
threads[i].start()
结果展示
最后就是我们的结果啦,快去试试吧!
源码分享
import turtle
import time
import random
import tkinter as tk
import threading
# 实现清屏
def clear_screen():
turtle.screensize(50, 50, bg='yellow')
turtle.penup() #画笔抬起
turtle.goto(0,0) #定位到(0,0)
turtle.color('white')
turtle.pensize(800) #画笔粗细
turtle.pendown() #画笔落下
turtle.setheading(0) #设置朝向
turtle.fd(300) #前进
turtle.bk(600) #后退
# 初始化海龟的位置
def go_start(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
#画线,state为真时海龟回到原点,为假时不回到原来的出发点
def draw_line(length, angle, state):
turtle.pensize(1)
turtle.pendown()
turtle.setheading(angle)
turtle.fd(length)
turtle.bk(length) if state else turtle.penup()
turtle.penup()
#显示倒数3,2,1
def draw_0(i):
turtle.screensize(50, 50, bg='yellow')
turtle.speed(0)
turtle.penup()
turtle.hideturtle() # 隐藏箭头显示
turtle.goto(-50, -100)
turtle.color('red')
write = turtle.write(i, font=('宋体', 200, 'normal'))
time.sleep(1)
# 显示文字
def draw_1():
turtle.penup()
turtle.hideturtle() #隐藏箭头显示
turtle.goto(-410, 0)
turtle.color('red')
write = turtle.write('叮咚~新年礼物到啦💕', font=('宋体', 60, 'normal'))
time.sleep(2)
def laohu():
import turtle as t
# 设置幕布大小及颜色
t.screensize(50, 50, bg='yellow')
t.title("老虎宝宝")
t.shape("classic")
t.pensize(10)
t.color("orange")
t.fillcolor("pink")
t.speed(100)
t.hideturtle()
# 左耳
t.penup()
t.goto(-105, 97)
t.setheading(160)
t.begin_fill()
t.pendown()
t.circle(-30, 230)
t.setheading(180)
t.circle(37, 90)
t.end_fill()
# 右耳
t.penup()
t.goto(105, 97)
t.setheading(20)
t.begin_fill()
t.pendown()
t.circle(30, 230)
t.setheading(0)
t.circle(-37, 90)
t.end_fill()
# 头部轮廓
t.penup()
t.goto(-67, 140)
t.setheading(30)
t.pendown()
t.circle(-134, 60)
t.penup()
t.goto(-50, -25)
t.setheading(180)
t.pendown()
t.circle(-100, 30)
t.circle(-30, 90)
t.setheading(100)
t.circle(-200, 20)
t.penup()
t.goto(50, -25)
t.setheading(0)
t.pendown()
t.circle(100, 30)
t.circle(30, 90)
t.setheading(80)
t.circle(200, 20)
# 两虎眼
# 左眼
t.penup()
t.goto(-90, 25)
t.setheading(-45)
t.fillcolor("orange")
t.begin_fill()
t.pendown()
# 椭圆绘制技巧
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.1
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.1
t.lt(3)
t.fd(a)
t.end_fill()
t.fillcolor("pink")
t.penup()
t.goto(-53, 43)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(19, 360)
t.end_fill()
t.penup()
t.pensize(4)
t.goto(-60, 57)
t.setheading(30)
t.pendown()
t.circle(-12, 60)
# 右眼
t.penup()
t.goto(90, 25)
t.setheading(45)
t.pensize(2)
t.fillcolor("orange")
t.begin_fill()
t.pendown()
# 椭圆绘制技巧
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.1
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.1
t.lt(3)
t.fd(a)
t.end_fill()
t.fillcolor("pink")
t.penup()
t.goto(53, 43)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(13, 360)
t.end_fill()
t.penup()
t.pensize(4)
t.goto(60, 57)
t.setheading(150)
t.pendown()
t.circle(12, 60)
# 鼻子和嘴吧
t.penup()
t.goto(-16, 20)
t.setheading(-90)
t.fillcolor("pink")
t.begin_fill()
t.pendown()
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3)
t.fd(a)
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
t.penup()
t.goto(-24, 0)
t.setheading(-60)
t.pendown()
t.circle(28, 120)
# 小老虎肢体
# 左肢
t.color("orange")
t.penup()
t.goto(-65, -24)
t.setheading(-140)
t.begin_fill()
t.pendown()
t.circle(100, 40)
t.setheading(180)
t.circle(30, 40)
t.setheading(-40)
t.circle(40, 40)
t.setheading(-150)
a = 0.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.05
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
elif 30 <= i < 60 or 90 <= i < 100:
a = a - 0.05
t.lt(3)
t.fd(a)
t.setheading(93)
t.circle(-150, 30)
t.end_fill()
t.penup()
t.goto(-85, -115)
t.setheading(-150)
t.color("pink", "pink")
t.begin_fill()
t.pendown()
a = 0.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
# 每个脚趾绘制函数
def toe(x, y):
t.begin_fill()
t.goto(x, y)
t.circle(3, 360)
t.end_fill()
t.penup()
toe(-98, -120)
toe(-96, -110)
toe(-88, -105)
toe(-80, -105)
# 右肢
t.color("orange")
t.penup()
t.goto(65, -24)
t.setheading(-40)
t.begin_fill()
t.pendown()
t.circle(-100, 40)
t.setheading(0)
t.circle(-30, 40)
t.setheading(-140)
t.circle(-40, 40)
t.setheading(-30)
a = 0.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.05
t.rt(3) # 向左转3度
t.fd(a) # 向前走a的步长
elif 30 <= i < 60 or 90 <= i < 100:
a = a - 0.05
t.rt(3)
t.fd(a)
t.setheading(87)
t.circle(150, 30)
t.end_fill()
t.penup()
t.goto(85, -115)
t.setheading(150)
t.color("pink", "pink")
t.begin_fill()
t.pendown()
a = 0.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
t.penup()
toe(98, -120)
toe(96, -110)
toe(88, -105)
toe(80, -105)
t.goto(-57, -140)
t.color("orange")
t.setheading(-20)
t.pendown()
t.circle(165, 40)
t.penup()
t.goto(0, 180)
t.write("祝大家虎年快乐,虎虎生威!",
align="center", font=("Times", 28, "bold"))
t.color("black")
t.penup()
t.goto(0, 80)
t.write("王",
align="center", font=("Times", 38, "bold"))
t.penup()
t.goto(0, -5)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
t.goto(0, -15)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
t.goto(0, -25)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
# 弹窗设置
def dow():
window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
a = random.randrange(0, width)
b = random.randrange(0, height)
window.title('虎来喽!')
window.geometry("200x50" + "+" + str(a) + "+" + str(b))
tk.Label(window,
text='虎年快乐虎虎生威', # 标签的文字
bg='red', # 背景颜色
font=('..', 17), # 字体和字体大小
width=18, height=2 # 标签长宽
).pack() # 固定窗口位置
window.mainloop()
number=[3,2,1] #储存显示界面倒数数字1,2,3
if __name__ == '__main__':
turtle.setup(900, 500) #调画布的尺寸
for i in number:
turtle.screensize(50, 50, bg='yellow')
draw_0(i)
clear_screen()
turtle.screensize(50, 50, bg='yellow')
draw_1()
clear_screen()
turtle.screensize(50, 50, bg='yellow')
laohu()
time.sleep(5)
threads = []
for i in range(100): # 需要的弹框数量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.01)
threads[i].start()
来源:https://blog.csdn.net/weixin_51390582/article/details/122500075
0
投稿
猜你喜欢
- 1. 前言python除了丰富的第三方库外,本身也提供了一些内在的方法和底层的一些属性,大家比较常用的如dict、list、set、min、
- jinjia和vue.js默认的模板转义符都是{{}}目前的解决办法是修改vue.js的转义符,将原来的{{}}替换为其他标签,我改为{[]
- 上节我们介绍了表连接,更确切的说是inner joins內连接. 內连接仅选出两张表中互相匹配的记录.因此,这会导致有时我们需要的记录没有包
- asp使用fso读取驱动器信息:<%vv=drive()response.write vv funct
- Python的3.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的
- 背景使用python进行图像可视化,很多情况下都需要subplots将多幅图像绘制在一个figure中。因为使用频率足够高,那么程序员就需要
- 实现代码# -*- coding: cp936 -*-import re s1 = 'adkkdk's2 = 'ab
- 项目需求:浏览器中访问django后端某一条url(如:127.0.0.1:8080/get_book/),实时朝数据库中生成一千条数据并将
- 本文实例讲述了Python文件及目录操作的方法。分享给大家供大家参考。具体分析如下:在python中对文件及目录的操作一般涉及多os模块,o
- 题目:轮盘分为三部分: 一等奖, 二等奖和三等奖;轮盘转的时候是随机的,如果范围在[0,0.08)之间,代表一等奖,如果范围在[0.08,0
- 1. 线性表简介线性表是一种线性结构,它是由零个或多个数据元素构成的有限序列。线性表的特征是在一个序列中,除了头尾元素,每个元素都有且只有一
- 1、Model signalsdjango.db.models.signales 作用于django的model操作上的一系列信号1)pre
- 在python中,“np”一般是指“numpy”库,是第三方库“numpy”的别名。方法:利用命令“import numpy as np”将
- python的一个加速器包,这里不讲原理,只讲应用过程,以及给出几个小例子,直接写就行了; 另外还给出了numba如何定义使用List1、最
- 一、前言了解过flask的python开发者想必都知道flask中核心机制莫过于上下文管理,当然学习flask如果不了解其中的处理流程,可能
- xlsxwriter 简介用于以 Excel 2007+ XLSX 文件格式编写文件,相较之下 PhpSpreadsheet 支持更多的格式
- InstrRev描述:返回某字符串在另一个字符串中出现的从结尾计起的位置。语法:InstrRev(string1, string2
- 一、引用返回引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时。不要用返回引用来增加性能,引擎足够聪明来自己进行优化。仅在有合理的技
- 想要根据django中的模型和配置生成SQL语句,需要先进行一定的设置:首先需要在你的app文件夹中进入setting.py文件,里面有一个
- 问题起因最近要将一个文本分割成好几个topic,每个topic设计一个regressor,各regressor是相互独立的,最后汇总所有to