【七夕特辑】用python来实现跳动爱心+爱心心电图+玫瑰花+.....

前言

什么??明天七夕?学了这么久的python,还写不出来给 对象或者心动Crush 的一个表白代码?

在这里插入图片描述

还得是我来帮你们准备几个吧
有去年比较火的跳动爱心,也有其他的,来看看,挑选一下,领取代码后,不要忘记发给人家了

在这里插入图片描述

emmm先来给你们都看一眼效果吧

获取代码 点击文末名片~

在这里插入图片描述

还有一些其他的 在这里我就不多说啦 不过都可以找文末名片直接获取哦

在这里插入图片描述

在这里插入图片描述

爱心跳动代码

请添加图片描述

代码

import random
from math import sin, cos, pi, log
from tkinter import *

CANVAS_WIDTH = 640  # 画布的宽
CANVAS_HEIGHT = 480  # 画布的高

CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标

HEART_COLOR = "#ff8181"  # 心的颜色,芜湖我喜欢的粉色

def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    """
    “爱心函数生成器”
    :param shrink_ratio: 放大比例
    :param t: 参数
    :return: 坐标
    """
    # 基础函数
    x = 16 * (sin(t) ** 3)
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

# 放大
    x *= shrink_ratio
    y *= shrink_ratio

    # 移到画布中央
    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y

    return int(x), int(y)

def scatter_inside(x, y, beta=0.15):
    """
    随机内部扩散
    :param x: 原x
    :param y: 原y
    :param beta: 强度
    :return: 新坐标
    """
    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())

    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)
python学习交流Q群:770699889 ### 源码领取
    return x - dx, y - dy

    """
    抖动
    :param x: 原x
    :param y: 原y
    :param ratio: 比例
    :return: 新坐标
 def calc(self, generate_frame):
        ratio = 10 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例

        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))

        all_points = []
         # 光环
        heart_halo_point = set()  # 光环的点坐标集合
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t, shrink_ratio=11.6)  # 魔法参数
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                # 处理新的点
                heart_halo_point.add((x, y))
                x += random.randint(-14, 14)
                y += random.randint(-14, 14)
                size = random.choice((1, 2, 2))
                all_points.append((x, y, size))

# 轮廓
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))
# 内容
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        self.all_points[generate_frame] = all_points

def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
python学习交流Q群:770699889 ### 源码领取

if __name__ == '__main__':
    root = Tk()  # 一个Tk
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()  # 心
    draw(root, canvas, heart)  # 开始画画~
    root.mainloop()

完整代码

【文末名片获取代码】

在这里插入图片描述

在这里插入图片描述

爱心心电图

代码

导入模块

import os
import pygame
import turtle as t
t.title("Python学习交流Q群:770699889")

画布大小

#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
t.hideturtle()

画爱心

def heart(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.color('pink')
    t.setheading(50)
    t.circle( -5, 180)
    t.circle( -45, 12)
    t.setheading(130)
    t.circle( -45, 12)
    t.circle( -5, 180)
heart(-30, 155)
heart(-220, 145)
heart(-210, 60)
heart(-100, 100)
heart(-20, 20)
heart(-70, 130)
heart(-140, -20)
heart(30, 100)
heart(-60, -20)
heart(10, 60)
heart(-100, -70)
heart(20, 145)
heart(-140, -20)
heart(-130, 130)
heart(-180, 20)
heart(-170, 155)
heart(-230, 100)
def write_mes(x, y, size, ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('black')
    t.write(ss, font=('Times New Roman', size, 'normal'))

画红心

print('画红心')
def heart_fill(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.color('red', 'red')
    t.begin_fill()
    t.setheading(50)
    t.circle( -5, 180)
    t.circle( -45, 12)
    t.setheading(130)
    t.circle( -45, 12)
    t.circle( -5, 180)
    t.end_fill()
x = 90
y = 110

右边爱心
这边的 ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ 是可以改成自己想说的话哦

write_mes(x, y, 11, '1')
heart_fill(-100, 100)
heart_fill(-70, 130)
heart_fill(-30, 155)
heart_fill(20, 145)
heart_fill(30, 100)
write_mes(x, y-30, 11, '2')
heart_fill(10, 60)
heart_fill(-20, 20)
heart_fill(-60, -20)
heart_fill(-100, -70)

左边爱心

write_mes(x, y-30*2, 11, '3')
heart_fill(-140, -20)
heart_fill(-180, 20)
heart_fill(-210, 60)
heart_fill(-230, 100)
write_mes(x, y-30*3, 11, '4')
heart_fill(-220, 145)
heart_fill(-170, 155)
heart_fill(-130, 130)
write_mes(x, y-30*4, 11, '5')
t.speed(200)

画心动线

t.penup()
t.goto(-170, 40)
t.pendown()
t.pencolor('red')
t.setheading(0)
t.pensize(2)
t.forward(10)

第一个小波浪

t.setheading(45)
t.circle(50, 10)
t.setheading(0)
t.circle(-3,90)
t.circle(50, 5)

横线

t.setheading(0)
t.forward(10)

第一个下尖峰

t.setheading(-80)
t.forward(7)
t.setheading(70)
t.forward(25)
t.setheading(-85)
t.forward(29)
t.setheading(70)
t.forward(13)
t.setheading(0)
t.forward(15)

画心

t.setheading(150)
t.circle(-20, 40)
t.circle(-10, 170)
t.setheading(70)
t.circle(-10, 170)
t.circle(-20, 40)
t.setheading(0)
t.forward(15)

写两个人的姓名
铛铛 ~ 这里就是写爱心里面的两个人的名字啦

    write_name(-180, 70, 11, '小圆')
    write_name(-180, 70, 11, '小圆')
    write_name(-180, 70, 11, '小圆')
    heart_bit()
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back2()
while 1:
    name_heart_bit()

玫瑰花

在这里插入图片描述

代码

模块

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.font_manager import FontProperties
  • 改变绘制图像的视角,即相机的位置,azim沿着z轴旋转,elev沿着y轴

中间的文字内容可以自定义修改

fig = plt.figure(figsize=(6, 8))
ax = fig.gca(projection='3d')
elev = 22
azim = 2.5
ax.view_init(elev, azim)  # 改变绘制图像的视角,即相机的位置,azim沿着z轴旋转,elev沿着y轴
font_set = FontProperties(fname=r"C:\Windows\Fonts\simhei.TTF", size=20)
ax.text(1, -0.8, 0, '"唯一的花送给我爱的宝贝"', fontproperties=font_set)

花梗

  • 把圆分按角度为50等分
u2 = np.linspace(0, 2 * np.pi, 50)
  • 把高度1均分为20份, 玫瑰花梗高度范围
h2 = np.linspace(0, 4, 20)  
  • x y 值重复20次
x2 = np.outer(0.05 * np.sin(u2), np.ones(len(h2)))  
y2 = np.outer(0.05 * np.cos(u2), np.ones(len(h2)))  
  • x,y 对应的高度
z2 = np.outer(np.ones(len(u2)), h2)  

完整代码

【点击文末名片可以直接获取】

在这里插入图片描述

最后

文章分享到这里就结束了 希望对大家有所帮助 ~

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值