python3D圣诞树

复制代码
import pygame
import math
from pygame.locals import *

# 初始化Pygame
pygame.init()

# 设置屏幕尺寸和标题
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('3D 圣诞树')

# 设置颜色
GREEN = (34, 139, 34)
BROWN = (139, 69, 19)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)

# 圣诞树参数
tree_height = 200
tree_width = 100
levels = 5
level_height = tree_height / levels
level_width = tree_width / levels

# 旋转角度
angle = 0

# 主循环
running = True
while running:
    screen.fill((0, 0, 0))

    # 处理事件
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

    # 创建3D圣诞树
    for i in range(levels):
        # 计算每一层的宽度和高度
        current_width = tree_width - i * level_width
        current_height = level_height

        # 计算每一层的四个顶点
        x1 = -current_width / 2
        y1 = -current_height * i
        x2 = current_width / 2
        y2 = y1

        x3 = 0
        y3 = current_height * (i + 1)

        # 绘制三角形
        pygame.draw.polygon(screen, GREEN, [(x1, y1), (x2, y2), (x3, y3)])

        # 添加装饰灯
        if i % 2 == 0:
            pygame.draw.circle(screen, RED, (int(x1 + current_width / 4), int(y1 + current_height / 2)), 5)
            pygame.draw.circle(screen, YELLOW, (int(x2 - current_width / 4), int(y2 + current_height / 2)), 5)

    # 绘制树干
    pygame.draw.rect(screen, BROWN, (-20, tree_height - 20, 40, 20))

    # 旋转效果
    angle += 0.01
    if angle > 2 * math.pi:
        angle = 0

    # 更新屏幕
    pygame.display.flip()

# 退出Pygame
pygame.quit()
相关推荐
余槐i2 分钟前
WAL 下写并发仍是 1:SQLite 单写者模型实测与绕开方案
数据库·python·性能优化·sqlite·wal
怕浪猫9 小时前
RAG 面试 6 连问,从原理到优化全部覆盖
python·算法·面试
高洁019 小时前
具身智能中的世界模型训练
人工智能·python·深度学习·机器学习·transformer
无线通信科研笔记9 小时前
IEEE TVT 2026 论文精读与完整复现|相位误差如何重塑近场 RIS 的幅相响应
论文阅读·人工智能·python·算法·论文笔记
朝朝辞暮i9 小时前
VLA 系统学习第 1 课:VLA 到底在干什么?
人工智能·python·计算机视觉·vla
2601_9628857210 小时前
如何用 Python 自动识别股票的支撑位与压力位?
开发语言·python
北冥有鱼被烹10 小时前
VCSEL全景解析:与光模块NPO CPO的关系、市场量化分析与产业链影响
python
caoerzhong10 小时前
中小企业上 WMS 该先上哪几块:JeeWMS 开源 Java 仓库管理系统的分批上线清单
java·python·开源
正在走向自律11 小时前
AI数据分析与可视化:从基础到应用实践
服务器·人工智能·python·机器学习·数据分析·pandas
databook11 小时前
从手动检查到自动监控:一个数据质量工作流的实现
后端·python·数据分析