目录

  • 一行代码画爱心
    • 拆解
  • 输出 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 = 'XXX'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()

我是CSDN钜铠,关注我,带你看更多硬核知识。喜欢记得点赞加收藏哦。

Python画各种爱心相关推荐

  1. 如何使用python画一个爱心

    1 问题 如何使用python画一个爱心. 2 方法 桌面新建一个文本文档,文件后缀改为.py,输入相关代码ctrl+s保存,关闭,最后双击运行. 代码清单 1 from turtle import ...

  2. python画动态爱心代码_教你用python画动态爱心表白

    原标题:教你用python画动态爱心表白 初级画心 学Python,感觉你们的都好复杂,那我来个简单的,我是直接把心形看作是一个正方形+两个半圆: 于是这就很简单了,十行代码解决: import tu ...

  3. Python编程 利用Python画一个爱心

    作者简介:一名在校计算机学生.每天分享Python的学习经验.和学习笔记.   座右铭:低头赶路,敬事如仪 个人主页:网络豆的主页​​​​​​ 目录 前言 一.所使用的库 1.turtle库 2.情人 ...

  4. python画动态爱心-使用Python画出小人发射爱心的代码

    我就废话不多说了,直接上代码吧! #2.14 from turtle import * from time import sleep def go_to(x, y): up() goto(x, y) ...

  5. python画立体爱心_Python画爱心

    都说程序员不浪漫,上次看到一个程序员小哥给自己老婆开发了一个专属的APP.其实程序员还有更多美好的事情可以做,比如,给你喜欢的妹纸,用代码的方式去表白(当然可能还有一些前戏啥的,自己结合实际场景再渲染 ...

  6. python画一个爱心

    大家好这是我的地一篇博客,我要写一个关于python的文章我要用python写一个爱心. 不说别的,先看效果 效果如下: 话不多说,上代码,在这之前要下载python下载这事咱们放在最后现在上代码!! ...

  7. 用python画颗爱心祝生日快乐_Python画颗爱心祝生日快乐

    年年有今日,岁岁有今朝,为了幸福生活和家庭和谐,我家那位每年的生日是必须要认真准备认真过的.蛋糕和生日礼物是不能少的,更重要的是我一定要陪在身边.除此之外,今年再玩上一点花样,用Python来画颗心出 ...

  8. python画动画爱心

    # * coding:utf-8_*_ # 作者 :XiangLin # 创建时间 :26/02/2020 20:57 # 文件 :love.py # IDE :PyCharm import turt ...

  9. python爱心代码动态_一篇文章教你用python画动态爱心表白

    hRf免费资源网 初级画心hRf免费资源网 学Python,感觉你们的都好复杂,那我来个简单的,我是直接把心形看作是一个正方形+两个半圆:hRf免费资源网 hRf免费资源网 于是这就很简单了,十行代码 ...

最新文章

  1. #中的引用型是什么意识_excel的vlookup函数经常引用错误,让我告诉你原因,迅速脱离误区...
  2. 【zookeeper+Dubbo】zookeeper和Dubbo安装
  3. SAP Varient Configuration Type SAP变量配置类型
  4. Java ServletRequestListener监听器的使用
  5. 怎样封装一个自己的mvc框架(五)
  6. Windows WMIC命令使用详解(附实例)
  7. 工业机器人用铸铁牌号_常用铸铁牌号
  8. 关于ORACLE 语句中,IN 超过1000个的解决方法
  9. CVPR 2019 | Stereo R-CNN 3D 目标检测
  10. 原子性判断,防并发思路
  11. 针对EasyUI的checkbox进行扩展
  12. 多路复用增益,PASTA定理
  13. 卸载Notepad++
  14. Simulink与Flightgear联合仿真详细教程
  15. 三菱Q系列PLC大型程序Q01U伺服12轴 实际使用中程序
  16. 2265. 统计值等于子树平均值的节点数
  17. HTC Vive榜单:盘点一周最受欢迎的VR应用
  18. 中台和低代码,“零和”还是“竞合”?
  19. 相机的变焦,对焦和景深的理解和照相機的成像原理
  20. 对高维数据进行降维的算法

热门文章

  1. python怎么生成日志_python 生成模拟日志
  2. 互联网+智慧环保建设需求
  3. CentOS 使用ifconfig没有显示ip
  4. 【转帖】2018年Windows漏洞年度盘点
  5. 多机房UPS及环境集中监控方案丨UPS环境综合监控主机
  6. python期末试题汇总
  7. 光场相机系列-----相机标定
  8. python eel vue_VU - OSCHINA
  9. 从拟物化到扁平,再到Material Design
  10. asp.net mysql 连接池_asp.net 使用数据库连接池