三个Python爱心表白代码,让她高兴一整天!
话不多说,咱直接上代码!
1、紫色爱心
先看效果
代码
#1-1导入turtle模块进行设计
import turtle
import time
#1-2画心形圆弧
def hart_arc():
for i in range(200):
turtle.right(1)
turtle.forward(2)
def move_pen_position(x, y):
turtle.hideturtle() # 隐藏画笔(先)
turtle.up() # 提笔
turtle.goto(x, y) # 移动画笔到指定起始坐标(窗口中心为0,0)
turtle.down() # 下笔
turtle.showturtle() # 显示画笔
love = "听闻小姐治家有方,鄙人余生愿闻其详?" #input("请输入表白话语:")
signature = "先生" #input("请签署你的名字:")
date= "" #input("请写上日期:")
if love == '':
love = 'I Love You'
#1-3初始化
turtle.setup(width=800, height=500) # 窗口(画布)大小
turtle.color('black', 'Pink') # 画笔颜色
turtle.pensize(5) # 画笔粗细
turtle.speed(100) # 描绘速度
# 初始化画笔起始坐标
move_pen_position(x=0, y=-180) # 移动画笔位
turtle.left(140) # 向左旋转140度
turtle.begin_fill() # 标记背景填充位置
#1-4画图和展示
turtle.forward(224) # 向前移动画笔,长度为224
# 画爱心圆弧
hart_arc() # 左侧圆弧
turtle.left(120) # 调整画笔角度
hart_arc() # 右侧圆弧
# 画心形直线( 右下方 )
turtle.forward(224)
turtle.end_fill() # 标记背景填充结束位置
move_pen_position(x=70, y=160) # 移动画笔位置
turtle.left(185) # 向左旋转180度
turtle.circle(-110,185) # 右侧圆弧
# 画心形直线( 右下方 )
#turtle.left(20) # 向左旋转180度
turtle.forward(50)
move_pen_position(x=-180, y=-180) # 移动画笔位置
turtle.left(180) # 向左旋转140度
# 画心形直线( 左下方 )
turtle.forward(600) # 向前移动画笔,长度为224
# 在心形中写上表白话语
move_pen_position(0,50) # 表白语位置
turtle.hideturtle() # 隐藏画笔
turtle.color('#CD5C5C', 'pink') # 字体颜色
# font:设定字体、尺寸(电脑下存在的字体都可设置) align:中心对齐
turtle.write(love, font=('Arial', 20, 'bold'), align="center")
# 签写署名和日期
if (signature != '') & (date != ''):
turtle.color('red', 'pink')
time.sleep(2)
move_pen_position(220, -180)
turtle.hideturtle() # 隐藏画笔
turtle.write(signature, font=('Arial', 20), align="center")
move_pen_position(220, -220)
turtle.hideturtle() # 隐藏画笔
turtle.write(date, font=('Arial', 20), align="center")
#1-5点击窗口关闭程序
window = turtle.Screen()
window.exitonclick()
2、love2 ilove you
效果展示
代码展示
import turtle as t
import math as mt
if __name__ == "__main__":
t.screensize(800,600,'white')
t.pensize(10)
t.speed(10)
#爱心1
t.color('black','pink')
t.begin_fill()
for i in range(-90,90,5):
x=mt.cos(mt.radians(i))
y=float(pow(mt.cos(mt.radians(i)),2/3))+float(mt.sin(mt.radians(i)))
t.penup()
# print(int(x*50)+10,int(y*50)+10)
t.goto(int(x*50)+50,int(y*50)+30)
t.pendown()
t.forward(1)
t.penup()
t.goto(-int(x*50)+50,int(y*50)+30)
t.pendown()
t.forward(1)
t.penup()
t.end_fill()
#爱心2
t.goto(0,10)
t.penup()
t.begin_fill()
for i in range(0,360,5):
r=60*(1-mt.sin(mt.radians(i)))
t.penup()
t.left(5)
t.forward(r)
t.pendown()
t.forward(1)
t.penup()
t.backward(r+1)
t.pendown()
t.end_fill()
#L
t.penup()
t.goto(-200,0)
t.left(90)
t.begin_fill()
t.pendown()
t.forward(100)
t.right(90)
t.forward(20)
t.right(90)
t.forward(80)
t.left(90)
t.forward(40)
t.right(90)
t.forward(20)
t.right(90)
t.forward(60)
t.end_fill()
#o
t.penup()
t.goto(-80,0)
t.pendown()
t.begin_fill()
t.circle(-50)
t.end_fill()
t.penup()
t.color('pink','black')
t.begin_fill()
t.goto(-80,20)
t.pendown()
t.circle(-30)
t.end_fill()
t.color('black','pink')
#E
t.penup()
t.goto(120, 0)
t.right(180)
t.left(90)
t.begin_fill()
t.pendown()
t.forward(100)#上
t.right(90)
t.forward(60)#横
t.right(90)
t.forward(20)#竖
t.right(90)
t.forward(40)#横
t.left(90)
t.forward(20)#竖
t.left(90)
t.forward(40)#横
t.right(90)
t.forward(20)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(20)
t.right(90)
t.forward(60)
t.end_fill()
t.mainloop()
3、红色爱心
效果展示
代码展示
import turtle as t
def face(x, y): #脸
t.setheading(-90)
t.penup()
t.goto(x, y)
t.pendown()
t.backward(15) # 左脸
t.circle(70,-80)
t.setheading(80) # 左耳
t.circle(-150, 15)
t.circle(-15, 180)
t.setheading(-115)
t.circle(-150, 13)
t.setheading(10)
t.circle(-100,10)
t.setheading(70) # 右耳
t.circle(-150, 20)
t.circle(-15, 180)
t.circle(-150, 16)
t.setheading(10)
t.setheading(160) # 右脸
t.circle(60, -130)
t.setheading(-75)
t.forward(40)
def word(x, y): # “如何骗人”
t.pensize(2)
t.pencolor("black")
t.setheading(0)
t.penup()
t.goto(x, y)
t.pendown()
t.forward(10) # “如”
t.penup()
t.setheading(90)
t.forward(8)
t.pendown()
t.setheading(-120)
t.forward(15)
t.setheading(-45)
t.forward(12)
t.penup()
t.setheading(80)
t.forward(15)
t.pendown()
t.setheading(-125)
t.forward(16)
t.penup()
t.setheading(42)
t.forward(16)
t.pendown()
t.setheading(-90)
t.forward(10)
t.penup()
t.backward(11)
t.pendown()
t.setheading(0)
t.forward(8)
t.setheading(-90)
t.forward(10)
t.penup()
t.setheading(180)
t.forward(8)
t.pendown()
t.setheading(0)
t.forward(8)
t.penup() # “何”
t.goto(x+7,y-18)
t.pendown()
t.setheading(-135)
t.forward(13)
t.penup()
t.goto(x+5, y - 20)
t.pendown()
t.setheading(-90)
t.forward(16)
t.penup()
t.goto(x+11, y-18)
t.pendown()
t.setheading(0)
t.forward(13)
t.penup()
t.goto(x+12, y-22)
t.pendown()
t.setheading(-90)
t.forward(8)
t.penup()
t.goto(x + 12, y - 22)
t.pendown()
t.setheading(0)
t.forward(6)
t.setheading(-90)
t.forward(8)
t.penup()
t.goto(x + 11, y - 31)
t.pendown()
t.setheading(0)
t.forward(6)
t.penup()
t.goto(x + 21, y - 19)
t.pendown()
t.setheading(-90)
t.forward(18)
t.setheading(145)
t.forward(5)
t.penup() # “骗”
t.goto(x + 40, y+3)
t.pendown()
t.setheading(0)
t.forward(10)
t.setheading(-90)
t.forward(7)
t.penup()
t.goto(x + 45, y + 3)
t.pendown()
t.setheading(-90)
t.forward(10)
t.setheading(0)
t.forward(7)
t.setheading(-100)
t.forward(10)
t.setheading(145)
t.forward(4)
t.penup()
t.goto(x+38, y-12)
t.pendown()
t.setheading(0)
t.forward(11)
t.penup()
t.goto(x+57, y+9)
t.pendown()
t.setheading(-45)
t.forward(4)
t.penup()
t.goto(x+54, y+3)
t.pendown()
t.setheading(0)
t.forward(13)
t.setheading(-90)
t.forward(5)
t.setheading(180)
t.forward(12)
t.penup()
t.goto(x + 54, y + 3)
t.pendown()
t.setheading(90)
t.circle(90,-10)
t.penup()
t.goto(x + 56, y-5)
t.pendown()
t.setheading(-90)
t.forward(11)
t.penup()
t.goto(x + 56, y - 5)
t.pendown()
t.setheading(0)
t.forward(13)
t.setheading(-90)
t.forward(12)
t.setheading(145)
t.forward(4)
t.penup()
t.goto(x + 56, y - 10)
t.pendown()
t.setheading(0)
t.forward(13)
t.penup()
t.goto(x + 56, y - 10)
t.pendown()
t.setheading(0)
t.forward(13)
t.penup()
t.goto(x + 60, y - 4)
t.pendown()
t.setheading(-90)
t.forward(10)
t.penup()
t.goto(x + 64, y - 4)
t.pendown()
t.setheading(-90)
t.forward(10)
t.penup() # “人”
t.goto(x + 60, y - 19)
t.pendown()
t.setheading(70)
t.circle(50, -30)
t.penup()
t.goto(x + 56, y - 27)
t.pendown()
t.setheading(130)
t.circle(-50, -20)
def book(x,y): #书
t.setheading(-90)
t.penup()
t.goto(x, y)
t.fillcolor("red")
t.begin_fill()
t.pendown()
t.forward(60)
t.setheading(0)
t.circle(-100, 25)
t.setheading(90)
t.forward(59)
t.setheading(-25)
t.circle(-100, -25)
t.penup()
t.setheading(-14)
t.forward(46)
t.pendown()
t.setheading(15)
t.circle(-100, 25)
t.setheading(-90)
t.forward(58)
t.setheading(-11)
t.circle(-105, -25)
t.end_fill()
def eyes(x, y): # 五官
t.setheading(-20)
t.penup()
t.goto(x, y)
t.pendown()
t.forward(10)
t.setheading(0)
t.penup()
t.forward(10)
t.pendown()
t.setheading(20)
t.forward(10)
t.setheading(-160)
t.penup()
t.forward(50)
t.pendown()
t.setheading(0)
t.forward(15)
t.penup()
t.forward(25)
t.pendown()
t.forward(18)
t.setheading(-145)
t.penup()
t.forward(45)
t.pendown()
t.setheading(0)
t.forward(10)
def cheek(x, y): #腮红
t.setheading(0)
for i in range(1, 4):
t.color("pink")
t.pensize(4)
t.penup()
t.right(110)
t.goto(x, y)
t.pendown()
t.forward(9)
t.left(110)
x += 9
t.pencolor("black")
def hands(x,y): # 小手手
t.penup()
t.goto(x, y)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.setheading(5)
t.forward(120)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.setheading(-165)
t.forward(35)
t.circle(10,180)
t.forward(15)
t.end_fill()
def heart(x,y,z): # 爱心
t.setheading(110)
t.pensize(2)
t.pencolor("black")
t.penup()
t.goto(x,y)
t.pendown()
t.fillcolor("red")
t.begin_fill() #左半边
t.circle(50,180)
t.circle(180,37)
t.left(46) #右半边
t.circle(180,37)
t.circle(50, 182)
t.end_fill()
def main():
"""
主函数
"""
t.pensize(0)
t.speed(6)
face(-95, 55)
eyes(-45, 110)
cheek(-80, 80)
cheek(0, 80)
book(-110, 55)
hands(-110,5)
word(-100,35)
heart(150,0,1)
t.hideturtle()
t.exitonclick()
if __name__ == "__main__":
main()
学习资源推荐
除了上述分享,学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!
👉Python所有方向的学习路线👈
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取)
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python70个实战练手案例&源码👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉Python大厂面试资料👈
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
👉Python副业兼职路线&方法👈
学好 Python 不论是就业还是做副业赚钱都不错,但要学会兼职接单还是要有一个学习规划。
👉 这份完整版的Python全套学习资料已经上传,朋友们如果需要可以V扫描下方二维码联系领取
【保证100%免费
】