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()
相关推荐
历程里程碑21 小时前
滑动窗口------滑动窗口最大值
大数据·python·算法·elasticsearch·搜索引擎·flask·tornado
AI_567821 小时前
Python正则表达式终极指南:从模式匹配到文本工程的智能跃迁
人工智能·python·正则表达式
B站_计算机毕业设计之家21 小时前
AI大模型:Deepseek美食推荐系统 机器学习 协同过滤推荐算法+可视化 Django框架 大数据毕业设计(源码)✅
python·算法·机器学习·数据分析·django·推荐算法·美食
黄筱筱筱筱筱筱筱21 小时前
第三次Python练习题
开发语言·python
2501_9413220321 小时前
叉车人员检测识别实战:基于YOLOv8-BiFPN的高精度目标检测方案
python·yolo·目标检测
筷乐老六喝旺仔21 小时前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
刘一说1 天前
Java中基于属性的访问控制(ABAC):实现动态、上下文感知的权限管理
java·网络·python
一晌小贪欢1 天前
Python 操作 Excel 高阶技巧:用 openpyxl 玩转循环与 Decimal 精度控制
开发语言·python·excel·openpyxl·python办公·python读取excel
铁蛋AI编程实战1 天前
Falcon-H1-Tiny 微型 LLM 部署指南:100M 参数也能做复杂推理,树莓派 / 手机都能跑
java·人工智能·python·智能手机
写代码的【黑咖啡】1 天前
Python 中的自然语言处理工具:spaCy
开发语言·python·自然语言处理