过年了,让GPT用Python给你写个放烟花的程序吧!

简介: 过年了,让GPT用Python给你写个放烟花的程序吧!

过年了,祝大家新年快乐~ 今天不学习,来放松一下 ~

有多少地方是禁放烟花的?

用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

好了,本文到这里,祝大家新年快乐~

欢迎点赞 + 关注🙏!

相关文章
|
7天前
|
Linux iOS开发 MacOS
【chat-gpt问答记录】python虚拟环境venv的简介及使用
【chat-gpt问答记录】python虚拟环境venv的简介及使用
18 2
|
7天前
|
存储 JSON JavaScript
【chat-gpt问答记录】python将数据存为json格式和yaml格式
【chat-gpt问答记录】python将数据存为json格式和yaml格式
23 1
|
22天前
|
数据采集 JSON 数据库
800个程序实例、5万行代码!清华大学出版【Python王者归来】
Python 的丰富模块(module)以及广泛的应用范围,使 Python 成为当下最重要的计算机语言之一,本书尝试将所有常用模块与应用分门别类组织起来,相信只要读者遵循本书实例,定可以轻松学会 Python 语法与应用,逐步向 Python 高手之路迈进,这也是撰写本书的目的。 本书以约 800 个程序实例讲解了:完整的 Python 语法,Python 的输入与输出,Python 的数据型态,列表(list)、元组(tuple)、字典(dict)、集合(set),函数设计,类别设计,使用系统与外部模块(module),设计自己的模块(module),文件压缩与解压缩,程序除错与异常处理…
|
7天前
|
存储 算法 数据库
Python 抽奖程序限定次数详解
构建Python抽奖程序,限定用户抽奖次数,使用字典存储用户ID及抽奖次数。`LotterySystem`类包含判断、记录和抽奖方法。当用户达到最大抽奖次数(默认3次)时,禁止继续。示例展示如何创建系统,模拟用户抽奖,并扩展功能如动态调整次数和多用户、多奖品池。性能优化可通过数据持久化和并发控制实现。
11 0
|
13天前
|
Python Windows
Python基础教程(第3版)中文版 第18章 程序打包 (笔记)
Python基础教程(第3版)中文版 第18章 程序打包 (笔记)
|
25天前
|
存储 区块链 Python
怎么把Python脚本打包成可执行程序?
最近根据用户提的需求用python做了一个小工具,但是在给客户使用的时候不能直接发送python文件,毕竟让客户去安装python环境,那就离了大谱了。所以这时候就需要把多个py文件带着运行环境打包成EXE可执行文件。
怎么把Python脚本打包成可执行程序?
|
11天前
|
Python 索引
【Python字符串攻略】:玩转文字,编织程序的叙事艺术
【Python字符串攻略】:玩转文字,编织程序的叙事艺术
|
11天前
|
Python 存储 数据处理
【Python数据类型的奥秘】:构建程序基石,驾驭信息之海
【Python数据类型的奥秘】:构建程序基石,驾驭信息之海
|
12天前
|
存储 区块链 Python
怎么把Python脚本打包成可执行程序?
【6月更文挑战第3天】最近根据用户提的需求用python做了一个小工具,但是在给客户使用的时候不能直接发送python文件,毕竟让客户去安装python环境,那就离了大谱了。所以这时候就需要把多个py文件带着运行环境打包成EXE可执行文件。
13 1
|
12天前
|
搜索推荐 区块链 开发者
【python程序打包教程】PyInstaller一键打包Python程序为独立可执行exe文件
【python程序打包教程】PyInstaller一键打包Python程序为独立可执行exe文件