Python打造虎年祝福神器的示例代碼
背景故事
2022虎年將至,值此新春佳節(jié)之際,各大社區(qū)更是你爭我趕紛紛發(fā)起春節(jié)征文活動正當我一籌莫展之際,幾位粉絲朋友們的小請求點醒了我:


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

倒數(shù)結(jié)束后,來到我們的展示環(huán)節(jié):

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

制作過程
一、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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)3度
? ? ? ? ? ? t.fd(a) ?# 向前走a的步長
? ? ? ? else:
? ? ? ? ? ? a = a - 0.03
? ? ? ? ? ? t.lt(3)
? ? ? ? ? ? t.fd(a)
? ? t.end_fill()
? ? # 每個腳趾繪制函數(shù)
? ? 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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)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"))看到這,我們的小老虎部分就已經(jīng)大功告成了,大家可以先欣賞一下我們的小老虎:

二、彈窗設置
在必要處修改我們的數(shù)據(jù)就可以啦,大家以后都可以拿這個去用!
# 彈窗設置
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. 實現(xiàn)清屏功能以及初始化位置
import turtle
import time
import random
import tkinter as tk
import threading
# 實現(xiàn)清屏
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) ? ? ?#后退
# 初始化海龜?shù)奈恢?
def go_start(x, y, state):
? ? turtle.pendown() if state else turtle.penup()
? ? turtle.goto(x, y)
? ? #畫線,state為真時海龜回到原點,為假時不回到原來的出發(fā)點
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. 顯示倒數(shù)3,2,1
#顯示倒數(shù)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. 設定代碼運行入口,調(diào)用目標函數(shù)
number=[3,2,1] #儲存顯示界面倒數(shù)數(shù)字1,2,3
if __name__ == '__main__':
turtle.setup(900, 500) #調(diào)畫布的尺寸
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): # 需要的彈框數(shù)量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.01)
threads[i].start()
結(jié)果展示
最后就是我們的結(jié)果啦,快去試試吧!

源碼分享
import turtle
import time
import random
import tkinter as tk
import threading
# 實現(xiàn)清屏
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) ? ? ?#后退
# 初始化海龜?shù)奈恢?
def go_start(x, y, state):
? ? turtle.pendown() if state else turtle.penup()
? ? turtle.goto(x, y)
#畫線,state為真時海龜回到原點,為假時不回到原來的出發(fā)點
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()
#顯示倒數(shù)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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)3度
? ? ? ? ? ? t.fd(a) ?# 向前走a的步長
? ? ? ? else:
? ? ? ? ? ? a = a - 0.03
? ? ? ? ? ? t.lt(3)
? ? ? ? ? ? t.fd(a)
? ? t.end_fill()
? ? # 每個腳趾繪制函數(shù)
? ? 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) ?# 向左轉(zhuǎn)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) ?# 向左轉(zhuǎn)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] ? ?#儲存顯示界面倒數(shù)數(shù)字1,2,3
if __name__ == '__main__':
? ? turtle.setup(900, 500) ? ? #調(diào)畫布的尺寸
? ? 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): ?# 需要的彈框數(shù)量
? ? ? ? t = threading.Thread(target=dow)
? ? ? ? threads.append(t)
? ? ? ? time.sleep(0.01)
? ? ? ? threads[i].start()以上就是Python打造虎年祝福神器的示例代碼的詳細內(nèi)容,更多關(guān)于Python祝福神器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中*args與**kwarsg及閉包和裝飾器的用法
這篇文章主要介紹了python中*args與**kwarsg及閉包和裝飾器的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
python神經(jīng)網(wǎng)絡學習數(shù)據(jù)增強及預處理示例詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡學習數(shù)據(jù)增強及預處理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
5道關(guān)于python基礎 while循環(huán)練習題
這篇文章主要給大家分享的是5道關(guān)于python基礎 while循環(huán)練習題,無論學習什么語言,練習都是必不可少的,下面文章的練習題挺精湛的,需要的朋友可以參考一下2021-11-11
如何在Python中利用matplotlib.pyplot畫出函數(shù)圖詳解
通過圖像可以直觀地學習函數(shù)變化、分布等規(guī)律,在學習函數(shù)、概率分布等方面效果顯著,下面這篇文章主要給大家介紹了關(guān)于如何在Python中利用matplotlib.pyplot畫出函數(shù)圖的相關(guān)資料,需要的朋友可以參考下2022-08-08
Python定時發(fā)送消息的腳本:每天跟你女朋友說晚安
今天小編就為大家分享一篇關(guān)于Python定時發(fā)送消息的腳本:每天跟你女朋友說晚安的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
詳解OpenCV中直方圖,掩膜和直方圖均衡化的實現(xiàn)
這篇文章主要為大家詳細介紹了OpenCV中直方圖、掩膜、直方圖均衡化詳細介紹及代碼的實現(xiàn),文中的示例代碼講解詳細,需要的可以參考一下2022-11-11

