过年了,祝大家新年快乐~ 今天不学习,来放松一下 ~
有多少地方是禁放烟花的?
用Python来写个放烟花的程序娱乐一下吧 ~
不会?没事儿,我也不会。让GPT帮你写!
1. 第一版
- Prompt
请用Python帮我写一个烟花绽放的程序
- ChatGPT回复:
请确保你已经安装了 Pygame 库,如果没有安装,可以使用
pip install pygame
来安装。
import sys import random import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 WIDTH, HEIGHT = 800, 600 WINDOW = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Fireworks Simulation") # 定义颜色 WHITE = (255, 255, 255) YELLOW = (255, 255, 0) ORANGE = (255, 165, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 定义烟花粒子类 class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = 2 self.vx = random.uniform(-1, 1) self.vy = random.uniform(-5, -1) self.gravity = 0.05 def update(self): self.x += self.vx self.y += self.vy self.vy += self.gravity def draw(self): pygame.draw.circle(WINDOW, self.color, (int(self.x), int(self.y)), self.radius) # 主函数 def main(): fireworks = [] clock = pygame.time.Clock() FPS = 60 # 主循环 while True: WINDOW.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 创建新的烟花 if random.random() < 0.03: fireworks.append(Particle(random.randint(0, WIDTH), HEIGHT, random.choice([WHITE, YELLOW, ORANGE, RED, GREEN, BLUE]))) # 更新并绘制烟花粒子 for firework in fireworks: firework.update() firework.draw() # 如果烟花粒子落地,就移除它 if firework.y > HEIGHT: fireworks.remove(firework) pygame.display.flip() clock.tick(FPS) if __name__ == "__main__": main()
- 运行结果
2. 第二版
- Prompt
烟花粒子太小了,而且没有爆炸效果,请修正程序
- ChatGPT回复
import sys import random import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 WIDTH, HEIGHT = 800, 600 WINDOW = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Fireworks Simulation") # 定义颜色 WHITE = (255, 255, 255) YELLOW = (255, 255, 0) ORANGE = (255, 165, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 定义烟花粒子类 class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = random.randint(5, 10) # 随机大小 self.vx = random.uniform(-2, 2) self.vy = random.uniform(-10, -5) self.gravity = 0.2 def update(self): self.x += self.vx self.y += self.vy self.vy += self.gravity def draw(self): pygame.draw.circle(WINDOW, self.color, (int(self.x), int(self.y)), self.radius) # 主函数 def main(): fireworks = [] clock = pygame.time.Clock() FPS = 60 # 主循环 while True: WINDOW.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 创建新的烟花 if random.random() < 0.03: fireworks.append(Particle(random.randint(0, WIDTH), HEIGHT, random.choice([WHITE, YELLOW, ORANGE, RED, GREEN, BLUE]))) # 更新并绘制烟花粒子 for firework in fireworks: firework.update() firework.draw() # 如果烟花粒子落地,就移除它 if firework.y > HEIGHT: fireworks.remove(firework) # 如果烟花粒子上升到一定高度,爆炸成更多粒子 if firework.vy >= 0: num_particles = random.randint(10, 20) for _ in range(num_particles): new_particle = Particle(firework.x, firework.y, firework.color) fireworks.append(new_particle) fireworks.remove(firework) pygame.display.flip() clock.tick(FPS) if __name__ == "__main__": main()
- 运行结果
3. 第三版
- Prompt
烟花从底部上升,爆炸,爆炸产生的粒子不能再爆炸
烟花爆炸高度太低了,调高一点
爆炸产生的子粒子使用随机颜色,随机从圆形和线形中选一个形状。爆炸的子例子范围再大一点
- 效果
4. 最终效果
- Prompt
在上面程序的基础上加入一个背景图片,并在背景图片的下半部分用颜色和艺术字体写"新年快乐!"
- 最终代码
import pygame import sys import random # 初始化 Pygame pygame.init() # 设置屏幕大小 WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Fireworks") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) # 加载背景图片 background = pygame.image.load('D:\GitHub\LEARN_LLM\\beijing.jpg') background = pygame.transform.scale(background, (WIDTH, HEIGHT)) # 加载字体 font = pygame.font.Font('c:\WINDOWS\Fonts\STXINGKA.TTF', 60) # 定义烟花粒子类 class Particle: def __init__(self, x, y, explosion=False): self.x = x self.y = y self.color = self.random_color() self.radius = random.randint(2, 6) # 调整粒子大小范围 if not explosion: self.vx = 0 self.vy = random.uniform(-10, -15) # 调整烟花爆炸的上升速度范围 else: self.vx = random.uniform(-5, 5) # 调整烟花爆炸的速度范围 self.vy = random.uniform(-5, 5) # 调整烟花爆炸的上升速度范围 self.gravity = 0.2 # 调整烟花爆炸的重力影响 self.alpha = 255 self.explosion = explosion def random_color(self): return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255) def update(self): self.x += self.vx self.y += self.vy self.vy += self.gravity self.alpha -= 5 # 调整粒子的消失速度 def draw(self, screen): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) # 主函数 def main(): fireworks = [] clock = pygame.time.Clock() FPS = 60 running = True while running: screen.blit(background, (0, 0)) # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 发射烟花 if random.randint(1, 60) == 1: x = random.randint(0, WIDTH) y = random.randint(HEIGHT // 2, HEIGHT) # 调整烟花爆炸的位置 firework = Particle(x, y) fireworks.append(firework) # 更新和绘制烟花 for firework in fireworks: firework.update() firework.draw(screen) if firework.explosion: if firework.alpha <= 0: fireworks.remove(firework) else: if firework.vy >= 0: explosion_size = random.randint(40, 80) # 调整爆炸粒子的数量范围 for _ in range(explosion_size): particle = Particle(firework.x, firework.y, explosion=True) fireworks.append(particle) fireworks.remove(firework) # 在屏幕下半部分绘制文字 text = font.render("新年快乐!", True, RED) text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 1.5)) screen.blit(text, text_rect) pygame.display.flip() clock.tick(FPS) pygame.quit() sys.exit() if __name__ == "__main__": main()
- 最终效果
最后也不是很好看,凑合玩一下吧,通过这个过程你可以练习下通过Prompt来与大模型进行对话式编程,不断完善自己的代码。
完整的对话记录:https://chat.openai.com/share/0865455b-0b71-4249-a381-207954fbffbc
好了,本文到这里,祝大家新年快乐~
欢迎点赞 + 关注🙏!