5种不同创意的Python爱心图形代码实现方法

#创意Python爱心代码分享#

以下分享5种不同创意的Python爱心图形代码实现方法,包含数学公式推导和动态效果:

一、动态渐变色旋转爱心(turtle库)

import turtle
import colorsys

t = turtle.Turtle()
s = turtle.Screen()
s.bgcolor('black')
t.speed(0)
h = 0

for i in range(360):
    c = colorsys.hsv_to_rgb(h,1,1)
    t.pencolor(c)
    t.width(i/100+1)
    t.right(59)
    # 爱心参数方程
    t.forward(100)
    t.left(120)
    t.forward(100*(1 - (i%2)*0.2))
    t.left(60)
    t.backward(50)
    h += 0.005

turtle.done()

特点:通过HSV色彩空间实现渐变效果,结合循环变量控制线宽变化

二、数学函数爱心(matplotlib)

(x2+y2−1)3−x2y3=0(x2+y2−1)3−x2y3=0

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 1000)
y = np.linspace(-2, 2, 1000)
X, Y = np.meshgrid(x, y)
F = (X**2 + Y**2 - 1)**3 - X**2 * Y**3

plt.contour(X, Y, F, levels=[0], colors='red')
plt.axis('equal')
plt.title('Math Function Heart')
plt.show()

三、跳动3D爱心(matplotlib)

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

theta = np.linspace(0, 2*np.pi, 100)
z = np.linspace(-1, 1, 100)
theta, z = np.meshgrid(theta, z)

r = (np.sin(theta)*np.sqrt(np.abs(np.cos(theta))))/(np.sin(theta)+7/5) - 2*np.sin(theta) + 2
x = r * np.cos(theta)
y = r * np.sin(theta)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, color='red')
plt.show()

 四、文字组合爱心(ASCII艺术)

def create_heart(text):
    heart = []
    length = len(text)
    for y in range(12, -12, -1):
        line = []
        for x in range(-30, 30):
            if ((x*0.04)**2 + (y*0.1)**2 -1)**3 - (x*0.04)**2*(y*0.1)**3 <=0:
                line.append(text[(x-y) % length])
            else:
                line.append(' ')
        heart.append(''.join(line))
    return '\n'.join(heart)

print(create_heart('LOVE'))

五、粒子动画爱心(pygame)

import pygame
import math
import random

pygame.init()
screen = pygame.display.set_mode((800,600))
particles = []

class Particle:
    def __init__(self):
        self.angle = random.uniform(0, 2*math.pi)
        self.radius = random.randint(50,200)
        self.speed = random.uniform(0.02, 0.1)
        self.color = (random.randint(200,255), 0, random.randint(50,150))
        
    def update(self):
        self.angle += self.speed
        x = 400 + self.radius * math.sin(self.angle) * 16 * (math.sin(math.pi*self.angle))**3
        y = 300 - self.radius * (13*math.cos(self.angle) - 5*math.cos(2*self.angle) - 
                               2*math.cos(3*self.angle) - math.cos(4*self.angle))
        return (int(x), int(y))

running = True
while running:
    # 事件处理...
    screen.fill((0,0,30))
    particles = [Particle() for _ in range(50)] + particles[:500]
    for p in particles:
        pos = p.update()
        pygame.draw.circle(screen, p.color, pos, 2)
    pygame.display.flip()
    pygame.time.wait(30)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值