
📢📢📢📣📣📣
🌻🌻🌻Hello,大家好我叫是Dream呀,一個有趣的Python博主,多多關照😜😜😜
🏅🏅🏅CSDN Python領域優質創作者,大二在讀,歡迎大家找我合作學習(文末有VX 想進學習交流群or學習資料 歡迎+++)
💕入門須知:這片樂園從不缺乏天才,努力才是你的最終入場券!🚀🚀🚀
💓最后,愿我們都能在看不到的地方閃閃發光,一起加油進步🍺🍺🍺
🍉🍉🍉“一萬次悲傷,依然會有Dream,我一直在最溫暖的地方等你”,唱的就是我!哈哈哈~🌈🌈🌈
🌟🌟🌟???
前言:時光飛快,歲月荏苒,轉眼間,2022年虎年要來了!Dream在這里祝所有朋友們:虎年快樂,虎虎生威,所遇皆所求!
悄悄告訴大家:凡是三連此文章的小伙伴,來年都可以走桃花運,脫單脫到手軟~(評論區是相親區,歡迎大家留言自己優點和對另一半的期望,快去匹配心動程式猿吧🙈🙈🙈)
🍉🍉🍉凡是在此評論區成功牽手者,前來找我領取紀念禮品喲~哈哈哈,開個玩笑,我們步入正題!
Python打造虎年祝福神器
- 背景故事👻👻👻
- 制作程序🚀🚀🚀
- 一、Python Turtle模塊畫小老虎
- 1. 定義庫以及初始化界面
- 2. 畫出左右兩只耳朵
- 3. 畫出小老虎頭部輪廓
- 4. 畫出老虎的兩只眼睛
- 5. 畫出老虎的鼻子和嘴巴
- 6. 畫出小老虎的左右肢體和腳趾
- 7. 在需要的位置寫上我們的新年祝福
- 二、彈窗設定
- 三、倒計時頁面設計
- 1. 實作清屏功能以及初始化位置
- 2. 顯示倒數3,2,1
- 3. 顯示我們需要的文字
- 4. 設定代碼運行入口,呼叫目標函式
- 結果展示💕💕💕
- 原始碼分享🌈🌈🌈
- exe打包檔案自取??????
背景故事👻👻👻
🌻🌻🌻2022虎年將至,值此新春佳節之際,各大社區更是你爭我趕紛紛發起春節征文活動,作為CSDN的一位老朋友,我不允許我所在社區的小伙伴們還沒有自己特殊的虎年神器,于是經過一晚上…又一晚上…又又又一晚上…的思考,我還是沒有思路😭 😭 😭
正當我一籌莫展之際,幾位粉絲朋友們的小請求點醒了我:



對呀,我何不用Python畫一個老虎出來呢,加之增添幾個功能,打造成一款虎年祝福神器!我瞬間靈感爆發,話不多說,先看成品🏃🏃🏃:
首先是剛打開時的倒數界面,神秘感十足:

倒數結束后,來到我們的展示環節:

最后,是我們的成果,一直可愛的小老虎以及滿屏的彈窗祝福:

看到這,是不是好奇心十足呢,先不要著急,看在博主這么辛苦的份上,給小Dream來個一鍵三連吧~😜😜😜
謝謝大家,大家前排就坐:
制作程序🚀🚀🚀
一、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()
結果展示💕💕💕
最后就是我們的結果啦,快去試試吧!如果有任何不懂的地方,歡迎在最下方添加我的vx,樂意為你排憂解難~

原始碼分享🌈🌈🌈
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()
exe打包檔案自取??????
考慮到大家可能對Python不太了解,我在這里為大家打包好了可以直接運行的exe檔案,大家直接發送給需要的人就可以啦,大家自取,別忘了五星好評喲~
虎年畫虎祝福已經打包的exe檔案,直接可以用,需要自取.zip
🌲🌲🌲 好啦,這就是今天要分享給大家的全部內容了,我們下期再見!
??????如果你喜歡的話,就不要吝惜你的一鍵三連了~


轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/413529.html
標籤:其他
