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
相关推荐
Q_Q51100828523 分钟前
python+django/flask的情绪宣泄系统撸码猿29 分钟前
《Python AI入门》第9章 让机器读懂文字——NLP基础与情感分析实战二川bro34 分钟前
多模态AI开发:Python实现跨模态学习2301_764441331 小时前
Python构建输入法应用love530love1 小时前
【笔记】ComfUI RIFEInterpolation 节点缺失问题(cupy CUDA 安装)解决方案青瓷程序设计1 小时前
昆虫识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法秋邱2 小时前
智启未来:AGI 教育融合 × 跨平台联盟 × 个性化空间,重构教育 AI 新范式开篇:一场 “教育 ×AI” 的范式革命爱吃泡芙的小白白2 小时前
vscode、anaconda、git、python配置安装(自用)谷隐凡二2 小时前
Kubernetes主从架构简单解析:基于Python的模拟实现老歌老听老掉牙2 小时前
Matplotlib Pyplot 数据可视化完全指南