1. 分形树(递归艺术)
import turtle
def tree(branch_len, t):
if branch_len > 5:
t.forward(branch_len)
t.right(20); tree(branch_len-15, t) # 画右子树
t.left(40); tree(branch_len-15, t) # 画左子树
t.right(20); t.backward(branch_len)
t = turtle.Turtle()
t.left(90); tree(100, t)
turtle.done()
效果:递归生成一棵对称分形树,数学美感拉满
2. 动态彩色粒子
import pygame, math
pygame.init()
screen = pygame.display.set_mode((800,600))
particles = [(400,300, (abs(math.sin(i*0.1))*255, i*0.02) for i in range(500)] # 初始化粒子
while True:
screen.fill((0,0,0))
for i, (x, y, color, angle) in enumerate(particles):
particles[i] = (x+math.cos(angle)*2, y+math.sin(angle)*2, color, angle+0.01) # 更新位置
pygame.draw.circle(screen, (color,color,255), (int(x), int(y)), 2)
pygame.display.flip()
效果:500个蓝色粒子以正弦波轨迹流动,如同星河
3.Python 彩虹命令行
import time
while True:
for i in range(0, 256, 5):
print(f"\033[38;2;{255-i};{i};{255}m{'█'*30}\033[0m", end='\r')
time.sleep(0.02) # 控制颜色变化速度
效果:终端中动态渐变彩虹条(支持支持RGB的终端如Windows Terminal)
Python特效需安装pygame/turtle库。这些代码大多不超过10行,却融合了数学、物理模拟和视觉设计,用极简逻辑触发感官冲击——正是编程的魅力所在!
84

被折叠的 条评论
为什么被折叠?



