python实现绘制烟花代码

在Python中,我们可以使用多个库来绘制烟花效果,例如turtle库用于简单的绘图,或者更复杂的库如pygamematplotlib结合动画。但是,由于turtle库是Python自带的,我们可以使用它来绘制一个简单的烟花效果。

下面是一个使用turtle库绘制简单烟花效果的示例代码:

import turtle  
import random  
  
# 设置画布和画笔  
screen = turtle.Screen()  
screen.bgcolor("black")  # 设置背景色为黑色  
  
firework = turtle.Turtle()  
firework.speed(0)  # 最快速度  
firework.hideturtle()  # 隐藏画笔  
  
colors = ["red", "orange", "yellow", "green", "blue", "purple", "white"]  
  
def draw_firework(x, y):  
    firework.penup()  
    firework.goto(x, y)  
    firework.pendown()  
      
    for _ in range(20):  # 绘制20条线模拟烟花效果  
        color = random.choice(colors)  
        firework.color(color)  
        for _ in range(random.randint(40, 100)):  # 控制线条长度  
            firework.forward(1)  
            firework.backward(1)  
        firework.right(random.randint(1, 360))  # 随机转向  
  
# 在屏幕上随机位置绘制多个烟花  
for _ in range(50):  # 绘制50个烟花  
    x = random.randint(-300, 300)  
    y = random.randint(-200, 200)  
    draw_firework(x, y)  
  
# 结束绘制,保持窗口不关闭  
turtle.done()

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是绘制烟花Python Pygame 代码: ```python import pygame import random # 初始化 Pygame pygame.init() # 设置窗口大小 screen = pygame.display.set_mode((800, 600)) # 设置窗口标题 pygame.display.set_caption("烟花") # 定义颜色 BLACK = (, , ) WHITE = (255, 255, 255) RED = (255, , ) GREEN = (, 255, ) BLUE = (, , 255) # 定义烟花类 class Firework: def __init__(self, x, y): self.x = x self.y = y self.color = (random.randint(, 255), random.randint(, 255), random.randint(, 255)) self.radius = 1 self.max_radius = random.randint(50, 100) self.exploded = False self.particles = [] def update(self): if not self.exploded: self.radius += 1 if self.radius >= self.max_radius: self.explode() else: for particle in self.particles: particle.update() self.particles = [particle for particle in self.particles if not particle.dead] def draw(self, screen): if not self.exploded: pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius) else: for particle in self.particles: particle.draw(screen) def explode(self): self.exploded = True for i in range(100): self.particles.append(Particle(self.x, self.y, self.color)) # 定义烟花粒子类 class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = 2 self.angle = random.uniform(, 2 * math.pi) self.speed = random.uniform(1, 5) self.dead = False self.life = random.randint(10, 50) def update(self): self.life -= 1 if self.life <= : self.dead = True self.x += self.speed * math.cos(self.angle) self.y += self.speed * math.sin(self.angle) self.radius -= .05 if self.radius <= : self.dead = True def draw(self, screen): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), int(self.radius)) # 创建烟花列表 fireworks = [] # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: fireworks.append(Firework(*event.pos)) # 更新烟花 for firework in fireworks: firework.update() # 绘制背景 screen.fill(BLACK) # 绘制烟花 for firework in fireworks: firework.draw(screen) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() ``` 希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孺子牛 for world

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值