完整的跨年烟花代码python
时间: 2023-08-31 08:19:18 浏览: 338
### 回答1:
好的,下面是一个简单的跨年烟花效果的 Python 代码。这个代码使用了 Pygame 库来创建窗口并绘制烟花,所以你需要先安装 Pygame 库。你可以使用 pip 命令来安装 Pygame:
```
pip install pygame
```
下面是完整的代码:
```python
import random
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口的大小和标题
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("跨年烟花")
# 定义颜色
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
# 生成随机的烟花
def create_firework():
x = random.randint(0, 600)
y = 400
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
return (x, y, color)
# 绘制烟花
def draw_firework(firework):
x, y, color = firework
pygame.draw.circle(screen, color, (x, y), 5)
# 更新烟花的位置
def update_firework(firework):
x, y, color = firework
return (x, y - 5, color)
# 生成随机数量的烟花
fireworks = [create_firework() for _ in range(random.randint(10, 20))]
# 主循环
running = True
while running:
# 检查事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 清空屏幕
screen.fill((0, 0, 0))
# 更新烟花的位置并绘制
new_fireworks = []
for firework in fireworks:
firework = update_firework(firework)
if firework[1] >= 0:
new_fireworks.append(firework)
draw_firework(firework)
fireworks = new_fireworks
# 显示窗口
pygame.display.flip()
### 回答2:
import time
from turtle import *
# 设置窗口大小和背景颜色
setup(800, 600)
bgcolor("black")
# 隐藏画笔
hideturtle()
# 定义一个烟花绘制函数
def draw_firework():
# 设置画笔颜色和形状
color("red")
shape("circle")
shapesize(0.5)
# 绘制烟花
for _ in range(36):
forward(100)
left(170)
# 清除屏幕上的烟花
clear()
# 绘制跨年倒计时
for i in range(10, 0, -1):
penup()
goto(0, 0)
pendown()
color("white")
write("倒计时:" + str(i), align="center", font=("Arial", 36, "bold"))
time.sleep(1)
clear()
# 绘制跨年烟花
for _ in range(5):
x = randint(-350, 350)
y = randint(-250, 250)
penup()
goto(x, y)
pendown()
draw_firework()
# 显示跨年祝福
penup()
goto(0, 0)
pendown()
color("white")
write("新年快乐!", align="center", font=("Arial", 48, "bold"))
# 等待几秒钟后关闭窗口
time.sleep(5)
bye()
### 回答3:
以下是一个使用Python编写的完整跨年烟花代码:
```
import random
import time
from tkinter import Canvas, Tk
# 创建窗口
root = Tk()
root.title('跨年烟花')
canvas = Canvas(root, width=800, height=600, bg='black')
canvas.pack()
# 烟花的颜色
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'white']
# 定义烟花类
class Firework:
def __init__(self):
self.x = random.randint(100, 700)
self.y = 600
self.color = random.choice(colors)
self.exploded = False
def launch(self):
while self.y >= 50:
canvas.create_oval(self.x - 3, self.y - 3, self.x + 3, self.y + 3, fill=self.color)
canvas.update()
time.sleep(0.03)
canvas.create_oval(self.x - 3, self.y - 3, self.x + 3, self.y + 3, fill='black')
self.y -= 10
self.explode()
def explode(self):
self.exploded = True
num_particles = random.randint(50, 100)
particles = []
for _ in range(num_particles):
dx = random.uniform(-5, 5)
dy = random.uniform(-15, -5)
particles.append([self.x, self.y, dx, dy, random.randint(3, 6)])
while particles:
for particle in particles:
x, y, dx, dy, life = particle
canvas.create_oval(x - 1, y - 1, x + 1, y + 1, fill=self.color)
particle[0] += dx
particle[1] += dy
particle[4] -= 0.1
if particle[4] <= 0:
particles.remove(particle)
canvas.update()
time.sleep(0.03)
# 创建烟花
fireworks = []
num_fireworks = 5
for _ in range(num_fireworks):
firework = Firework()
fireworks.append(firework)
# 启动烟花
for firework in fireworks:
firework.launch()
# 进入主循环
root.mainloop()
```
这个代码使用`tkinter`库创建了一个窗口,然后定义了一个`Firework`类表示烟花。每个烟花的位置和颜色是随机生成的。在`launch`方法中,烟花从底部往上升直到达到一定高度,然后调用`explode`方法触发爆炸效果。在`explode`方法中,烟花将分裂为多个粒子,每个粒子会以随机的速度和路径向四周飞散,并在一段时间后消失。
主程序中创建了多个烟花对象,并依次启动每个烟花。整个程序在窗口中展示了跨年夜烟花的效果。
阅读全文