python动态圣诞下雪图

运行图片

代码

python 复制代码
import pygame
import random

# 初始化Pygame
pygame.init()

# 创建窗口
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Christmas Tree')

# 定义颜色
GREEN = (34, 139, 34)
RED = (255, 0, 0)
WHITE = (255, 255, 255)


# 绘制圣诞树
def draw_christmas_tree():
    triangle_height = 300
    triangle_width = 200

    # 绘制树干
    pygame.draw.rect(screen, pygame.Color('brown'), (width // 2 - 25, height - 200, 50, 200))

    # 绘制树冠
    for i in range(triangle_height):
        pygame.draw.polygon(screen, GREEN, [(width // 2 - triangle_width // 2 - i, height - 50 - i),
                                            (width // 2 + triangle_width // 2 + i, height - 50 - i),
                                            (width // 2, height - 50 - i - triangle_height)])


# 随机生成雪花
def generate_snowflakes():
    snowflakes = []
    for _ in range(100):
        x = random.randint(0, width)
        y = random.randint(0, height)
        size = random.randint(1, 5)
        snowflakes.append((x, y, size))
    return snowflakes


# 绘制雪花
def draw_snowflakes(snowflakes):
    for flake in snowflakes:
        pygame.draw.circle(screen, WHITE, (flake[0], flake[1]), flake[2])


# 绘制圣诞老人
def draw_santa():
    santa_image = pygame.Surface((120, 120), pygame.SRCALPHA)

    # 头部和身体
    pygame.draw.circle(santa_image, RED, (60, 60), 50)
    pygame.draw.polygon(santa_image, RED, [(0, 60), (120, 60), (60, 120)])

    # 眼睛和嘴巴
    pygame.draw.circle(santa_image, WHITE, (45, 45), 10)
    pygame.draw.circle(santa_image, WHITE, (75, 45), 10)
    pygame.draw.arc(santa_image, WHITE, (30, 45, 60, 60), 3.14 / 2, 3 * 3.14 / 2, 3)

    # 鼻子
    pygame.draw.polygon(santa_image, pygame.Color('orange'), [(60, 50), (70, 70), (50, 70)])

    # 胡须
    pygame.draw.line(santa_image, WHITE, (55, 85), (20, 90), 2)
    pygame.draw.line(santa_image, WHITE, (65, 85), (100, 90), 2)

    # 帽子
    pygame.draw.polygon(santa_image, RED, [(30, 0), (90, 0), (60, 60)])

    screen.blit(santa_image, (width // 2 - 60, height - 200 - 120))


# 游戏主循环
running = True
clock = pygame.time.Clock()
snowflakes = generate_snowflakes()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 0, 0))  # 清空屏幕

    draw_christmas_tree()
    draw_santa()
    draw_snowflakes(snowflakes)

    # 雪花下落
    for i in range(len(snowflakes)):
        x, y, size = snowflakes[i]
        snowflakes[i] = (x, y + size, size)
        if y + size > height:
            snowflakes[i] = (random.randint(0, width), 0, size)

    pygame.display.flip()  # 刷新屏幕
    clock.tick(30)  # 控制帧率

# 退出游戏
pygame.quit()
相关推荐
zone77391 天前
001:简单 RAG 入门
后端·python·面试
F_Quant1 天前
🚀 Python打包踩坑指南:彻底解决 Nuitka --onefile 配置文件丢失与重启报错问题
python·操作系统
允许部分打工人先富起来1 天前
在node项目中执行python脚本
前端·python·node.js
IVEN_1 天前
Python OpenCV: RGB三色识别的最佳工程实践
python·opencv
haosend1 天前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化
曲幽1 天前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
IVEN_2 天前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang2 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮2 天前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling2 天前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python