七夕來襲!是時候展現專屬於程序員的浪漫了!你打算怎麼給心愛的人表達愛意?鮮花禮物?代碼表白?還是創意DIY?本文用一篇Python實例項目來向情人作為表白禮物。
講述和親愛的TA一起經歷的那些故事
以程序員的方式撒狗糧,專業浪漫,值得擁有!
1、顯示表白文字
2、顯示人物和愛心
主要是編寫如下的幾個函數,來實現七夕表白的功能。
# 清屏函數
def clear_all():
turtle.penup()
turtle.goto(0, 0)
turtle.color('white')
turtle.pensize(800)
turtle.pendown()
turtle.setheading(0)
turtle.fd(300)
turtle.bk(600)
# 重定位海龜的位置
def go_to(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
# 第一個畫面,顯示文字
def paintingOne():
turtle.penup()
turtle.goto(-300, 0)
turtle.color('pink')
turtle.write('時光讓我們相遇,我的情人,七夕快樂!!!', font=('楷體', 24, 'normal'))
time.sleep(3)
# 畫出人物
def draw_people(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.pensize(2)
turtle.color('pink')
turtle.setheading(0)
turtle.circle(60, 360)
turtle.penup()
turtle.setheading(90)
turtle.fd(75)
turtle.setheading(180)
turtle.fd(20)
turtle.pensize(4)
turtle.pendown()
turtle.circle(2, 360)
turtle.setheading(0)
turtle.penup()
turtle.fd(40)
turtle.pensize(4)
turtle.pendown()
turtle.circle(-2, 360)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(20)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(40)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(-60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(60)
turtle.setheading(-135)
turtle.fd(60)
turtle.bk(60)
turtle.setheading(-45)
turtle.fd(30)
turtle.setheading(-135)
turtle.fd(35)
turtle.penup()
# 畫愛心
def draw_heart(size):
turtle.color('red', 'pink')
turtle.pensize(2)
turtle.pendown()
turtle.setheading(150)
turtle.begin_fill()
turtle.fd(size)
turtle.circle(size * -3.745, 45)
turtle.circle(size * -1.431, 165)
turtle.left(120)
turtle.circle(size * -1.431, 165)
turtle.circle(size * -3.745, 45)
turtle.fd(size)
turtle.end_fill()
def Main():
turtle.setup(900, 500)
paintingOne()
clear_all()
paintingTwo()
clear_all()
turtle.done()
if __name__ == '__main__':
Main()
import turtle
import time
# 清屏函數
def clear_all():
turtle.penup()
turtle.goto(0, 0)
turtle.color('white')
turtle.pensize(800)
turtle.pendown()
turtle.setheading(0)
turtle.fd(300)
turtle.bk(600)
# 重定位海龜的位置
def go_to(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
# 畫愛心
def draw_heart(size):
turtle.color('red', 'pink')
turtle.pensize(2)
turtle.pendown()
turtle.setheading(150)
turtle.begin_fill()
turtle.fd(size)
turtle.circle(size * -3.745, 45)
turtle.circle(size * -1.431, 165)
turtle.left(120)
turtle.circle(size * -1.431, 165)
turtle.circle(size * -3.745, 45)
turtle.fd(size)
turtle.end_fill()
# 第一個畫面,顯示文字
def paintingOne():
turtle.penup()
turtle.goto(-300, 0)
turtle.color('pink')
turtle.write('時光讓我們相遇,我的情人,七夕快樂!!!', font=('楷體', 24, 'normal'))
time.sleep(3)
# 畫出人物
def draw_people(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.pensize(2)
turtle.color('pink')
turtle.setheading(0)
turtle.circle(60, 360)
turtle.penup()
turtle.setheading(90)
turtle.fd(75)
turtle.setheading(180)
turtle.fd(20)
turtle.pensize(4)
turtle.pendown()
turtle.circle(2, 360)
turtle.setheading(0)
turtle.penup()
turtle.fd(40)
turtle.pensize(4)
turtle.pendown()
turtle.circle(-2, 360)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(20)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(40)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(-60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(60)
turtle.setheading(-135)
turtle.fd(60)
turtle.bk(60)
turtle.setheading(-45)
turtle.fd(30)
turtle.setheading(-135)
turtle.fd(35)
turtle.penup()
# 第二個畫面,顯示發射愛心的小人
def paintingTwo():
turtle.speed(10)
draw_people(-250, 20)
turtle.penup()
turtle.goto(-150, -30)
draw_heart(14)
turtle.penup()
turtle.goto(-20, -60)
draw_heart(25)
turtle.penup()
turtle.goto(250, -100)
draw_heart(45)
turtle.hideturtle()
time.sleep(1)
def Main():
turtle.setup(900, 500)
paintingOne()
clear_all()
paintingTwo()
clear_all()
turtle.done()
if __name__ == '__main__':
Main()
遇見即是上上簽。
遇到了你,才覺得世界更明亮。
浮生若夢,慕爾如星。
初識只作乍見之歡,日後驚於久處不厭。
習慣等待的人,會以為愛情是慢慢加載的。直到遇見你,才知道愛是加速度。
在遇見你之前,我從未想過有人共我水北天南。
千山萬水就當是伏筆,總會遇到姗姗來遲的你。
想帶你飛屋環游,想和你私奔到月球,卻也感謝地心引力,讓我遇見你。
我也曾把光陰浪費到視死如歸,只因遇上你才明白流年難能可貴。
白茶清歡無別事,我在等風,也在等你。
皎皎白駒,在彼空谷。生刍一束,其人如玉。毋金玉爾音,而有遐心。