时间已来到二月,情人节就要到了。在今年的特殊环境下,想必很多人都要和自己的心上人异地相隔过节了(比如久久不被允许返校的学生党),那么如何给自己的男/女朋友送上一份惊喜呢?如果你的对象恰巧会那么一点python,不妨试试利用python的第三方库turtle,略表自己心意吧。
先放一张成品图:
前辈曾曰过,“人生苦短,我用python”,python强大的第三方库可以让各种编程想法得到迅速实现。用python来画图实好比叫猛张飞来做针线活儿,要的就是这种反差萌。
首先,打开编译器,导入需要用到的库:
import turtle as timport time
写上文字,画大白:
#初始化画布t.setup(width=800, height=600)time.sleep(2)#上部文字t.hideturtle()t.color("#FF6A6A")t.penup()t.goto(-300,220)words1 = '当'words2 = ['送','给','小','可','爱']for i in range(4):t.write(words1, font=("华文彩云", 3*i+14, "bold"))t.forward(20+3*i)time.sleep(0.15)t.forward(8)t.write("!", font=("华文彩云", 23, "bold"))t.forward(20)time.sleep(0.5)for word in words2:t.write(word, font=("华文彩云", 26, "bold"))t.forward(35)time.sleep(0.4)t.write(":", font=("华文彩云", 23, "bold"))t.forward(35)#大白#头t.goto(-100,150)t.right(90)t.color("black")t.pensize(1)t.pendown() size = 0.6a = 0.8*sizefor i in range(120): if 0 <= i < 30 or 60 <= i < 90: a = a + 0.2*sizet.left(3) # 向左转3度 t.forward(a) # 向前走a的步长 else: a = a - 0.2*sizet.left(3) t.forward(a) t.penup() #眼睛time.sleep(0.5)t.goto(-70,150)t.dot(14)time.sleep(0.5)t.goto(-24,150)t.dot(14)time.sleep(0.5)t.right(60)t.pendown()t.speed(2)t.circle(-50,50)#身体t.penup()t.goto(-89.85,131.47)t.left(65)t.pendown()t.circle(250,60)t.penup()t.goto(-2.64,133.09)t.left(30)t.pendown()t.circle(-250,60)t.right(20)t.circle(-134,110)#腿t.penup()t.left(135)t.goto(-120,-145)t.pendown()t.circle(120,45)t.left(15)t.circle(42,80)t.left(50)t.forward(70)t.penup()t.back(70)t.pendown()t.right(130)t.circle(42,80)t.left(15)t.circle(115,48)t.penup()#胳膊t.goto(-125,89)t.pendown()t.left(112)t.circle(250,50)t.circle(50,45)t.circle(20,80)t.circle(50,45)t.circle(180,16)t.penup()t.goto(31,90)t.pendown()t.right(126)t.circle(-250,50)t.circle(-50,45)t.circle(-20,80)t.circle(-50,45)t.circle(-180,14)#手t.penup()t.goto(-208,-90)t.pendown()t.left(150)t.right(180)t.circle(-30,20)t.penup()t.goto(-208,-90)t.right(160)t.pendown()t.circle(30,40)t.circle(5,120)t.circle(40,40)
画上玫瑰花,玫瑰花的代码源自网上,自己调整了下位置和大小参数(在注释行标出)
#画玫瑰t.penup()t.goto(-270,60)t.pendown()#玫瑰花角度t.right(60)#玫瑰花大小size=0.35# 花蕊t.fillcolor("#EE0000")t.begin_fill()t.circle(10*size, 180)t.circle(25*size, 110)t.left(50)t.circle(60*size, 45)t.circle(20*size, 170)t.right(24)t.forward(30*size)t.left(10)t.circle(30*size, 110)t.forward(20*size)t.left(40)t.circle(90*size, 70)t.circle(30*size, 150)t.right(30)t.forward(15*size)t.circle(80*size, 90)t.left(15)t.forward(45*size)t.right(165)t.forward(20*size)t.left(155)t.circle(150*size, 80)t.left(50)t.circle(150*size, 90)t.end_fill()# 花瓣1t.left(150)t.circle(-90*size, 70)t.left(20)t.circle(75*size, 105)t.setheading(80)t.circle(80*size, 98)t.circle(-90*size, 40)# 花瓣2t.left(180)t.circle(90*size, 40)t.circle(-80*size, 98)t.setheading(-63.5)# 叶子1t.forward(30*size)t.left(90)t.forward(25*size)t.left(45)t.fillcolor("#00CD00")t.begin_fill()t.circle(-80*size, 90)t.right(90)t.circle(-80*size, 90)t.end_fill()t.right(135)t.forward(60*size)t.left(180)t.penup()t.forward(85*size)t.pendown()t.left(90)t.forward(80*size)# 叶子2t.right(90)t.right(45)t.fillcolor("#00CD00")t.begin_fill()t.circle(80*size, 90)t.left(90)t.circle(80*size, 90)t.end_fill()t.left(135)t.forward(60*size)t.left(180)t.forward(60*size)t.right(90)t.circle(-1000*size,8.6)t.penup()t.goto(-200,-110)t.pendown()t.circle(-1000*size,10)
最后,写上情人节快乐:
#情人节快乐words3 = ['情','人','节','快','乐','!']t.penup()t.color("#FF6A6A")t.goto(30,220)t.setheading(0)for word in words3:t.write(word, font=("华文彩云", 26, "bold"))t.forward(35)time.sleep(0.4)t.done()
至此贺卡部分就完工了。为了留有互动空间,我利用turtle库里的textinput函数可以弹出会话框的功能,为贺卡设置了密码,效果是这样的:
打开贺卡之前先让他/她猜猜密码,文字可以自己DIY:
#密码部分str=t.textinput("情人节快乐!","小可爱你好呀,这是白白送你的节日贺卡,你要输入正确的密码才能打开哦")if (str!='-10-06'):str=t.textinput("Sorry","密码不对哦,提示一下是八位数字,再输一次吧")if (str=='1006'):str=t.textinput("HaHaHaHa","宝贝儿真棒,其实你已经猜对啦,密码格式是‘XXXX-XX-XX’,再输一次吧")while(str!='-10-06'):str=t.textinput("HaHaHaHa","是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")elif (str!='-10-06'):str=t.textinput("Sorry","是一个对我们很有意义的日子哦")if (str=='1006'):str=t.textinput("HaHaHaHa","宝贝儿真棒,其实你已经猜对啦,密码格式是‘XXXX-XX-XX’,再输一次吧")while(str!='-10-06'):str=t.textinput("HaHaHaHa","密码是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")elif (str!='-10-06'):str=t.textinput("Sorry","提示最后一次啦,是我们的纪念日呀")if (str=='1006'):str=t.textinput("HaHaHaHa","宝贝儿真棒,其实你已经猜对啦,密码格式是‘XXXX-XX-XX’,再输一次吧")while(str!='-10-06'):str=t.textinput("HaHaHaHa","密码是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")else:while(str!='-10-06'):str=t.textinput("Sorry","密码是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")
也就是if-else语句啦。
愿天下有情人终成眷属,愿这场灾难早日结束,附上完整代码,密码是-10-06
# -*- coding: utf-8 -*-"""Created on Wed Jan 29 16:51:51 @author: user"""import turtle as timport time #密码部分str=t.textinput("情人节快乐!","小可爱你好呀,这是白白送你的节日贺卡,你要输入正确的密码才能打开哦")if (str!='-10-06'):str=t.textinput("Sorry","密码不对哦,提示一下是八位数字,再输一次吧")if (str=='1006'):str=t.textinput("HaHaHaHa","宝贝儿真棒,其实你已经猜对啦,密码格式是‘XXXX-XX-XX’,再输一次吧")while(str!='-10-06'):str=t.textinput("HaHaHaHa","是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")elif (str!='-10-06'):str=t.textinput("Sorry","是一个对我们很有意义的日子哦")if (str=='1006'):str=t.textinput("HaHaHaHa","宝贝儿真棒,其实你已经猜对啦,密码格式是‘XXXX-XX-XX’,再输一次吧")while(str!='-10-06'):str=t.textinput("HaHaHaHa","密码是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")elif (str!='-10-06'):str=t.textinput("Sorry","提示最后一次啦,是我们的纪念日呀")if (str=='1006'):str=t.textinput("HaHaHaHa","宝贝儿真棒,其实你已经猜对啦,密码格式是‘XXXX-XX-XX’,再输一次吧")while(str!='-10-06'):str=t.textinput("HaHaHaHa","密码是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")else:while(str!='-10-06'):str=t.textinput("Sorry","密码是我们在一起的日子呀,格式是‘XXXX-XX-XX’,再输一次吧")#初始化画布t.setup(width=800, height=600)time.sleep(2)#上部文字t.hideturtle()t.color("#FF6A6A")t.penup()t.goto(-300,220)words1 = '当'words2 = ['送','给','小','可','爱']words3 = ['情','人','节','快','乐','!']for i in range(4):t.write(words1, font=("华文彩云", 3*i+14, "bold"))t.forward(20+3*i)time.sleep(0.15)t.forward(8)t.write("!", font=("华文彩云", 23, "bold"))t.forward(20)time.sleep(0.5)for word in words2:t.write(word, font=("华文彩云", 26, "bold"))t.forward(35)time.sleep(0.4)t.write(":", font=("华文彩云", 23, "bold"))t.forward(35)#大白#头t.goto(-100,150)t.right(90)t.color("black")t.pensize(1)t.pendown() size = 0.6a = 0.8*sizefor i in range(120): if 0 <= i < 30 or 60 <= i < 90: a = a + 0.2*sizet.left(3) # 向左转3度 t.forward(a) # 向前走a的步长 else: a = a - 0.2*sizet.left(3) t.forward(a) t.penup() #眼睛time.sleep(0.5)t.goto(-70,150)t.dot(14)time.sleep(0.5)t.goto(-24,150)t.dot(14)time.sleep(0.5)t.right(60)t.pendown()t.speed(2)t.circle(-50,50)#身体t.penup()t.goto(-89.85,131.47)t.left(65)t.pendown()t.circle(250,60)t.penup()t.goto(-2.64,133.09)t.left(30)t.pendown()t.circle(-250,60)t.right(20)t.circle(-134,110)#腿t.penup()t.left(135)t.goto(-120,-145)t.pendown()t.circle(120,45)t.left(15)t.circle(42,80)t.left(50)t.forward(70)t.penup()t.back(70)t.pendown()t.right(130)t.circle(42,80)t.left(15)t.circle(115,48)t.penup()#胳膊t.goto(-125,89)t.pendown()t.left(112)t.circle(250,50)t.circle(50,45)t.circle(20,80)t.circle(50,45)t.circle(180,16)t.penup()t.goto(31,90)t.pendown()t.right(126)t.circle(-250,50)t.circle(-50,45)t.circle(-20,80)t.circle(-50,45)t.circle(-180,14)#手t.penup()t.goto(-208,-90)t.pendown()t.left(150)t.right(180)t.circle(-30,20)t.penup()t.goto(-208,-90)t.right(160)t.pendown()t.circle(30,40)t.circle(5,120)t.circle(40,40)#画玫瑰t.penup()t.goto(-270,60)t.pendown()#玫瑰花角度t.right(60)#玫瑰花大小size=0.35# 花蕊t.fillcolor("#EE0000")t.begin_fill()t.circle(10*size, 180)t.circle(25*size, 110)t.left(50)t.circle(60*size, 45)t.circle(20*size, 170)t.right(24)t.forward(30*size)t.left(10)t.circle(30*size, 110)t.forward(20*size)t.left(40)t.circle(90*size, 70)t.circle(30*size, 150)t.right(30)t.forward(15*size)t.circle(80*size, 90)t.left(15)t.forward(45*size)t.right(165)t.forward(20*size)t.left(155)t.circle(150*size, 80)t.left(50)t.circle(150*size, 90)t.end_fill()# 花瓣1t.left(150)t.circle(-90*size, 70)t.left(20)t.circle(75*size, 105)t.setheading(80)t.circle(80*size, 98)t.circle(-90*size, 40)# 花瓣2t.left(180)t.circle(90*size, 40)t.circle(-80*size, 98)t.setheading(-63.5)# 叶子1t.forward(30*size)t.left(90)t.forward(25*size)t.left(45)t.fillcolor("#00CD00")t.begin_fill()t.circle(-80*size, 90)t.right(90)t.circle(-80*size, 90)t.end_fill()t.right(135)t.forward(60*size)t.left(180)t.penup()t.forward(85*size)t.pendown()t.left(90)t.forward(80*size)# 叶子2t.right(90)t.right(45)t.fillcolor("#00CD00")t.begin_fill()t.circle(80*size, 90)t.left(90)t.circle(80*size, 90)t.end_fill()t.left(135)t.forward(60*size)t.left(180)t.forward(60*size)t.right(90)t.circle(-1000*size,8.6)t.penup()t.goto(-200,-110)t.pendown()t.circle(-1000*size,10)#情人节快乐t.penup()t.color("#FF6A6A")t.goto(30,220)t.setheading(0)for word in words3:t.write(word, font=("华文彩云", 26, "bold"))t.forward(35)time.sleep(0.4)t.done()