給父母祝壽用的程式

徐楷宸發表於2020-12-27

內容如下:

import math
import random
import tkinter
import tkinter.messagebox
import turtle as t
import math as m
import random as r
import time

window=tkinter.Tk()
window.geometry("400x300")
def drawX(a, i):
    angle = m.radians(i)
    return a * m.cos(angle)
def drawY(b, i):
    angle = m.radians(i)
    return b * m.sin(angle)

def fun():
	'''
    if time.strftime("%m%d")!='****':#****指代父母生日,如“1212”“0122”
        tkinter.messagebox.showinfo("提示","今天不是你的生日!!")
        exit()
    '''#用來判斷父母生日的
    tkinter.messagebox.showinfo("提示","開始了!!")
    window.destroy()
    t.speed(0)
    t.tracer(False)
    t.hideturtle()

    t.setup(1200, 750)
    #########################################
    t.bgcolor("#d3dae8")
    t.setup(1000, 800)
    t.penup()
    t.goto(150, 0)
    t.pendown()
    # 1
    t.pencolor("white")
    t.begin_fill()
    for i in range(360):
        x = drawX(150, i)
        y = drawY(60, i)
        t.goto(x, y)
    t.fillcolor("#fef5f7")
    t.end_fill()
    # 2
    t.begin_fill()
    for i in range(180):
        x = drawX(150, -i)
        y = drawY(70, -i)
        t.goto(x, y)
    for i in range(180, 360):
        x = drawX(150, i)
        y = drawY(60, i)
        t.goto(x, y)
    t.fillcolor("#f2d7dd")
    t.end_fill()
    # 3
    t.pu()
    t.goto(120, 0)
    t.pd()
    t.begin_fill()
    for i in range(360):
        x = drawX(120, i)
        y = drawY(48, i)
        t.goto(x, y)
    t.fillcolor("#cbd9f9")
    t.end_fill()
    # 4
    t.begin_fill()
    t.pencolor("#fee48c")
    for i in range(540):
        x = drawX(120, i)
        y = drawY(48, i) + 70
        t.goto(x, y)
    t.goto(-120, 0)
    t.fillcolor("#cbd9f9")
    t.end_fill()
    # 5
    t.pu()
    t.goto(120, 70)
    t.pd()
    t.pencolor("#fff0f3")
    t.begin_fill()
    for i in range(360):
        x = drawX(120, i)
        y = drawY(48, i) + 70
        t.goto(x, y)
    t.fillcolor("#fff0f3")
    t.end_fill()
    # 6
    t.pu()
    t.goto(110, 70)
    t.pd()
    t.pencolor("#fff9fb")
    t.begin_fill()
    for i in range(360):
        x = drawX(110, i)
        y = drawY(44, i) + 70
        t.goto(x, y)
    t.fillcolor("#fff9fb")
    t.end_fill()
    # 7
    t.pu()
    t.goto(120, 0)
    t.pd()
    t.begin_fill()
    t.pencolor("#ffa79d")
    for i in range(180):
        x = drawX(120, -i)
        y = drawY(48, -i) + 10
        t.goto(x, y)
    t.goto(-120, 0)
    for i in range(180, 360):
        x = drawX(120, i)
        y = drawY(48, i)
        t.goto(x, y)
    t.fillcolor("#ffa79d")
    t.end_fill()
    # 8
    t.pu()
    t.goto(120, 70)
    t.pd()
    t.begin_fill()
    t.pensize(4)
    t.pencolor("#fff0f3")
    for i in range(1800):
        x = drawX(120, 0.1 * i)
        y = drawY(-18, i) + 10
        t.goto(x, y)
    t.goto(-120, 70)
    t.pensize(1)
    for i in range(180, 360):
        x = drawX(120, i)
        y = drawY(48, i) + 70
        t.goto(x, y)
    t.fillcolor("#fff0f3")
    t.end_fill()
    # 9
    t.pu()
    t.goto(80, 70)
    t.pd()
    t.begin_fill()
    t.pencolor("#6f3732")
    t.goto(80, 120)
    for i in range(180):
        x = drawX(80, i)
        y = drawY(32, i) + 120
        t.goto(x, y)
    t.goto(-80, 70)
    for i in range(180, 360):
        x = drawX(80, i)
        y = drawY(32, i) + 70
        t.goto(x, y)
    t.fillcolor("#6f3732")
    t.end_fill()
    # 10
    t.pu()
    t.goto(80, 120)
    t.pd()
    t.pencolor("#ffaaa0")
    t.begin_fill()
    for i in range(360):
        x = drawX(80, i)
        y = drawY(32, i) + 120
        t.goto(x, y)
    t.fillcolor("#ffaaa0")
    t.end_fill()
    # 11
    t.pu()
    t.goto(70, 120)
    t.pd()
    t.pencolor("#ffc3be")
    t.begin_fill()
    for i in range(360):
        x = drawX(70, i)
        y = drawY(28, i) + 120
        t.goto(x, y)
    t.fillcolor("#ffc3be")
    t.end_fill()
    # 12
    t.pu()
    t.goto(80, 120)
    t.pd()
    t.begin_fill()
    t.pensize(3)
    t.pencolor("#ffaaa0")
    for i in range(1800):
        x = drawX(80, 0.1 * i)
        y = drawY(-12, i) + 80
        t.goto(x, y)
    t.goto(-80, 120)
    t.pensize(1)
    for i in range(180, 360):
        x = drawX(80, i)
        y = drawY(32, i) + 120
        t.goto(x, y)
    t.fillcolor("#ffaaa0")
    t.end_fill()
    # 13
    t.pu()
    t.goto(64, 120)
    t.pd()
    t.pencolor("#b1c9e9")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) + 60
        y = drawY(1, i) + 120
        t.goto(x, y)
    t.goto(64, 170)
    for i in range(540):
        x = drawX(4, i) + 60
        y = drawY(1, i) + 170
        t.goto(x, y)
    t.goto(56, 120)
    t.fillcolor("#b1c9e9")
    t.end_fill()
    t.pencolor("white")
    t.pensize(2)
    for i in range(1, 6):
        t.goto(64, 120 + 10 * i)
        t.pu()
        t.goto(56, 120 + 10 * i)
        t.pd()
    t.pu()
    t.goto(60, 170)
    t.pd()
    t.goto(60, 180)
    t.pensize(1)
    #
    t.pu()
    t.goto(64, 190)
    t.pd()
    t.pencolor("#f1add1")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) + 60
        y = drawY(10, i) + 190
        t.goto(x, y)
    t.fillcolor("#f1add1")
    t.end_fill()

    # 14
    t.pu()
    t.goto(-56, 120)
    t.pd()
    t.pencolor("#b1c9e9")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) - 60
        y = drawY(1, i) + 120
        t.goto(x, y)
    t.goto(-56, 170)
    for i in range(540):
        x = drawX(4, i) - 60
        y = drawY(1, i) + 170
        t.goto(x, y)
    t.goto(-64, 120)
    t.fillcolor("#b1c9e9")
    t.end_fill()
    t.pencolor("white")
    t.pensize(2)
    for i in range(1, 6):
        t.goto(-56, 120 + 10 * i)
        t.pu()
        t.goto(-64, 120 + 10 * i)
        t.pd()
    t.pu()
    t.goto(-60, 170)
    t.pd()
    t.goto(-60, 180)
    t.pensize(1)
    #
    t.pu()
    t.goto(-56, 190)
    t.pd()
    t.pencolor("#f1add1")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) - 60
        y = drawY(10, i) + 190
        t.goto(x, y)
    t.fillcolor("#f1add1")
    t.end_fill()
    # 15
    t.pu()
    t.goto(0, 130)
    t.pd()
    t.pencolor("#b1c9e9")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i)
        y = drawY(1, i) + 130
        t.goto(x, y)
    t.goto(4, 180)
    for i in range(540):
        x = drawX(4, i)
        y = drawY(1, i) + 180
        t.goto(x, y)
    t.goto(-4, 130)
    t.fillcolor("#b1c9e9")
    t.end_fill()
    t.pencolor("white")
    t.pensize(2)
    for i in range(1, 6):
        t.goto(4, 130 + 10 * i)
        t.pu()
        t.goto(-4, 130 + 10 * i)
        t.pd()
    t.pu()
    t.goto(0, 180)
    t.pd()
    t.goto(0, 190)
    t.pensize(1)
    #
    t.pu()
    t.goto(4, 200)
    t.pd()
    t.pencolor("#f1add1")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i)
        y = drawY(10, i) + 200
        t.goto(x, y)
    t.fillcolor("#f1add1")
    t.end_fill()
    # 16
    t.pu()
    t.goto(30, 110)
    t.pd()
    t.pencolor("#b1c9e9")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) + 30
        y = drawY(1, i) + 110
        t.goto(x, y)
    t.goto(34, 160)
    for i in range(540):
        x = drawX(4, i) + 30
        y = drawY(1, i) + 160
        t.goto(x, y)
    t.goto(26, 110)
    t.fillcolor("#b1c9e9")
    t.end_fill()
    t.pencolor("white")
    t.pensize(2)
    for i in range(1, 6):
        t.goto(34, 110 + 10 * i)
        t.pu()
        t.goto(26, 110 + 10 * i)
        t.pd()
    t.pu()
    t.goto(30, 160)
    t.pd()
    t.goto(30, 170)
    t.pensize(1)
    #
    t.pu()
    t.goto(34, 180)
    t.pd()
    t.pencolor("#f1add1")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) + 30
        y = drawY(10, i) + 180
        t.goto(x, y)
    t.fillcolor("#f1add1")
    t.end_fill()
    # 17
    t.pu()
    t.goto(-30, 110)
    t.pd()
    t.pencolor("#b1c9e9")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) - 30
        y = drawY(1, i) + 110
        t.goto(x, y)
    t.goto(-26, 160)
    for i in range(540):
        x = drawX(4, i) - 30
        y = drawY(1, i) + 160
        t.goto(x, y)
    t.goto(-34, 110)
    t.fillcolor("#b1c9e9")
    t.end_fill()
    t.pencolor("white")
    t.pensize(2)
    for i in range(1, 6):
        t.goto(-26, 110 + 10 * i)
        t.pu()
        t.goto(-34, 110 + 10 * i)
        t.pd()
    t.pu()
    t.goto(-30, 160)
    t.pd()
    t.goto(-30, 170)
    t.pensize(1)
    #
    t.pu()
    t.goto(-26, 180)
    t.pd()
    t.pencolor("#f1add1")
    t.begin_fill()
    for i in range(360):
        x = drawX(4, i) - 30
        y = drawY(10, i) + 180
        t.goto(x, y)
    t.fillcolor("#f1add1")
    t.end_fill()
    ###隨機
    color = ["#e28cb9", "#805a8c", "#eaa989", "#6e90b7", "#b8b68f", "#e174b5", "#cf737c", "#7c8782"]
    for i in range(80):
        t.pu()
        x = r.randint(-120, 120)
        y = r.randint(-25, 30)
        t.goto(x, y)
        t.pd()
        t.dot(r.randint(2, 5), color[r.randint(0, 7)])
    for i in range(40):
        t.pu()
        x = r.randint(-90, 90)
        y = r.randint(-35, 10)
        t.goto(x, y)
        t.pd()
        t.dot(r.randint(2, 5), color[r.randint(0, 7)])

    for i in range(40):
        t.pu()
        x = r.randint(-80, 80)
        y = r.randint(60, 90)
        t.goto(x, y)
        t.pd()
        t.dot(r.randint(2, 5), color[r.randint(0, 7)])
    for i in range(30):
        t.pu()
        x = r.randint(-50, 50)
        y = r.randint(45, 70)
        t.goto(x, y)
        t.pd()
        t.dot(r.randint(2, 5), color[r.randint(0, 7)])
    for i in range(50):
        t.pu()
        x = r.randint(-500, 500)
        y = r.randint(120, 300)
        t.goto(x, y)
        t.pd()
        t.dot(r.randint(3, 5), color[r.randint(0, 7)])
    t.seth(90)
    t.pu()
    t.goto(0, 0)
    t.fd(210)
    t.left(90)
    t.fd(170)
    t.pd()
    t.write("Happy Birthday", font=("Curlz MT", 50))
    time.sleep(5)
    #########################################################
    tkinter.messagebox.showerror("提示","Type Error!")
    print("修復進度:")
    for i in range(20):
        for j in range(5):
            print("▌",end=(""))
            time.sleep(0.1)
        print(" ",end=(""))
    tkinter.messagebox.showinfo("提示","問題已修復,請繼續觀看")
    
    ############################################
    t.bgcolor("white")
    t.clear()
    t.setheading(0)
    t.pendown()
    list1=[]
    for i in range(5):
        list1.append(int(random.uniform(-500,500)))
    list2=[]
    for i in range(5):
        list2.append(int(random.uniform(-200,-50)))
    list3=[]
    for i in range(8):
        list3.append(int(random.uniform(-400,400)))
    list4=[]
    for i in range(8):
        list4.append(int(random.uniform(-150,-50)))
    list5=[]
    for i in range(7):
        list5.append(int(random.uniform(-300,300)))
    list6=[]
    for i in range(7):
        list6.append(int(random.uniform(-200,-100)))
    list7=[]
    for i in range(18):
        list7.append(int(random.uniform(-500,500)))
    list8=[]
    for i in range(18):
        list8.append(int(random.uniform(-100,100)))
    #繪製人名    
    t.speed(0)
    t.penup()
    t.goto(-290,200)
    t.pendown()
    t.color("black")
    t.forward(30)
    t.backward(15)
    t.right(90)
    t.forward(45)
    t.penup()
    t.goto(-250,170)
    t.pendown()
    t.circle(15,180)
    t.forward(15)
    t.circle(15,180)
    t.forward(15)
    t.penup()
    t.goto(-200,190)
    t.pendown()
    t.fillcolor("black")
    t.begin_fill()
    t.pencolor("black")
    t.circle(3, 360)
    t.penup()
    t.goto(-200,170)
    t.pendown()
    t.circle(3,360)
    t.end_fill()
    t.penup()
    t.goto(-190,200)
    t.pendown()
    t.left(45)
    t.forward(25)
    t.penup()
    t.goto(-155,200)
    t.pendown()
    t.right(90)
    t.forward(25)
    t.left(45)
    t.forward(30)
    t.penup()
    t.goto(-150,170)
    t.pendown()
    t.circle(15,180)
    t.forward(15)
    t.circle(15,180)
    t.forward(15)
    t.penup()
    t.goto(-135,170)
    t.pendown()
    t.left(55)
    t.forward(25)
    t.penup()
    t.goto(0,0)
    t.left(35)#將畫筆設定到初始化位置
     
    #畫大號愛心(位置隨機)
    for x,y in list(zip(list1,list2)):
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.fillcolor("#FF6A6A")
        t.begin_fill()
        t.pencolor("#FF6A6A")
        t.forward(40)
        t.circle(20, 180)
        t.right(90)
        t.circle(20, 180)
        t.forward(40)
        t.end_fill()
        t.penup()
        t.goto(x, y)
    #畫中號愛心(位置隨機)
    for x,y in list(zip(list5,list6)):
        t.pendown()
        t.fillcolor("#FFA07A")
        t.begin_fill()
        t.pencolor("#FFA07A")
        t.forward(30)
        t.circle(15, 180)
        t.right(90)
        t.circle(15, 180)
        t.forward(30)
        t.end_fill()
        t.penup()
        t.goto(x, y)
    #畫小號愛心(位置隨機)
    for x,y in list(zip(list3,list4)):
        t.pendown()
        t.fillcolor("#FFD39B")
        t.begin_fill()
        t.pencolor("#FFD39B")
        t.forward(20)
        t.circle(10,180)
        t.right(90)
        t.circle(10,180)
        t.forward(20)
        t.end_fill()
        t.penup()
        t.goto(x, y)
    #畫點點(位置隨機)
    for x,y in list(zip(list7,list8)):
        t.pendown()
        t.fillcolor("#FF6A6A")
        t.begin_fill()
        t.pencolor("#FF6A6A")
        t.circle(3, 360)
        t.end_fill()
        t.penup()
        t.goto(x, y)
     
     
    def DegreeCurve(n, r, d=1):
        for i in range(n):
            t.left(d)
            t.circle(r, abs(d))
    # 初始位置設定
    s = 0.2
    # t.setup(450*5*s, 750*5*s)
    t.pencolor("black")
    t.fillcolor("#FF4040")
    t.speed(100)
    t.penup()
    t.goto(0, 900*s)
    t.pendown()
    # 繪製花朵形狀
    t.begin_fill()
    t.circle(200*s,30)
    DegreeCurve(60, 50*s)
    t.circle(200*s,30)
    DegreeCurve(4, 100*s)
    t.circle(200*s,50)
    DegreeCurve(50, 50*s)
    t.circle(350*s,65)
    DegreeCurve(40, 70*s)
    t.circle(150*s,50)
    DegreeCurve(20, 50*s, -1)
    t.circle(400*s,60)
    DegreeCurve(18, 50*s)
    t.fd(250*s)
    t.right(150)
    t.circle(-500*s,12)
    t.left(140)
    t.circle(550*s,110)
    t.left(27)
    t.circle(650*s,100)
    t.left(130)
    t.circle(-300*s,20)
    t.right(123)
    t.circle(220*s,57)
    t.end_fill()
    # 繪製花枝形狀
    t.left(120)
    t.fd(280*s)
    t.left(115)
    t.circle(300*s,33)
    t.left(180)
    t.circle(-300*s,33)
    DegreeCurve(70, 225*s, -1)
    t.circle(350*s,104)
    t.left(90)
    t.circle(200*s,105)
    t.circle(-500*s,63)
    t.penup()
    t.goto(170*s,-30*s)
    t.pendown()
    t.left(160)
    DegreeCurve(20, 2500*s)
    DegreeCurve(220, 250*s, -1)
    # 繪製一個綠色葉子
    t.fillcolor('#00CD00')
    t.penup()
    t.goto(670*s,-180*s)
    t.pendown()
    t.right(140)
    t.begin_fill()
    t.circle(300*s,120)
    t.left(60)
    t.circle(300*s,120)
    t.end_fill()
    t.penup()
    t.goto(180*s,-550*s)
    t.pendown()
    t.right(85)
    t.circle(600*s,40)
    # 繪製另一個綠色葉子
    t.penup()
    t.goto(-150*s,-1000*s)
    t.pendown()
    t.begin_fill()
    t.rt(120)
    t.circle(300*s,115)
    t.left(75)
    t.circle(300*s,100)
    t.end_fill()
    t.penup()
    t.goto(430*s,-1070*s)
    t.pendown()
    t.right(30)
    t.circle(-600*s,35)

    t.penup()
    t.goto(150,150)
    t.pendown()
    t.write("**** 生日快樂",font=("微軟雅黑",30))#****代指父母姓名
    ############################################
    time.sleep(10)
    tkinter.messagebox.showinfo("提示","結束了!")
    time.sleep(3)
    tkinter.messagebox.showinfo("提示","真的結束了!")
    time.sleep(3)
    tkinter.messagebox.showinfo("提示","即將自動退出!!!")
    time.sleep(1)
    t.bye()

    
    print("結束了")
    print("祝生日快樂")
    exit()

bt=tkinter.Button(window,text="開啟你的 生 日 祝 福 ",font=("微軟雅黑",20),width=20,command=fun)
label=tkinter.Label(window,text="準備好了嗎??",font=("微軟雅黑",20))
label.pack(pady=30)
bt.pack(pady=20)

就是有一些是用了別人的程式碼,不喜勿噴
只是對父母的心意,沒有別的意思

相關文章