新年烟花python(附源码)

这篇具有很好参考价值的文章主要介绍了新年烟花python(附源码)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

import pygame
from random import randint, uniform, choice
import math

vector = pygame.math.Vector2
gravity = vector(0, 0.3)
DISPLAY_WIDTH = DISPLAY_HEIGHT = 800

trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75),
                 (125, 125, 125), (150, 150, 150)]
dynamic_offset = 1
static_offset = 5


class Firework:

    def __init__(self):
        self.colour = (randint(0, 255), randint(0, 255), randint(0, 255))
        self.colours = (
            (randint(0, 255), randint(0, 255), randint(0, 255)
             ), (randint(0, 255), randint(0, 255), randint(0, 255)),
            (randint(0, 255), randint(0, 255), randint(0, 255)))
        self.firework = Particle(randint(0, DISPLAY_WIDTH), DISPLAY_HEIGHT, True,
                                 self.colour)  # Creates the firework particle
        self.exploded = False
        self.particles = []
        self.min_max_particles = vector(100, 225)

    def update(self, win):  # called every frame
        if not self.exploded:
            self.firework.apply_force(gravity)
            self.firework.move()
            for tf in self.firework.trails:
                tf.show(win)

            self.show(win)

            if self.firework.vel.y >= 0:
                self.exploded = True
                self.explode()
        else:
            for particle in self.particles:
                particle.apply_force(
                    vector(gravity.x + uniform(-1, 1) / 20, gravity.y / 2 + (randint(1, 8) / 100)))
                particle.move()
                for t in particle.trails:
                    t.show(win)
                particle.show(win)

    def explode(self):
        amount = randint(self.min_max_particles.x, self.min_max_particles.y)
        for i in range(amount):
            self.particles.append(
                Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours))

    def show(self, win):
        pygame.draw.circle(win, self.colour, (int(self.firework.pos.x), int(
            self.firework.pos.y)), self.firework.size)

    def remove(self):
        if self.exploded:
            for p in self.particles:
                if p.remove is True:
                    self.particles.remove(p)

            if len(self.particles) == 0:
                return True
            else:
                return False


class Particle:

    def __init__(self, x, y, firework, colour):
        self.firework = firework
        self.pos = vector(x, y)
        self.origin = vector(x, y)
        self.radius = 20
        self.remove = False
        self.explosion_radius = randint(5, 18)
        self.life = 0
        self.acc = vector(0, 0)
        # trail variables
        self.trails = []  # stores the particles trail objects
        self.prev_posx = [-10] * 10  # stores the 10 last positions
        self.prev_posy = [-10] * 10  # stores the 10 last positions

        if self.firework:
            self.vel = vector(0, -randint(17, 20))
            self.size = 5
            self.colour = colour
            for i in range(5):
                self.trails.append(Trail(i, self.size, True))
        else:
            self.vel = vector(uniform(-1, 1), uniform(-1, 1))
            self.vel.x *= randint(7, self.explosion_radius + 2)
            self.vel.y *= randint(7, self.explosion_radius + 2)
            self.size = randint(2, 4)
            self.colour = choice(colour)
            for i in range(5):
                self.trails.append(Trail(i, self.size, False))

    def apply_force(self, force):
        self.acc += force

    def move(self):
        if not self.firework:
            self.vel.x *= 0.8
            self.vel.y *= 0.8

        self.vel += self.acc
        self.pos += self.vel
        self.acc *= 0

        if self.life == 0 and not self.firework:  # check if particle is outside explosion radius
            distance = math.sqrt((self.pos.x - self.origin.x)
                                 ** 2 + (self.pos.y - self.origin.y) ** 2)
            if distance > self.explosion_radius:
                self.remove = True

        self.decay()

        self.trail_update()

        self.life += 1

    def show(self, win):
        pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)),
                           self.size)

    def decay(self):  # random decay of the particles
        if 50 > self.life > 10:  # early stage their is a small chance of decay
            ran = randint(0, 30)
            if ran == 0:
                self.remove = True
        elif self.life > 50:
            ran = randint(0, 5)
            if ran == 0:
                self.remove = True

    def trail_update(self):
        self.prev_posx.pop()
        self.prev_posx.insert(0, int(self.pos.x))
        self.prev_posy.pop()
        self.prev_posy.insert(0, int(self.pos.y))

        for n, t in enumerate(self.trails):
            if t.dynamic:
                t.get_pos(self.prev_posx[n + dynamic_offset],
                          self.prev_posy[n + dynamic_offset])
            else:
                t.get_pos(self.prev_posx[n + static_offset],
                          self.prev_posy[n + static_offset])


class Trail:

    def __init__(self, n, size, dynamic):
        self.pos_in_line = n
        self.pos = vector(-10, -10)
        self.dynamic = dynamic

        if self.dynamic:
            self.colour = trail_colours[n]
            self.size = int(size - n / 2)
        else:
            self.colour = (255, 255, 200)
            self.size = size - 2
            if self.size < 0:
                self.size = 0

    def get_pos(self, x, y):
        self.pos = vector(x, y)

    def show(self, win):
        pygame.draw.circle(win, self.colour, (int(
            self.pos.x), int(self.pos.y)), self.size)


def update(win, fireworks):
    for fw in fireworks:
        fw.update(win)
        if fw.remove():
            fireworks.remove(fw)

    pygame.display.update()


def main():
    pygame.init()
    pygame.display.set_caption("Fireworks in Pygame")
    win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
    clock = pygame.time.Clock()

    fireworks = [Firework() for i in range(2)]  # create the first fireworks
    running = True

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

            if event.type == pygame.KEYDOWN:  # Change game speed with number keys
                if event.key == pygame.K_1:
                    fireworks.append(Firework())
                if event.key == pygame.K_2:
                    for i in range(10):
                        fireworks.append(Firework())
        win.fill((20, 20, 30))  # draw background

        if randint(0, 20) == 1:  # create new firework
            fireworks.append(Firework())

        update(win, fireworks)

        # stats for fun
        # total_particles = 0
        # for f in fireworks:
        #    total_particles += len(f.particles)

        # print(f"Fireworks: {len(fireworks)}\nParticles: {total_particles}\n\n")

    pygame.quit()
    quit()


main()

查看效果: 

新年烟花python(附源码),python,python,pygame,开发语言,pycharm文章来源地址https://www.toymoban.com/news/detail-756435.html

到了这里,关于新年烟花python(附源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【c++】新年烟花完整代码

    分享一下我的烟花代码,图片可以去百度自己搜索,如想要可以私信找我取 这是一个烟花连续放的程序,如果要更多的烟花道理相同,如果不想自己写可以私信我哦。

    2024年02月08日
    浏览(32)
  • 新年烟花代码-html版

    2024年02月02日
    浏览(33)
  • 【附源码】使用python+pygame开发消消乐游戏

    消消乐小游戏相信大家都玩过,大人小孩都喜欢玩的一款小游戏,那么基于程序是如何实现的呢?今天带大家,用python+pygame来实现一下这个花里胡哨的消消乐小游戏功能,感兴趣的朋友一起看看吧 目录 一、环境要求 二、游戏简介 三、完整开发流程 1、项目主结构 2、详细配

    2024年02月04日
    浏览(46)
  • pygame 烟花效果

    pygame.init() screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption(\\\'烟花效果\\\') particles = []  # 焰火粒子 def firework(x, y):     num_particles = 100  # 每次发射的粒子数量     for _ in range(num_particles):         direction = random.uniform(0, 2 * math.pi

    2024年04月23日
    浏览(36)
  • Python烟花(有源码)

    本人将圣诞树和烟花相结合进行了绘制,话不多说,代码如下,赶紧给你的心仪之人吧!时间仓促,大家千万别嘲笑哈,祝大家一切顺利!使用前记得改署名呦! 三连一下哦~谢谢!

    2024年02月08日
    浏览(32)
  • Python-动态烟花【附完整源码】

    运行效果:Python动态烟花代码

    2024年01月17日
    浏览(28)
  • 含源码,用Python实现浪漫烟花

    目录 前言 环境准备 代码编写 效果展示 Python实现浪漫的烟花特效 现在很多地方都不能放烟花了,既然看不到, 那作为程序猿的我们还不能自己用代码做一个吗? 今天就带大家用代码做一个烟花特效吧。 这里使用到的库有: pygame (用于游戏的编写)、random(用于产生随机

    2024年02月12日
    浏览(56)
  • 如何在pycharm中安装pygame游戏插件 和Python中安装pygame教程

    在用pycharm软件开发python小游戏前,需要安装pygame的插件,如何安装呢? 方法一、包管理器安装 1、在pycharm软件中定位到  file  – settings 2、定位到: project (自己的项目中)–python interpreter,选择右边的 pip 3、双击 pip ,进入查询插件界面,输入  pygame  ,进行查询这个插件

    2024年02月02日
    浏览(36)
  • 2023巨细的 Python安装库 以pycharm 和 Anaconda安装pygame为例

    在我们做 Python 实验或者编写代码中,总是需要导入各种库和包或者创建环境,这些库和包就需要我们学会下载和调用。下面以简单的例子 python安装库pygame库 , Anaconda 创建环境和安装实验需要的包 为例,其他的各种下载安装步骤都一样希望对大家有帮助。 pygame :pygame 是为

    2024年02月15日
    浏览(28)
  • 【2022最新Python跨年烟花代码,可自选背景音乐,快收藏起来吧 附完整源码】

    大家好,我是钱der,英文名chandler。 还有两天就到元旦了!!!很多小伙伴已经和朋友约好一起跨年了 但是现在很多地方都禁止烟花燃放,跨年没有烟花总感觉缺少一点味道~为了实现在家和朋友看烟花的愿望~这篇文章给大家带来2022跨年烟花代码,让大家都能在家和朋友一起

    2024年02月11日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包