当前位置: 首页 > news >正文

神码ai伪原创工具【php源码】

大家好,小编为大家解答python炫酷烟花表白源代码的问题。很多人还不知道html代码烟花特效python,现在让我们一起来看看吧!

 

火车头采集ai伪原创插件截图:

目录

前言

环境准备

代码编写

效果展示


前言

Python实现浪漫的烟花特效
现在很多地方都不能放烟花了,既然看不到,
那作为程序猿的我们还不能自己用代码做一个吗?
今天就带大家用代码做一个烟花特效吧。


环境准备

这里使用到的库有:pygame(用于游戏的编写)、random(用于产生随机范围数)、math(用于数学计算),期中pygame属于第三方模块,如果未安装该模块可以使用命令:pip install pygame 进行安装。

pip install pygame

代码编写

全局变量:

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 = 3

 Firework : 整体部分

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 particleself.exploded = Falseself.particles = []self.min_max_particles = vector(100, 225)def update(self, win):  # 每帧调用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 = Trueself.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 数量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 Trueelse:return False

Particle:烟花粒子(包含轨迹)

class Particle:def __init__(self, x, y, firework, colour):self.firework = fireworkself.pos = vector(x, y)self.origin = vector(x, y)self.radius = 20self.remove = Falseself.explosion_radius = randint(5, 18)self.life = 0self.acc = vector(0, 0)# trail variablesself.trails = []  # stores the particles trail objectsself.prev_posx = [-10] * 10  # stores the 10 last positionsself.prev_posy = [-10] * 10  # stores the 10 last positionsif self.firework:self.vel = vector(0, -randint(17, 20))self.size = 5self.colour = colourfor 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)# 5 个 tails总计for i in range(5):self.trails.append(Trail(i, self.size, False))def apply_force(self, force):self.acc += forcedef move(self):if not self.firework:self.vel.x *= 0.8self.vel.y *= 0.8self.vel += self.accself.pos += self.velself.acc *= 0if self.life == 0 and not self.firework:  # 检查粒子的爆炸范围distance = math.sqrt((self.pos.x - self.origin.x) ** 2 + (self.pos.y - self.origin.y) ** 2)if distance > self.explosion_radius:self.remove = Trueself.decay()self.trail_update()self.life += 1def 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 particlesif 50 > self.life > 10:  # early stage their is a small chance of decayran = randint(0, 30)if ran == 0:self.remove = Trueelif self.life > 50:ran = randint(0, 5)if ran == 0:self.remove = Truedef 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])

 Trail:烟花轨迹,本质上是一个点 。创建 Trail 类,定义 show 方法绘制轨迹 、get_pos  实时获取轨迹坐标

class Trail:def __init__(self, n, size, dynamic):self.pos_in_line = nself.pos = vector(-10, -10)self.dynamic = dynamicif self.dynamic:self.colour = trail_colours[n]self.size = int(size - n / 2)else:self.colour = (255, 255, 200)self.size = size - 2if self.size < 0:self.size = 0def 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.font.init()pygame.display.set_caption("祝您新年快乐")  # 标题background = pygame.image.load("./5.png")  # 背景sound_wav = pygame.mixer.music.load("2.mp3")pygame.mixer.music.play()pygame.init()
# 加载背景音乐
'''pygame.mixer.music.load("./res/音乐文件名")
# 循环播放背景音乐
pygame.mixer.music.play(-1)
# 停止背景音乐
pygame.mixer.music.stop()
# 加载音效
boom_sound = pygame.mixer.Sound("./res/音效名")
# 播放音效
boom_sound.play()
boom_sound.stop()
myfont = pygame.font.Font("simkai.TTF", 80)
myfont1 = pygame.font.Font("simkai.ttf", 30)
testsurface = myfont.render("虎虎生威", False, (0, 0, 0), (220, 20, 60))
testsurface1 = myfont1.render("", False, (251, 59, 85))'''# pygame.image.load("")
win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
# win.blit(background)
clock = pygame.time.Clock()fireworks = [Firework() for i in range(2)]  # create the first fireworks
running = Truewhile running:clock.tick(60)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:  # Change game speed with number keysif event.key == pygame.K_1:  # 按下 1fireworks.append(Firework())if event.key == pygame.K_2:  # 按下 2 加入10个烟花for i in range(10):fireworks.append(Firework())if event.key == pygame.K_3:  # 按下 3 加入100个烟花for i in range(100):fireworks.append(Firework())win.fill((20, 20, 30))  # draw background#win.blit(background, (0, 0))#win.blit(testsurface, (200, 30))#win.blit(testsurface1, (520, 80))if randint(0, 20) == 1:  # 创建新的烟花fireworks.append(Firework())update(win, fireworks)
pygame.quit()
quit()

运行游戏:

if __name__ == 'main':main()

效果展示

http://www.lryc.cn/news/116127.html

相关文章:

  • Linux命令200例:mkdir用于创建目录(常用)
  • C语言内嵌汇编
  • 《网络是怎样连接的》(三)
  • SpringBoot 配置文件
  • 【K8S】 deployment.yaml文件与Service yaml文件详解
  • GMSL 9296芯片对GMSL链路 插损/回损/线束要求
  • 用库造一个list的轮子 【C++】
  • java中的,>>,<<位运算
  • 成功解决Android设备adb连接后显示device unauthorized
  • 初识mysql数据库之引入mysql客户端库
  • 勘探开发人工智能技术:机器学习(1)
  • MySQL查看当前数据库视图-SQL语句
  • Clickhouse 存储引擎
  • 基于golang多消息队列中间件的封装nsq,rabbitmq,kafka
  • 【第一阶段】kotlin的函数
  • PAM安全配置-用户密码锁定策略
  • AndroidManifest.xml日常笔记
  • SpringBoot异步框架
  • 导出LLaMA ChatGlm2等LLM模型为onnx
  • C++项目:在线五子棋对战网页版--匹配对战模块开发
  • ssh 连接断开,正在执行的shell脚本也被中断了
  • UML 用例图,类图,时序图,活动图
  • Java 面试题2023
  • 【CSS3】CSS3 动画 ④ ( 使用动画制作地图热点图 )
  • 命令模式(Command)
  • Dapper 微型orm的光
  • Mysql随心记--第一篇
  • 使用dockerfile安装各种服务组件
  • 如何简单的无人直播
  • 【基于HBase和ElasticSearch构建大数据实时检索项目】