Python绘制圣诞树

PythonTurtle库实现动态树形图形,
import turtle

# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor("#008080")

# 定义一个函数来绘制树杈
def branch(length, level):
    if level == 0:
        # 到达最后一层,绘制树叶
        turtle.color("#008000")
        turtle.begin_fill()
        turtle.circle(10)
        turtle.end_fill()
    else:
        # 绘制树枝
        turtle.pensize(level)
        turtle.color("#8B4513")
        turtle.forward(length)
        turtle.right(30)
        branch(length * 0.8, level - 1)
        turtle.left(60)
        branch(length * 0.8, level - 1)
        turtle.right(30)
        turtle.backward(length)

# 绘制树干
turtle.color("#8B4513")
turtle.pensize(20)
turtle.penup()
turtle.goto(0, -250)
turtle.pendown()
turtle.goto(0, -50)

# 绘制树杈
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
branch(100, 4)

# 绘制树底
turtle.color("#8B4513")
turtle.begin_fill()
turtle.penup()
turtle.goto(-150, -250)
turtle.pendown()
turtle.goto(150, -250)
turtle.goto(0, -300)
turtle.end_fill()

# 绘制星星
turtle.color("#FFFF00")
turtle.penup()
turtle.goto(0, 50)
turtle.pendown()
turtle.begin_fill()
for i in range(5):
    turtle.forward(20)
    turtle.right(144)
turtle.end_fill()

turtle.done()
 

 

 

 

### 3D 动态圣诞树 Python 实现 使用 `matplotlib` 和 `numpy` 可以创建一个动态旋转的 3D 圣诞树。以下代码结合参数化曲面与动功能,生成一个绿色锥形树体,并添加装饰点和动态旋转效果[^1]。 ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib.animation import FuncAnimation # 参数设置 fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111, projection='3d') ax.set_xlim(-5, 5) ax.set_ylim(-5, 5) ax.set_zlim(0, 10) ax.set_facecolor('black') ax.grid(False) ax.axis('off') # 生成圣诞树的点云 def generate_christmas_tree(u, v): u = u * 2 * np.pi v = v * 4 x = v * np.cos(u) * (1 - v / 10) y = v * np.sin(u) * (1 - v / 10) z = v return x, y, z # 装饰物(随机分布的小球) decoration_angles = np.random.rand(15) * 2 * np.pi decoration_heights = np.random.rand(15) * 6 + 2 decoration_colors = np.random.choice(['red', 'gold', 'blue', 'silver'], size=15) u = np.linspace(0, 1, 20) v = np.linspace(0, 1, 20) U, V = np.meshgrid(u, v) X, Y, Z = generate_christmas_tree(U, V) tree_surface = ax.plot_surface(X, Y, Z, color='forestgreen', alpha=0.9, edgecolor='none', shade=True)[^1] # 添加装饰点 deco_x = decoration_heights * np.cos(decoration_angles) * (1 - decoration_heights / 10) deco_y = decoration_heights * np.sin(decoration_angles) * (1 - decoration_heights / 10) deco_z = decoration_heights scat = ax.scatter(deco_x, deco_y, deco_z, color=decoration_colors, s=50, edgecolors='white', linewidth=1.2)[^1] # 动更新函数 angle = 0 def update(frame): global angle angle += 1 ax.view_init(elev=20, azim=angle) return tree_surface, scat[^1] ani = FuncAnimation(fig, update, frames=np.arange(0, 360, 2), interval=50, blit=False) plt.show() ``` 该实现中,树的形状通过极坐标参数方程构建,形成逐渐收窄的锥形结构。装饰点随机分布在树的不同高度上,模拟彩球效果。动通过不断更新视角方位角实现自动旋转视觉效果[^1]。 ### 光效增强版本(可选) 可通过添加闪烁灯光效果进一步提升视觉表现: ```python # 模拟闪烁灯光 lights_x = [] lights_y = [] lights_z = [] lights_color = [] for _ in range(30): h = np.random.rand() * 8 angle_light = np.random.rand() * 2 * np.pi r = h * 0.6 * (1 - h / 10) lights_x.append(r * np.cos(angle_light)) lights_y.append(r * np.sin(angle_light)) lights_z.append(h) lights_color.append(np.random.choice(['yellow', 'red', 'cyan', 'magenta'])) light_points = ax.scatter(lights_x, lights_y, lights_z, color='yellow', s=30, alpha=0.8)[^1] def update_with_lights(frame): ax.view_init(elev=20, azim=frame) # 随机改变部分灯光颜色或透明度 new_colors = np.random.choice(['yellow', 'red', 'cyan'], size=len(lights_x)) light_points.set_color(new_colors) light_points.set_alpha(np.random.rand() * 0.5 + 0.5) return tree_surface, scat, light_points[^1] ani = FuncAnimation(fig, update_with_lights, frames=np.arange(0, 360, 3), interval=100, blit=False)[^1] plt.show() ``` 此版本在树体表面添加了更多动态光源点,使其呈现闪烁效果,增强节日氛围[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值