python爱心代码合集

  • 一行代码画爱心
  • 输出 I 爱 U
  • 填充型
  • 动态画红心
    • 桃心
    • 线性
  • 立体红心
  • 画一朵玫瑰花
  • 画树
  • 附录

一行代码画爱心

print('\n'.join([''.join([('Love'[(x-y)%len('Love')] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ') for x in range(-30,30)]) for y in range(15,-15,-1)]))

拆解一下,能看明白点。

a = [''.join([('Love'[(x-y)%len('Love')]if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)]print('\n'.join(a))

输出 I 爱 U

空格与星号可以换位置,运行会有不一样的效果。

import time
y = 2.5
while y>=-1.6:x = -3.0while x<=4.0:if (x*x+y*y-1)**3<=3.6*x*x*y*y*y or (x>-2.4 and x<-2.1 and y<1.5 and y>-1) or (((x<2.5 and x>2.2)or(x>3.4 and x<3.7)) and y>-1 and y<1.5) or (y>-1 and y<-0.6 and x<3.7 and x>2.2):print('*',end="")else:print(' ',end="")x += 0.1print()time.sleep(0.25)y -= 0.2

填充型

输出五个爱心,分别由Dear I love you forever! 五个单词填充而成。

import time
sentence = "Dear, I love you forever!"
for char in sentence.split():allChar = []for y in range(12, -12, -1):lst = []lst_con = ''for x in range(-30, 30):formula = ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3if formula <= 0:lst_con += char[(x) % len(char)]else:lst_con += ' 'lst.append(lst_con)allChar += lstprint('\n'.join(allChar))time.sleep(1)

动态画红心

桃心

import turtle
import time
def LittleHeart():for i in range(200):turtle.right(1)turtle.forward(2)# love = input('请输入表白语句,然后回车,默认为"I Love You":\n')
# me = input('请输入要表白的人:\n')
# if love=='':
# # 如果未输入表白语句,则使用默认语句
#     love='I Love you'love='I Love you'
me = '卧槽,这年轻人。'turtle.setup(width=900,height=600)  # 爱心的画布的大小
turtle.color('red','red')          # 爱心的颜色及外边笔的颜色
turtle.pensize(5)                   # 画笔的粗细
turtle.speed(1000000)               # 绘制速度turtle.up()                         # 画笔向上turtle.hideturtle()
turtle.goto(0,-180)
turtle.showturtle()
turtle.down()
turtle.speed(5)
turtle.begin_fill()# 开始填充
turtle.left(140)
turtle.forward(224)
LittleHeart()
turtle.left(120)
LittleHeart()
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
turtle.goto(0,0)
turtle.showturtle()
turtle.color('#CD5C5C','pink')
turtle.write(love,font=('gungsuh',30,),align="center")
turtle.up()
turtle.hideturtle()if me !='':turtle.color('black', 'pink')time.sleep(2)turtle.goto(180,-180)turtle.showturtle()turtle.write(me, font=(20,), align="center", move=True)window=turtle.Screen()window.exitonclick()

线性

import turtle
import math
turtle.pen()
t=turtle
t.up()
t.goto(0,150)
t.down()
t.color('red')
t.begin_fill()
t.fillcolor('red')
t.speed(1)
t.left(45)
t.forward(150)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(250+math.sqrt(2)*100)
t.right (90)
t.speed(2)
t.forward(250+100*math.sqrt(2))
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(150)
t.end_fill()
t.goto(-10,0)
t.pencolor('white')
# L
t.pensize(10)
t.goto(-50,0)
t.goto(-50,80)
t.up ()
# I
t.goto(-100,0)
t.down()
t.goto(-160,0)
t.goto(-130,0)
t.goto(-130,80)
t.goto(-160,80)
t.goto(-100,80)
t.up()
# O
t.goto(10,25)
t.down()
t.right(45)
t.circle(25,extent=180)
t.goto(60,55)
t.circle(25,extent=180)
t.goto(10,25)
t.up()
t.goto(75,80)
t.down()
t.goto(100,0)
t.goto(125,80)
t.up()
t.goto(180,80)
t.down()
t.goto(140,80)
t.goto(140,0)
t.goto(180,0)
t.up()
t.goto(180,40)
t.down()
t.goto(140,40)
# U
t.up()
t.goto(-40,-30)
t.down()
t.goto(-40,-80)
t.circle(40,extent=180)
t.goto(40,-30)
t.hideturtle()window=turtle.Screen()
window.exitonclick()

立体红心

import matplotlib
import matplotlib.pyplot as plt #导入绘图模块
from mpl_toolkits.mplot3d import Axes3D #3d绘图模块
import numpy as np #导入数值计算拓展模块
import timestart = time.time()x_lim, y_lim, z_lim = np.linspace(-10,10,520), np.linspace(-10,10,520), np.linspace(-10,10,520)
# 用来存放绘图点X,Y,Z坐标
X_points, Y_points, Z_points = [], [], []for x in x_lim:for y in y_lim:for z in z_lim:if (x**2+(9/4)*y**2+z**2-1)**3-(9/80)*y**2*z**3-x**2*z**3<=0:X_points.append(x)Y_points.append(y)Z_points.append(z)end = time.time()fig=plt.figure()                                # 画布初始化
ax=fig.add_subplot(111,projection='3d')         # 采用3d绘图
ax.scatter(X_points,Y_points,Z_points,s=20,alpha=0.5,color="red") # 3d散点图填充
plt.show()print(end-start)

运行了八百多秒,有点久。。。

画一朵玫瑰花

# 绘制玫瑰花并添加文字
import turtle# 设置画布大小
# turtle.screensize(canvwidth=None, canvheight=None, bg=None)
turtle.setup(width=0.6, height=0.6)
# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)# 输出文字
printer = turtle.Turtle()
printer.hideturtle()
printer.penup()
printer.back(200)
printer.write("赠给亲爱的 XX\n\n", align="right", font=("楷体", 16, "bold"))
printer.write("from XXX", align="center", font=("楷体", 12, "normal"))# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill()# 花瓣1
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)
turtle.circle(80, 98)
turtle.circle(-90, 40)# 花瓣2
turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)turtle.done()

画树

import turtle
import randomdef love(x, y):         # 在(x,y)处画爱心lalalalv = turtle.Turtle()lv.hideturtle()lv.up()lv.goto(x, y)       # 定位到(x,y)def curvemove():    # 画圆弧for i in range(20):lv.right(10)lv.forward(2)lv.color('red', 'pink')lv.speed(10000000)lv.pensize(1)# 开始画爱心lalalalv.down()lv.begin_fill()lv.left(140)lv.forward(22)curvemove()lv.left(120)curvemove()lv.forward(22)lv.write("YZ", font=("Arial", 12, "normal"), align="center")  # 写上表白的人的名字lv.left(140)  # 画完复位lv.end_fill()def tree(branchLen, t):if branchLen > 5:       # 剩余树枝太少要结束递归if branchLen < 20:  # 如果树枝剩余长度较短则变绿t.color("green")t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))t.down()t.forward(branchLen)love(t.xcor(), t.ycor())  # 传输现在turtle的坐标t.up()t.backward(branchLen)t.color("brown")returnt.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))t.down()t.forward(branchLen)# 以下递归ang = random.uniform(15, 45)t.right(ang)tree(branchLen - random.uniform(12, 16), t)  # 随机决定减小长度t.left(2 * ang)tree(branchLen - random.uniform(12, 16), t)  # 随机决定减小长度t.right(ang)t.up()t.backward(branchLen)myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(1000)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100, t)
myWin.exitonclick()

附录

参考资料:https://blog.csdn.net/qq_32532663/article/details/106242488

如果想要把python文件转成exe文件,发送给没有安装Python的对象,在她/他的电脑上运行,可以参考 Pyinstaller(Python打包为exe文件)

python爱心代码合集相关推荐

  1. GitHub上7000+ Star的Python常用代码合集

    作者 | 二胖并不胖 来源 | 大数据前沿(ID:bigdataqianyan) 今天二胖给大家介绍一个由一个国外小哥用好几年时间维护的Python代码合集.简单来说就是,这个程序员小哥在几年前开始保 ...

  2. 全网各编程语言的爱心代码合集

    背景 由于电视剧<点燃我温暖你>的男主角李峋在期中考试用c语言编译了一个粒子爱心动态图,掀起了网络上各个编程语言版本的爱心代码层出不穷. 李峋同款爱心C语言版 运行后效果: 编译器: vi ...

  3. Python表白代码合集:5种表白代码、比小心心、无限弹窗、520表白墙、抖音热门表白小软件、无套路表白

       一.介绍一下         写了5个Python表白代码,代码很简单,通俗易懂,都有注释.有什么问题联系本人QQ:483062431.文章最后教你们怎么打包成exe,如果你懒得搞懂代码怎么回事 ...

  4. Python表白代码合集,让你过年前找到对象

    嗨嗨,各位中秋节快乐 ~ 话说这都中秋节了,马上就要国庆了,再接着就是过年咯.各位都有对象了吗 ,再不找,家里都要开始催了吧.咱们敲代码的也不能天天敲代码啊,还是要找找对象,处处女朋友的 .不然生活都 ...

  5. python代码合集-python初学者代码合集

    [写在前边的话] 从零开始自学python有几个月了,一直坚持在github上上传一些代码片段.贴一下github地址:Teingi/test 供初学者借鉴.在刷题的过程中总结了思路和方法,放在了我的 ...

  6. Python表白代码合集,该不会只有你单身吧

    嗨嗨,各位单身人士,你们好呀~ 也是闲来无事,今天给你们分享一些有趣的东西~ 自己慢慢看吧 万一以后某天就能用上了 今年年前抓紧啊,别回去又要被亲戚念叨咯 1.给女神比个小心心 还有什么是比发个笔芯更 ...

  7. Python表白代码合集:5种表白代码,找不到对象你来找我,这也太秀了叭

    文章目录 一.容我啰嗦两句 二.来吧,代码展示 1.给女神比个小心心 2.无限弹窗式表白 3.这货不是表白代码,悄悄送给你们 4.520表白墙 5.抖音热门表白小软件 6.无套路表白 三.写在最后 一 ...

  8. ❤️动态爱心代码合集❤️

    ❤️AllenIverrui❤️

  9. python画心形代码大全_Python代码合集看点 机器人算法、自动导航算法算法大全...

    导读:本文包含机器人算法.自动导航算法的Python代码合集.其主要特点有以下三点:选择了在实践中广泛应用的算法:依赖最少.容易阅读,容易理解每个算法的基本思想.希望阅读本文后能对你有所帮助.文章来源 ...

最新文章

  1. 漫谈视频目标跟踪与分割
  2. matlab中sinks,MATLAB Simulink模块库详解(二)Sinks篇
  3. free() invalid next size异常
  4. akka的介绍_Akka笔记–演员介绍
  5. 与计算机密度密切相关的是,全国计算机二级MS考前冲刺试题及答案2016
  6. 数据统计作业0429_因子分析/FA
  7. sharepoint2010无法连接到配置数据库。
  8. k8s认证及ServiceAccount-十五
  9. hibernate使用sql进行查询
  10. turboc7运行C测试 vc6.0测试
  11. lzg_ad:XPE镜像文件部署详解
  12. 如何用VBA制作工资条
  13. unity 2d 动画
  14. 23种设计模式-观察者模式
  15. 什么是虚继承?虚基类?
  16. 改进的IAYOLO---GDIP:Gated Differentiable Image Processing for Object-Detection inAdverse Conditions
  17. 智慧水务解决方案-最新全套文件
  18. PHP论坛实现积分系统的思路
  19. Markdown(Typora)学习记录
  20. IP地址分类(ABCDE)

热门文章

  1. k-means算法详解及python代码
  2. 物理学家看人工智能:懂了你就不怕了
  3. http://www.jianshu.com/p/55458caf0814
  4. apm性能监控系统,程序员怎样优雅度过35岁中年危机?大厂内部资料
  5. html内容显示重叠了,css怎么让元素重叠?
  6. 工伤事故带班的工头是否要经济赔偿
  7. 查漏补缺!阿里内部Android笔记火爆IT圈,已拿offer入职
  8. 怎样用计算机做初中物理实验,初中物理实验软件
  9. 上海诺基亚贝尔-S-010W-AV2B卡刷固件包
  10. 如何免费将多个PDF合并成一个PDF文件?