当前位置:   article > 正文

烟花代码(包教包会)_randint, uniform,choice下载

randint, uniform,choice下载

新的一年就要来了,想放放烟花?但专家说放烟花会污染环境,这不,网上有安全无污染的烟花,这是用C写的代码烟花,有声音有配乐,还很好看

想要代码的去百度网盘自取

链接:https://pan.baidu.com/s/10VHpjHkjwXtlefybkuA2ow?pwd=new6 
提取码:new6

运行环境:   Visual Studio 2022       EasyX 

我已经加EasyX环境到文件里了,  点击 Fire\x64\Debug\Fire.exe 这个文件看看能运行吗?

不能运行就下载Visual Studio 2022配置环境

看下面

 

下载 Visual Studio 2022 这个在编程界有一定地位的IDE,懒得写了,看别人的文章

(10条消息) Visual Studio 2022 IDE 下载安装与环境配置,C语言/C++集成环境配置,VS2022。详细环境配置教程,最适合写c语言的编译器_立志冲海大的博客-CSDN博客_c语言vs工作负载选哪个https://blog.csdn.net/lwj8819/article/details/122384631

下载后配置EasyX环境

easyx下载

EasyX Graphics Library for C++EasyX Graphics Library 是针对 Visual C++ 的绘图库,支持 VC6.0 ~ VC2019,简单易用,学习成本极低,应用领域广泛。目前已有许多大学将 EasyX 应用在教学当中。https://easyx.cn/
进入EasyX的官网,按照如下步骤安装EasyX:

在这里插入图片描述

点击下载,下载最新版本。在电脑上下载的如下版本:
 在这里插入图片描述

 在这里插入图片描述

 下载成功后直接双击即可进行安装,此安装路径默认为VS2022中的安装路径。

 环境配置完,安装完重启一下Visual Studio2022就可以使用了

Visual Studio新建一个C++项目

 

 然后把刚才网盘下载的代码文件中的下面5个素材文件复制到新建的test项目中

 

Visual Studio,源文件——添加——现有项

 选择test项目下的Fire.cpp

 好了,可以点击运行了

 效果很惊艳是吧?

换另一个表白烟花的代码试试

链接:https://pan.baidu.com/s/1EfFaKSp9Ba9ROIP7K_qMqQ?pwd=love 
提取码:love

最后

附python烟花代码

 运行环境:PyCharm Community Edition   ,pygame库

  1. import pygame
  2. from random import randint, uniform, choice
  3. import math
  4. vector = pygame.math.Vector2
  5. gravity = vector(0, 0.3)
  6. DISPLAY_WIDTH = DISPLAY_HEIGHT = 800
  7. trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75),
  8. (125, 125, 125), (150, 150, 150)]
  9. dynamic_offset = 1
  10. static_offset = 5
  11. class Firework:
  12. def __init__(self):
  13. self.colour = (randint(0, 255), randint(0, 255), randint(0, 255))
  14. self.colours = (
  15. (randint(0, 255), randint(0, 255), randint(0, 255)
  16. ), (randint(0, 255), randint(0, 255), randint(0, 255)),
  17. (randint(0, 255), randint(0, 255), randint(0, 255)))
  18. self.firework = Particle(randint(0, DISPLAY_WIDTH), DISPLAY_HEIGHT, True,
  19. self.colour) # Creates the firework particle
  20. self.exploded = False
  21. self.particles = []
  22. self.min_max_particles = vector(100, 225)
  23. def update(self, win): # called every frame
  24. if not self.exploded:
  25. self.firework.apply_force(gravity)
  26. self.firework.move()
  27. for tf in self.firework.trails:
  28. tf.show(win)
  29. self.show(win)
  30. if self.firework.vel.y >= 0:
  31. self.exploded = True
  32. self.explode()
  33. else:
  34. for particle in self.particles:
  35. particle.apply_force(
  36. vector(gravity.x + uniform(-1, 1) / 20, gravity.y / 2 + (randint(1, 8) / 100)))
  37. particle.move()
  38. for t in particle.trails:
  39. t.show(win)
  40. particle.show(win)
  41. def explode(self):
  42. amount = randint(self.min_max_particles.x, self.min_max_particles.y)
  43. for i in range(amount):
  44. self.particles.append(
  45. Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours))
  46. def show(self, win):
  47. pygame.draw.circle(win, self.colour, (int(self.firework.pos.x), int(
  48. self.firework.pos.y)), self.firework.size)
  49. def remove(self):
  50. if self.exploded:
  51. for p in self.particles:
  52. if p.remove is True:
  53. self.particles.remove(p)
  54. if len(self.particles) == 0:
  55. return True
  56. else:
  57. return False
  58. class Particle:
  59. def __init__(self, x, y, firework, colour):
  60. self.firework = firework
  61. self.pos = vector(x, y)
  62. self.origin = vector(x, y)
  63. self.radius = 20
  64. self.remove = False
  65. self.explosion_radius = randint(5, 18)
  66. self.life = 0
  67. self.acc = vector(0, 0)
  68. # trail variables
  69. self.trails = [] # stores the particles trail objects
  70. self.prev_posx = [-10] * 10 # stores the 10 last positions
  71. self.prev_posy = [-10] * 10 # stores the 10 last positions
  72. if self.firework:
  73. self.vel = vector(0, -randint(17, 20))
  74. self.size = 5
  75. self.colour = colour
  76. for i in range(5):
  77. self.trails.append(Trail(i, self.size, True))
  78. else:
  79. self.vel = vector(uniform(-1, 1), uniform(-1, 1))
  80. self.vel.x *= randint(7, self.explosion_radius + 2)
  81. self.vel.y *= randint(7, self.explosion_radius + 2)
  82. self.size = randint(2, 4)
  83. self.colour = choice(colour)
  84. for i in range(5):
  85. self.trails.append(Trail(i, self.size, False))
  86. def apply_force(self, force):
  87. self.acc += force
  88. def move(self):
  89. if not self.firework:
  90. self.vel.x *= 0.8
  91. self.vel.y *= 0.8
  92. self.vel += self.acc
  93. self.pos += self.vel
  94. self.acc *= 0
  95. if self.life == 0 and not self.firework: # check if particle is outside explosion radius
  96. distance = math.sqrt((self.pos.x - self.origin.x)
  97. ** 2 + (self.pos.y - self.origin.y) ** 2)
  98. if distance > self.explosion_radius:
  99. self.remove = True
  100. self.decay()
  101. self.trail_update()
  102. self.life += 1
  103. def show(self, win):
  104. pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)),
  105. self.size)
  106. def decay(self): # random decay of the particles
  107. if 50 > self.life > 10: # early stage their is a small chance of decay
  108. ran = randint(0, 30)
  109. if ran == 0:
  110. self.remove = True
  111. elif self.life > 50:
  112. ran = randint(0, 5)
  113. if ran == 0:
  114. self.remove = True
  115. def trail_update(self):
  116. self.prev_posx.pop()
  117. self.prev_posx.insert(0, int(self.pos.x))
  118. self.prev_posy.pop()
  119. self.prev_posy.insert(0, int(self.pos.y))
  120. for n, t in enumerate(self.trails):
  121. if t.dynamic:
  122. t.get_pos(self.prev_posx[n + dynamic_offset],
  123. self.prev_posy[n + dynamic_offset])
  124. else:
  125. t.get_pos(self.prev_posx[n + static_offset],
  126. self.prev_posy[n + static_offset])
  127. class Trail:
  128. def __init__(self, n, size, dynamic):
  129. self.pos_in_line = n
  130. self.pos = vector(-10, -10)
  131. self.dynamic = dynamic
  132. if self.dynamic:
  133. self.colour = trail_colours[n]
  134. self.size = int(size - n / 2)
  135. else:
  136. self.colour = (255, 255, 200)
  137. self.size = size - 2
  138. if self.size < 0:
  139. self.size = 0
  140. def get_pos(self, x, y):
  141. self.pos = vector(x, y)
  142. def show(self, win):
  143. pygame.draw.circle(win, self.colour, (int(
  144. self.pos.x), int(self.pos.y)), self.size)
  145. def update(win, fireworks):
  146. for fw in fireworks:
  147. fw.update(win)
  148. if fw.remove():
  149. fireworks.remove(fw)
  150. pygame.display.update()
  151. def main():
  152. pygame.init()
  153. pygame.display.set_caption("Fireworks in Pygame")
  154. win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
  155. clock = pygame.time.Clock()
  156. fireworks = [Firework() for i in range(2)] # create the first fireworks
  157. running = True
  158. while running:
  159. clock.tick(60)
  160. for event in pygame.event.get():
  161. if event.type == pygame.QUIT:
  162. running = False
  163. if event.type == pygame.KEYDOWN: # Change game speed with number keys
  164. if event.key == pygame.K_1:
  165. fireworks.append(Firework())
  166. if event.key == pygame.K_2:
  167. for i in range(10):
  168. fireworks.append(Firework())
  169. win.fill((20, 20, 30)) # draw background
  170. if randint(0, 20) == 1: # create new firework
  171. fireworks.append(Firework())
  172. update(win, fireworks)
  173. # stats for fun
  174. # total_particles = 0
  175. # for f in fireworks:
  176. # total_particles += len(f.particles)
  177. # print(f"Fireworks: {len(fireworks)}\nParticles: {total_particles}\n\n")
  178. pygame.quit()
  179. quit()
  180. main()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/682056
推荐阅读
相关标签
  

闽ICP备14008679号