Python新春烟花盛宴
    		       		warning:
    		            这篇文章距离上次修改已过440天,其中的内容可能已经有所变动。
    		        
        		                
                在Python中创建新春烟花,你可以使用pygame库来创建一个简单的烟花弹射器,并用pyautogui库来全屏显示烟花动画。以下是一个简单的示例:
首先,确保安装了所需的库:
pip install pygame pyautogui然后,你可以使用以下代码创建烟花:
import pygame
import random
import pyautogui
 
# 初始化pygame
pygame.init()
 
# 全屏设置
screen_width, screen_height = pyautogui.size()
 
# 设置窗口和图像
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.Surface(screen.get_size()).convert()
background_color = (0, 0, 0)
background.fill(background_color)
 
# 定义烟花类
class Firework:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.exploded = False
        self.colors = [(255, 0, 0), (255, 69, 0), (255, 144, 0), (255, 255, 0), (173, 255, 47), (0, 255, 0), (0, 255, 255), (0, 100, 255), (0, 0, 255), (139, 0, 255), (255, 0, 255)]
        self.explosion_speed = 5
        self.explosion_radius = 100
        self.particles = []
        for i in range(random.randint(80, 120)):
            self.particles.append(pygame.math.Vector2(self.x, self.y))
 
    def update(self):
        if not self.exploded:
            self.exploded = True
            for i in range(len(self.particles)):
                direction = random.uniform(0, 2 * 3.14)
                speed = random.uniform(1, 3)
                self.particles[i] = pygame.math.Vector2(self.x + self.explosion_radius * math.cos(direction), self.y + self.explosion_radius * math.sin(direction))
                self.particles[i].sub(screen_width / 2, screen_height / 2)
                self.particles[i].normalize_ip()
                self.particles[i].mul(speed)
        for i in self.particles:
            i.y += self.explosion_speed
 
    def draw(self, screen):
        if not self.exploded:
            pygame.draw.circle(screen, self.colors[random.randint(0, len(self.colors) - 1)], (int(self.x), int(self.y)), 5)
        else:
            for i in self.particles:
                if i.y < screen_height + 10:
                    pygame.draw.circle(screen, self.colors[random.randint(0, len(self.co
评论已关闭