[Python烟花]Python小练习:实现满屏幕随即烟花

一、内容概述

本文同之前的飞机大战一样,面向python3的初学者。在这篇文章中,我们通过pygame、math、random三个包提供的内容实现满屏幕的烟花绽放,烟花会从屏幕最底端发射,沿着直线,并在随机位置发生爆炸,爆炸后,会在爆炸位置产生众多烟花颗粒,这些烟花颗粒会渐渐消失.

二、最终代码及成果展示

最终代码

import pygame
import random
import math

class Firework():
    def __init__(self, x, y, screen):
        self.screen = screen
        self.start_x = x
        self.start_y = y
        self.cur_x = x
        self.cur_y = y
        self.color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))

    def draw(self):
        pygame.draw.circle(self.screen, self.color, (self.cur_x, self.cur_y), 5)

    def move_up(self):
        self.cur_y -= 5

class Particle():
    def __init__(self, x, y, screen):
        self.x = x
        self.y = y
        self.screen = screen
        self.color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        self.speed = random.uniform(1, 3)
        self.angle = random.uniform(0, 2 * math.pi)
        self.radius = random.randint(2, 4)
        self.life = random.randint(10, 20)   #决定烟花颗粒存活的时间

    def move(self):
        self.x += self.speed * math.cos(self.angle)
        self.y += self.speed * math.sin(self.angle)
        self.life -= 1

    def draw(self):
        pygame.draw.circle(self.screen, self.color, (int(self.x), int(self.y)), self.radius)

def main():
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    clock = pygame.time.Clock()

    fireworks = []
    particles = []

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        fireworks.append(Firework(random.randint(0,800), 600, screen))

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

        for f in fireworks:
            f.draw()
            f.move_up()
            if random.randint(1, 100) > 96:
                for _ in range(random.randint(50,100)):
                    particles.append(Particle(f.cur_x, f.cur_y, screen))
                fireworks.remove(f)

        for p in particles:
            p.draw()
            p.move()
            if p.life <= 0:
                particles.remove(p)

        pygame.display.flip()
        clock.tick(60)  # 控制刷新率

main()

三、代码详解

首先我们引入三个必要的包

import pygame
import random
import math

然后,我们定义了一个最重要的类,及决定烟花颗粒的类,在Particle类中,最为重要的是life属性,通过调节life的random范围,可以随机决定烟花颗粒的存活时间;radius属性决定烟花颗粒的大小,其余属性一看就懂

class Particle():
    def __init__(self, x, y, screen):
        self.x = x
        self.y = y
        self.screen = screen
        self.color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        self.speed = random.uniform(1, 3)
        self.angle = random.uniform(0, 2 * math.pi)
        self.radius = random.randint(2, 4)
        self.life = random.randint(10, 20)   #决定烟花颗粒存活的时间

    def move(self):
        self.x += self.speed * math.cos(self.angle)
        self.y += self.speed * math.sin(self.angle)
        self.life -= 1

    def draw(self):
        pygame.draw.circle(self.screen, self.color, (int(self.x), int(self.y)), self.radius)

这个类用于生成烟花本身

class Firework():
    def __init__(self, x, y, screen):
        self.screen = screen
        self.start_x = x
        self.start_y = y
        self.cur_x = x
        self.cur_y = y
        self.color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))

    def draw(self):
        pygame.draw.circle(self.screen, self.color, (self.cur_x, self.cur_y), 5)

    def move_up(self):
        self.cur_y -= 5

最后,我们定义主函数,并在主函数中实现代码循环,完成最主要部分

def main():
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    clock = pygame.time.Clock()

    fireworks = []
    particles = []

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        fireworks.append(Firework(random.randint(0,800), 600, screen))

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

        for f in fireworks:
            f.draw()
            f.move_up()
            if random.randint(1, 100) > 96:
                for _ in range(random.randint(50,100)):
                    particles.append(Particle(f.cur_x, f.cur_y, screen))
                fireworks.remove(f)

        for p in particles:
            p.draw()
            p.move()
            if p.life <= 0:
                particles.remove(p)

        pygame.display.flip()
        clock.tick(60)  # 控制刷新率

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 烟花代码是一种用Python编写的模拟烟花爆炸效果的程序。它通过绘制图形和模拟物理效果来实现烟花的动态效果。 以下是一个简单的Python烟花代码示例: ```python import random import turtle # 创建画布 canvas = turtle.Screen() canvas.bgcolor("black") # 创建烟花粒子类 class Particle(turtle.Turtle): def __init__(self, color): turtle.Turtle.__init__(self) self.shape("circle") self.color(color) self.penup() def launch(self): self.goto(0, -200) self.setheading(random.randint(0, 360)) self.speed(10) self.showturtle() def explode(self): self.hideturtle() for _ in range(20): self.color(random.choice(["red", "orange", "yellow", "green", "blue", "purple"])) self.goto(random.randint(-300, 300), random.randint(-300, 300)) self.showturtle() # 创建烟花管理类 class FireworkManager: def __init__(self): self.particles = [] def launch_firework(self): particle = Particle("white") particle.launch() self.particles.append(particle) def explode_firework(self): for particle in self.particles: particle.explode() # 创建烟花管理器对象 manager = FireworkManager() # 发射烟花 manager.launch_firework() # 等待一段时间后,烟花爆炸 turtle.delay(1000) manager.explode_firework() # 关闭画布 turtle.done() ``` 这段代码使用了Python的turtle库来绘制图形,并通过模拟粒子的运动和爆炸效果来实现烟花的动态效果。代码中定义了一个烟花粒子类Particle,以及一个烟花管理类FireworkManager。通过调用FireworkManager的launch_firework方法来发射烟花,然后等待一段时间后调用explode_firework方法使烟花爆炸。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值