以下分享几个创意Python爱心代码,涵盖字符画、数学曲线、动态效果等不同风格,可直接运行体验

 ASCII动态闪烁爱心

import time

def heartbeat():
    while True:
        print("\033[31m" + r'''
         ♥         ♥     
       ♥   ♥     ♥   ♥   
      ♥     ♥   ♥     ♥  
     ♥       ♥ ♥       ♥ 
      ♥       ♥       ♥  
       ♥             ♥   
         ♥         ♥     
           ♥     ♥       
             ♥ ♥         
              ♥          
        ''')
        time.sleep(0.5)
        print("\033[91m" + r'''
         ♥         ♥     
       ♥   ♥     ♥   ♥   
      ♥     ♥   ♥     ♥  
     ♥       ♥ ♥       ♥ 
      ♥       ♥       ♥  
       ♥             ♥   
         ♥         ♥     
           ♥     ♥       
             ♥ ♥         
              ♥          
        ''')
        time.sleep(0.5)

heartbeat()

效果‌:红色爱心在终端中交替深浅闪烁,按Ctrl+C停止


2. 数学函数三维爱心

import numpy as np
import matplotlib.pyplot as plt

# 心形方程:(x² + (5y/4 - sqrt(|x|))²) = 1
x = np.linspace(-2, 2, 1000)
y = np.linspace(-3, 1.5, 1000)
X, Y = np.meshgrid(x, y)
F = (X‌**2 + (5*Y/4 - np.sqrt(np.abs(X)))**‌2 - 1)

plt.contour(X, Y, F, , colors='red')
plt.text(0, -1.5, 'I Love Python', ha='center', fontsize=15, color='#FF69B4')
plt.axis('off')
plt.show()

效果‌:生成标准的数学函数爱心图形,底部显示文字


3. 交互式名字爱心墙

from turtle import *
import random

def draw_heart(name):
    speed(0)
    color('#FF1493')
    begin_fill()
    left(140)
    forward(180)
    circle(-90, 200)
    left(120)
    circle(-90, 200)
    forward(180)
    end_fill()
    
    penup()
    goto(0, 50)
    write(name, align="center", font=("Arial", 24, "bold"))
    hideturtle()

def fireworks():
    for _ in range(30):
        x = random.randint(-300, 300)
        y = random.randint(-200, 200)
        color(random.choice(['#FF4500','#FFD700','#FF69B4']))
        dot(random.randint(5, 15), (x, y))

screen = Screen()
screen.bgcolor('black')
draw_heart(input("输入你的名字:"))
fireworks()
done()

效果‌:输入名字后生成粉色爱心+名字+随机彩色烟花背景


4. 粒子流动爱心

import curses
import math

def main(stdscr):
    curses.curs_set(0)
    sh, sw = stdscr.getmaxyx()
    heart = []
    
    # 生成心形坐标
    for t in np.linspace(0, 2*math.pi, 200):
        x = 16*(math.sin(t)**3)
        y = 13*math.cos(t) - 5*math.cos(2*t) - 2*math.cos(3*t) - math.cos(4*t)
        heart.append((int(y+sh//2), int(x*2+sw//2)))
    
    # 粒子流动效果
    for i in range(1, len(heart)):
        stdscr.addstr(heart[i], heart[i], '*', curses.color_pair(1))
        stdscr.refresh()
        curses.napms(20)
        if i > 10: 
            stdscr.addstr(heart[i-10], heart[i-10], ' ')
    
curses.wrapper(main)

运行‌:终端输入python3 -m pip install curses后执行
效果‌:ASCII字符组成的爱心轮廓,粒子从右向左流动


5. 物理引擎弹跳爱心

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()

heart_img = pygame.image.load('heart.png')  # 需准备爱心图片
heart_rect = heart_img.get_rect(center=(400,300))
velocity = [5, -8]
gravity = 0.3

while True:
    screen.fill((255,255,255))
    for event in pygame.event.get():
        if event.type == QUIT: pygame.quit()
    
    velocity += gravity
    heart_rect = heart_rect.move(velocity)
    
    # 碰撞检测
    if heart_rect.left < 0 or heart_rect.right > 800:
        velocity *= -0.9
    if heart_rect.top < 0 or heart_rect.bottom > 600:
        velocity *= -0.8
    
    screen.blit(heart_img, heart_rect)
    pygame.display.update()
    clock.tick(60)

效果‌:爱心像皮球一样在窗口弹跳,速度逐渐衰减


选择建议‌:

  • 快速体验 → ‌方案1‌ (纯终端运行)
  • 数学之美 → ‌方案2‌ (需matplotlib)
  • 表白神器 → ‌方案3‌ (交互+烟花)
  • 极客风格 → ‌方案4‌ (终端粒子效果)
  • 游戏化设计 → ‌方案5‌ (物理弹跳)
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐