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()
相关推荐
程序媛-徐师姐1 分钟前
基于 Python Django 的校园互助平台(附源码,文档)
开发语言·python·django·校园互助·校园互助平台
大数据追光猿24 分钟前
【深度学习】Pytorch项目实战-基于协同过滤实现物品推荐系统
人工智能·pytorch·python·深度学习·ai编程·推荐算法
师范大学生33 分钟前
基于CNN的FashionMNIST数据集识别2——模型训练
python·深度学习·cnn
web1376560764341 分钟前
纯 Python、Django、FastAPI、Flask、Pyramid、Jupyter、dbt 解析和差异分析
python·django·fastapi
大模型铲屎官44 分钟前
哈希表入门到精通:从原理到 Python 实现全解析
开发语言·数据结构·python·算法·哈希算法·哈希表
qq4054251971 小时前
基于python的旅客游记和轨迹分析可视化系统设计(新)
开发语言·python
m0_594526302 小时前
基于 PyQt5 实现分组列表滚动吸顶效果
开发语言·python·qt
thinkMoreAndDoMore3 小时前
深度学习(3)-TensorFlow入门(常数张量和变量)
开发语言·人工智能·python
kngines3 小时前
【Python量化金融实战】-第1章:Python量化金融概述:1.4 开发环境搭建:Jupyter Notebook、VS Code、PyCharm
python·量化金融
kngines3 小时前
【Python量化金融实战】-第1章:Python量化金融概述:1.2 Python在量化金融中的优势与生态
python·量化金融