用python做一个动态烟花特效,该界面中上方打开txt文件,获取里面内容并水平滚动显示内容
时间: 2024-03-22 11:40:07 浏览: 65
用python实现烟花效果
5星 · 资源好评率100%
好的,下面是一个简单的Python程序实现动态烟花特效以及水平滚动显示文本内容的界面。
```python
import tkinter as tk
# 创建主窗口
root = tk.Tk()
root.title("烟花特效")
# 设置窗口大小
canvas_width = 800
canvas_height = 600
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()
# 设置文本滚动区域
text = tk.Text(root, height=5)
text.pack(side=tk.TOP)
# 打开txt文件并获取内容
with open("text.txt", "r") as f:
content = f.read()
# 将文本内容插入滚动区域
text.insert(tk.END, content)
# 定义文本滚动类
class TextScroll:
def __init__(self, text_widget):
self.text_widget = text_widget
self.text_widget.config(state=tk.DISABLED)
self.text_widget.bind("<Configure>", self.on_configure)
self.offset = 0
self.width = self.text_widget.winfo_width()
self.canvas_id = canvas.create_text(canvas_width, 10, text=content, anchor=tk.NW, fill="white")
self.scroll()
# 滚动文本
def scroll(self):
self.offset -= 2
canvas.coords(self.canvas_id, self.offset, 10)
if self.offset <= -self.width:
self.offset = canvas_width
root.after(20, self.scroll)
# 监听文本框尺寸变化
def on_configure(self, event):
self.width = event.width
# 创建文本滚动对象
text_scroll = TextScroll(text)
# 定义烟花类
class Firework:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.init_speed = random.uniform(10, 20)
self.speed = self.init_speed
self.radius = 3
self.is_exploded = False
self.particles = []
# 更新烟花状态
def update(self):
if not self.is_exploded:
self.y -= self.speed
self.speed -= 0.5
if self.speed <= 0:
self.explode()
else:
for p in self.particles:
p.update()
if all([p.is_dead for p in self.particles]):
self.is_dead = True
# 绘制烟花
def draw(self):
if not self.is_exploded:
canvas.create_oval(self.x - self.radius, self.y - self.radius,
self.x + self.radius, self.y + self.radius, fill=self.color)
else:
for p in self.particles:
p.draw()
# 爆炸
def explode(self):
self.is_exploded = True
for i in range(30):
angle = random.uniform(0, 2 * 3.14)
speed = random.uniform(5, 15)
color = '#{:06x}'.format(random.randint(0, 0xFFFFFF))
p = Particle(self.x, self.y, angle, speed, color)
self.particles.append(p)
# 定义烟花粒子类
class Particle:
def __init__(self, x, y, angle, speed, color):
self.x = x
self.y = y
self.color = color
self.speed_x = speed * math.cos(angle)
self.speed_y = speed * math.sin(angle)
self.radius = 2
self.is_dead = False
# 更新粒子状态
def update(self):
self.x += self.speed_x
self.y += self.speed_y
self.speed_y += 0.1
if self.y >= canvas_height:
self.is_dead = True
# 绘制粒子
def draw(self):
canvas.create_oval(self.x - self.radius, self.y - self.radius,
self.x + self.radius, self.y + self.radius, fill=self.color)
# 创建烟花列表
fireworks = []
# 定义主循环函数
def main_loop():
# 更新烟花状态
for f in fireworks:
f.update()
# 绘制烟花和粒子
canvas.delete(tk.ALL)
for f in fireworks:
f.draw()
# 创建新的烟花
if random.random() < 0.03:
x = random.randint(50, canvas_width - 50)
y = canvas_height
color = '#{:06x}'.format(random.randint(0, 0xFFFFFF))
f = Firework(x, y, color)
fireworks.append(f)
# 延迟50毫秒
root.after(50, main_loop)
# 运行主循环函数
main_loop()
# 进入主循环
root.mainloop()
```
在运行程序之前,需要在程序所在目录下创建一个名为 `text.txt` 的文件,并在其中输入要显示的文本内容。
该程序通过创建文本滚动类来实现水平滚动显示文本内容,并通过创建烟花和粒子类来实现动态烟花特效。在主循环函数中,每隔50毫秒更新一次烟花状态并绘制烟花和粒子,同时以一定的概率创建新的烟花。
阅读全文