> 技术文档 > 高级Python爱心代码分享_3d立体爱心粒子代码python

高级Python爱心代码分享_3d立体爱心粒子代码python

以下是一些创意且高级的Python爱心代码实现,使用不同的技术和方法来生成爱心图案:

目录

1. 3D旋转爱心 (使用matplotlib)

2. 动态粒子爱心 (使用pygame)

3. ASCII艺术爱心 (带颜色和动画)

4. 爱心树 (分形实现)

5. 爱心烟花 (使用turtle图形)


1. 3D旋转爱心 (使用matplotlib)

import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import animationdef heart_3d(x, y, z): return (x**2 + (9/4)*y**2 + z**2 - 1)**3 - x**2*z**3 - (9/80)*y**2*z**3def plot_heart(): fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(111, projection=\'3d\') x = np.linspace(-1.5, 1.5, 100) y = np.linspace(-1.5, 1.5, 100) z = np.linspace(-1.5, 1.5, 100) X, Y, Z = np.meshgrid(x, y, z) vol = heart_3d(X, Y, Z) verts, faces, _, _ = marching_cubes(vol, 0, spacing=(0.1, 0.1, 0.1)) ax.plot_trisurf(verts[:, 0], verts[:, 1], faces, verts[:, 2],  color=\'red\', lw=0, alpha=0.8) ax.set_xlim(-1.5, 1.5) ax.set_ylim(-1.5, 1.5) ax.set_zlim(-1.5, 1.5) ax.axis(\'off\') def rotate(angle): ax.view_init(30, angle) return fig, anim = animation.FuncAnimation(fig, rotate, frames=360, interval=50) plt.show() return anim# 需要安装scikit-image库来使用marching_cubesfrom skimage.measure import marching_cubesplot_heart()

2. 动态粒子爱心 (使用pygame)

import pygameimport numpy as npimport randomimport mathpygame.init()width, height = 800, 600screen = pygame.display.set_mode((width, height))pygame.display.set_caption(\"粒子爱心\")particles = []for i in range(500): x = random.randint(0, width) y = random.randint(0, height) particles.append([x, y, random.uniform(0.5, 2), random.uniform(0, 2*math.pi)])def heart_shape(t, scale=15): 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) return width//2 + x * scale, height//2 - y * scalerunning = Trueclock = pygame.time.Clock()while running: screen.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 更新粒子位置 for i, p in enumerate(particles): t = p[3] p[3] += 0.01 if p[3] > 2 * math.pi: p[3] = 0 target_x, target_y = heart_shape(p[3], scale=20) p[0] += (target_x - p[0]) * 0.05 p[1] += (target_y - p[1]) * 0.05 color = (255, int(100 + 100 * math.sin(p[3])), int(100 + 100 * math.sin(p[3]))) pygame.draw.circle(screen, color, (int(p[0]), int(p[1])), int(p[2])) pygame.display.flip() clock.tick(60)pygame.quit()

3. ASCII艺术爱心 (带颜色和动画)

import timeimport osimport mathfrom colorama import init, Foreinit()def heart_shape(t, scale=1): 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) return x * scale, y * scaledef draw_heart(): for _ in range(100): os.system(\'cls\' if os.name == \'nt\' else \'clear\') for y in range(30, -30, -1): line = \"\" for x in range(-60, 60): char = \" \" for t in range(0, 628, 10):  hx, hy = heart_shape(t/100, scale=10)  if abs(x/2 - hx) < 1 and abs(y - hy)  200 or g > 200 or b > 200: char = Fore.RED + \"❤\" + Fore.RESET else: char = Fore.MAGENTA + \"♥\" + Fore.RESET break line += char print(line) time.sleep(0.1)draw_heart()

4. 爱心树 (分形实现)

import turtleimport randomimport colorsysdef draw_tree(branch_len, t, hue=0.1): if branch_len < 3: return else: # 绘制爱心作为叶子 if branch_len < 15: t.penup() t.goto(t.xcor(), t.ycor() - 5) t.pendown() t.fillcolor(colorsys.hsv_to_rgb(hue, 1, 1)) t.begin_fill() t.left(140) t.forward(10) for _ in range(200): t.right(1) t.forward(0.1) t.left(120) for _ in range(200): t.right(1) t.forward(0.1) t.forward(10) t.end_fill() t.penup() t.goto(t.xcor(), t.ycor() + 5) t.pendown() return angle = random.randint(15, 45) sf = random.uniform(0.6, 0.8) t.pensize(branch_len / 10) t.forward(branch_len) # 右分支 t.right(angle) draw_tree(branch_len * sf, t, hue + 0.02) # 左分支 t.left(angle * 2) draw_tree(branch_len * sf, t, hue + 0.02) t.right(angle) t.backward(branch_len)def main(): t = turtle.Turtle() screen = turtle.Screen() screen.bgcolor(\"black\") t.speed(0) t.left(90) t.penup() t.goto(0, -200) t.pendown() t.color(\"brown\") draw_tree(100, t) t.hideturtle() screen.exitonclick()main()

5. 爱心烟花 (使用turtle图形)

import turtleimport randomimport mathdef draw_heart(x, y, size, color): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.color(color) turtle.begin_fill() turtle.left(140) turtle.forward(size) for _ in range(200): turtle.right(1) turtle.forward(size/100) turtle.left(120) for _ in range(200): turtle.right(1) turtle.forward(size/100) turtle.forward(size) turtle.end_fill()def firework(): colors = [\'red\', \'orange\', \'yellow\', \'green\', \'blue\', \'purple\', \'pink\'] for _ in range(8): x = random.randint(-300, 300) y = random.randint(-300, 300) size = random.randint(20, 50) color = random.choice(colors) draw_heart(x, y, size, color) turtle.update() turtle.ontimer(lambda: draw_heart(x, y, size, \'black\'), 1000)def main(): turtle.setup(800, 800) turtle.bgcolor(\'black\') turtle.title(\"爱心烟花\") turtle.tracer(0) turtle.hideturtle() for _ in range(5): firework() turtle.ontimer(firework, 2000) turtle.mainloop()main()

这些代码展示了不同风格的爱心实现,包括3D效果、粒子系统、ASCII艺术、分形和动画效果。要运行这些代码,你需要安装相应的库(如matplotlib、pygame、colorama等)。每个示例都可以进一步自定义和扩展,创造出更独特的效果。