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()
python3D圣诞树
Chinese Red Guest2024-12-07 11:49
相关推荐
Amour恋空2 分钟前
Java多线程小陈工8 分钟前
2026年3月28日技术资讯洞察:5G-A边缘计算落地、低延迟AI推理革命与工业智造新范式智算菩萨41 分钟前
【OpenGL】10 完整游戏开发实战:基于OpenGL的2D/3D游戏框架、物理引擎集成与AI辅助编程指南jason成都2 小时前
IoT 设备监控系统实战:基于 EMQX 的 MQTT 连接监控与数据格式指纹识别愤豆2 小时前
05-Java语言核心-语法特性--模块化系统详解AI-Ming2 小时前
程序员转行学习 AI 大模型: 踩坑记录:服务器内存不够,程序被killed2401_873544923 小时前
使用Python处理计算机图形学(PIL/Pillow)njidf3 小时前
自动化机器学习(AutoML)库TPOT使用指南只与明月听3 小时前
RAG深入学习之向量数据库极光代码工作室3 小时前
基于Hadoop的日志数据分析系统设计