Python绘画案例4——用Python的turtle模块画烟花秀,包含源码

部署运行你感兴趣的模型镜像

Python绘画案例4——用Python的turtle模块画烟花秀,包含源码

写在开始

这个Python专栏大概会出一百多个绘画作品,大家有喜欢的收藏关注一下,谢谢咯~

运行结果展示

烟花秀

话不多说,展示源代码

import pygame
import random
import math

# 初始化参数
pygame.init()
info = pygame.display.Info()
WIDTH, HEIGHT = info.current_w, info.current_h  # 自动获取屏幕分辨率
# 新增关闭按钮参数
CLOSE_BUTTON_RADIUS = 20
CLOSE_BUTTON_POS = (WIDTH - CLOSE_BUTTON_RADIUS - 10, CLOSE_BUTTON_RADIUS + 10)
FPS = 60
COLORS = [
    (255,255,255),   # White
    (240,230,140),   # Khaki
    (135,206,235),   # SkyBlue
    (0,0,255),       # Blue
    (0,255,255),     # Cyan
    (255,192,203),  # 标准粉
    (255,182,193),  # 浅粉
    (255,20,147),   # 艳粉
    (255,105,180),  # HotPink(热粉)
    (255,0,127),    # 霓虹粉
    (199,21,133)    # MediumVioletRed(紫调粉)
]


class Firework:
    def __init__(self):
        self.x = random.randint(50, WIDTH - 50)
        self.y = HEIGHT
        self.base_color = random.choice(COLORS)  # 基础颜色
        self.speed = random.uniform(8, 12)  # 提升升空速度
        self.trail = []  # 轨迹数组
        self.exploded = False

    def move(self):
        self.y -= self.speed
        self.trail.append((self.x, self.y))  # 记录轨迹
        if len(self.trail) > 15:  # 限制轨迹长度
            self.trail.pop(0)

        if self.y < HEIGHT * 0.3 + random.randint(-50, 50):
            self.explode()
            return False
        return True

    def explode(self):
        global sparks
        explosion_colors = [
            self.base_color,
            (min(self.base_color[0] + 50, 255),
             min(self.base_color[1] + 50, 255),
             min(self.base_color[2] + 50, 255)),
            (max(self.base_color[0] - 50, 0),
             max(self.base_color[1] - 50, 0),
             max(self.base_color[2] - 50, 0))
        ]
        for _ in range(150):  # 增加爆炸粒子数量
            angle = random.uniform(0, 2 * math.pi)
            speed = random.uniform(4, 8)
            life = random.randint(80, 120)
            sparks.append(Spark(
                self.x, self.y,
                random.choice(explosion_colors),  # 颜色渐变组合
                angle, speed, life
            ))


class Spark:
    def __init__(self, x, y, color, angle, speed, life):
        self.x = x
        self.y = y
        self.color = color
        self.vx = math.cos(angle) * speed * random.uniform(0.8, 1.2)
        self.vy = math.sin(angle) * speed * random.uniform(0.8, 1.2)
        self.life = life
        self.gravity = 0.25 + random.random() * 0.1  # 随机重力系数
        self.size = random.randint(2, 4)  # 随机粒子大小

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += self.gravity
        self.life -= 2
        return self.life > 0


def resize_screen():
    global WIDTH, HEIGHT
    WIDTH, HEIGHT = pygame.display.get_surface().get_size()


# 初始化窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN | pygame.SCALED)
pygame.display.set_caption("烟花秀")
clock = pygame.time.Clock()

fireworks = []
sparks = []

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.VIDEORESIZE:
            resize_screen()
        elif event.type == pygame.MOUSEBUTTONDOWN:  # 检测鼠标点击
            if event.button == 1:  # 左键点击
                # 计算鼠标与关闭按钮的距离
                mouse_pos = pygame.mouse.get_pos()
                distance = math.hypot(mouse_pos[0] - CLOSE_BUTTON_POS[0],
                                    mouse_pos[1] - CLOSE_BUTTON_POS[1])
                if distance <= CLOSE_BUTTON_RADIUS:
                    running = False

    screen.fill((0, 0, 0))

    pygame.draw.circle(screen, (135,206,235), CLOSE_BUTTON_POS, CLOSE_BUTTON_RADIUS)
    offset = 8
    pygame.draw.line(screen, (255, 255, 255),
                    (CLOSE_BUTTON_POS[0] - offset, CLOSE_BUTTON_POS[1] - offset),
                    (CLOSE_BUTTON_POS[0] + offset, CLOSE_BUTTON_POS[1] + offset), 3)
    pygame.draw.line(screen, (255, 255, 255),
                    (CLOSE_BUTTON_POS[0] + offset, CLOSE_BUTTON_POS[1] - offset),
                    (CLOSE_BUTTON_POS[0] - offset, CLOSE_BUTTON_POS[1] + offset), 3)

    if random.random() < 0.04:
        fireworks.append(Firework())

    for fw in fireworks:
        for i, pos in enumerate(fw.trail):
            alpha = 255 * (i / len(fw.trail))
            pygame.draw.circle(screen, fw.base_color + (int(alpha),), pos, 3 * (i / len(fw.trail)))

    for fw in fireworks[:]:
        if fw.move():
            pygame.draw.circle(screen, fw.base_color, (int(fw.x), int(fw.y)), 5)
        else:
            fireworks.remove(fw)

    for sp in sparks[:]:
        if sp.update():
            alpha = max(0, min(255, int(255 * (sp.life / 120))))
            pygame.draw.circle(screen, sp.color + (alpha,), (int(sp.x), int(sp.y)), sp.size)
        else:
            sparks.remove(sp)

    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值