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()
相关推荐
hui函数2 小时前
Flask电影投票系统全解析
后端·python·flask
闲人编程3 小时前
Python第三方库IPFS-API使用详解:构建去中心化应用的完整指南
开发语言·python·去中心化·内存·寻址·存储·ipfs
计算机编程小咖4 小时前
《基于大数据的农产品交易数据分析与可视化系统》选题不当,毕业答辩可能直接挂科
java·大数据·hadoop·python·数据挖掘·数据分析·spark
zhangfeng11335 小时前
以下是基于图论的归一化切割(Normalized Cut)图像分割工具的完整实现,结合Tkinter界面设计及Python代码示
开发语言·python·图论
flashlight_hi6 小时前
LeetCode 分类刷题:2529. 正整数和负整数的最大计数
python·算法·leetcode
Ashlee_code6 小时前
香港券商櫃台系統跨境金融研究
java·python·科技·金融·架构·系统架构·区块链
Jia-Hui Su6 小时前
GDSFactory环境配置(PyCharm+Git+KLayout)
git·python·pycharm
学习3人组7 小时前
手写数字识别代码
人工智能·python
古译汉书8 小时前
蓝桥杯算法之基础知识(2)——Python赛道
数据结构·python·算法·蓝桥杯
少陵野小Tommy8 小时前
Python能用古诗词数据库做什么7:根据标题、诗句查找诗歌
开发语言·数据库·python