Python打造虎年祝福神器的示例代碼
背景故事
2022虎年將至,值此新春佳節(jié)之際,各大社區(qū)更是你爭(zhēng)我趕紛紛發(fā)起春節(jié)征文活動(dòng)正當(dāng)我一籌莫展之際,幾位粉絲朋友們的小請(qǐng)求點(diǎn)醒了我:
對(duì)呀,我何不用Python畫一個(gè)老虎出來呢,加之增添幾個(gè)功能,打造成一款虎年祝福神器!我瞬間靈感爆發(fā),話不多說,先看成品:
首先是剛打開時(shí)的倒數(shù)界面,神秘感十足:
倒數(shù)結(jié)束后,來到我們的展示環(huán)節(jié):
最后,是我們的成果,一直可愛的小老虎以及滿屏的彈窗祝福:
制作過程
一、Python Turtle模塊畫小老虎
在這里,我們使用了Python中的一個(gè)非常好玩的庫(kù):Turtle,也就是我們常說的海龜畫圖!不懂的同學(xué)可以自行參考學(xué)習(xí)這篇文章,在這里不做過多的講解:海龜畫圖全解–值得你一看!
1. 定義庫(kù)以及初始化界面
def laohu(): import turtle as t # 設(shè)置幕布大小及顏色 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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? else: ? ? ? ? ? ? a = a - 0.03 ? ? ? ? ? ? t.lt(3) ? ? ? ? ? ? t.fd(a) ? ? t.end_fill() ? ? # 每個(gè)腳趾繪制函數(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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? 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è)置
在必要處修改我們的數(shù)據(jù)就可以啦,大家以后都可以拿這個(gè)去用!
# 彈窗設(shè)置 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='虎年快樂虎虎生威', # 標(biāo)簽的文字 bg='red', # 背景顏色 font=('..', 17), # 字體和字體大小 width=18, height=2 # 標(biāo)簽長(zhǎng)寬 ).pack() # 固定窗口位置 window.mainloop()
三、倒計(jì)時(shí)頁(yè)面設(shè)計(jì)
1. 實(shí)現(xiàn)清屏功能以及初始化位置
import turtle import time import random import tkinter as tk import threading # 實(shí)現(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) ? ? ? ? #畫筆粗細(xì) ? ? turtle.pendown() ? ? ? ? ? #畫筆落下 ? ? turtle.setheading(0) ? ? ? ?#設(shè)置朝向 ? ? turtle.fd(300) ? ? ? #前進(jìn) ? ? turtle.bk(600) ? ? ?#后退 # 初始化海龜?shù)奈恢? def go_start(x, y, state): ? ? turtle.pendown() if state else turtle.penup() ? ? turtle.goto(x, y) ? ? #畫線,state為真時(shí)海龜回到原點(diǎn),為假時(shí)不回到原來的出發(fā)點(diǎn) 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. 設(shè)定代碼運(yùn)行入口,調(diào)用目標(biāo)函數(shù)
number=[3,2,1] #儲(chǔ)存顯示界面倒數(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 # 實(shí)現(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) ? ? ? ? #畫筆粗細(xì) ? ? turtle.pendown() ? ? ? ? ? #畫筆落下 ? ? turtle.setheading(0) ? ? ? ?#設(shè)置朝向 ? ? turtle.fd(300) ? ? ? #前進(jìn) ? ? turtle.bk(600) ? ? ?#后退 # 初始化海龜?shù)奈恢? def go_start(x, y, state): ? ? turtle.pendown() if state else turtle.penup() ? ? turtle.goto(x, y) #畫線,state為真時(shí)海龜回到原點(diǎn),為假時(shí)不回到原來的出發(fā)點(diǎn) 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 ? ? # 設(shè)置幕布大小及顏色 ? ? 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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? else: ? ? ? ? ? ? a = a - 0.03 ? ? ? ? ? ? t.lt(3) ? ? ? ? ? ? t.fd(a) ? ? t.end_fill() ? ? # 每個(gè)腳趾繪制函數(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的步長(zhǎng) ? ? ? ? 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的步長(zhǎng) ? ? ? ? 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")) # 彈窗設(shè)置 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='虎年快樂虎虎生威', ?# 標(biāo)簽的文字 ? ? ? ? ? ? ?bg='red', ?# 背景顏色 ? ? ? ? ? ? ?font=('..', 17), ?# 字體和字體大小 ? ? ? ? ? ? ?width=18, height=2 ?# 標(biāo)簽長(zhǎng)寬 ? ? ? ? ? ? ?).pack() ?# 固定窗口位置 ? ? window.mainloop() number=[3,2,1] ? ?#儲(chǔ)存顯示界面倒數(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打造虎年祝福神器的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python祝福神器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?4種實(shí)現(xiàn)定時(shí)任務(wù)的方案
這篇文章主要給大家分享了Python?4種實(shí)現(xiàn)定時(shí)任務(wù)的方案,運(yùn)用 while True: + sleep()、Timeloop 庫(kù)、threading.Timer 、內(nèi)置模塊 sched ,下面就來看看具體的實(shí)現(xiàn)過程吧2021-12-12Python真題案例之錯(cuò)位鍵盤?單詞長(zhǎng)度?字母重排詳解
這篇文章主要介紹了python實(shí)操案例練習(xí),本文給大家分享的案例中主要任務(wù)有錯(cuò)位鍵盤、單詞長(zhǎng)度、字母重排,需要的小伙伴可以參考一下2022-03-03python打開windows應(yīng)用程序的實(shí)例
今天小編就為大家分享一篇python打開windows應(yīng)用程序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06django admin 自定義替換change頁(yè)面模板的方法
今天小編就為大家分享一篇django admin 自定義替換change頁(yè)面模板的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python中可復(fù)用函數(shù)的6種實(shí)踐
為了實(shí)現(xiàn)可維護(hù)性,我們的Python函數(shù)應(yīng)該:小型、只做一項(xiàng)任務(wù);沒有重復(fù);有一個(gè)層次的抽象性;有一個(gè)描述性的名字和有少于四個(gè)參數(shù),下面我們就來看看這6個(gè)特性的實(shí)踐吧2023-08-08python用函數(shù)創(chuàng)造字典的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于python用函數(shù)創(chuàng)造字典的實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-06-06