发布时间:2025-06-24 19:06:38  作者:北方职教升学中心  阅读量:242


height = 800, 600scren = pygame.display.set_mode((width, height))pygame.display.set_caption("新年快乐,但也可以先用。
 。新年快乐!

import pygameimport randomimport math# Pygamepygame初始化.init()# 设置窗口大小width,")# WHITE的颜色定义 = (255, 255, 255)BLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)YELLOW = (255, 255, 0)# 文字设置font__path = "C:/Windows/Fonts/simhei.ttf"  # 替换你的中文字体路径font = pygame.font.Font(font_path, 74)text = font.render("新年快乐,

运行效果如下:#xff0c;也可以定制自己想要祝福的人,虽然做得不太好,", True, WHITE)text_rect = text.get_rect(center=(width // 2, height // 2))# 烟花粒子类classs Firework: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.particles = [] self.exploded = False def add_particle(self): angle = random.uniform(0, 2 * math.pi) speed = random.uniform(5, 10) particle = { 'x': self.x, 'y': self.y, 'vx': speed * math.cos(angle), 'vy': -speed * math.sin(angle), 'life': 50 } self.particles.append(particle) def update(self): if not self.exploded: self.y -= 5 if self.y < height // 2: self.exploded = True for _ in range(100): self.add_particle() else: for particle in self.particles: particle['x'] += particle['vx'] particle['y'] += particle['vy'] particle['vy'] += 0.1 # 加速度 particle['life'] -= 1 self.particles = [p for p in self.particles if p['life'] > 0] def draw(self, screen): if not self.exploded: pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3) else: for particle in self.particles: pygame.draw.circle(screen, self.color, (int(particle['x']), int(particle['y'])), 2)# 创建烟花列表fireworks = []# running主循环 = Trueshow_text = Truetext_duration = 3000 # 显示文本的时间(毫秒)start_time = pygame.time.get_ticks()while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False current_time = pygame.time.get_ticks() elapsed_time = current_time - start_time if show_text and elapsed_time >= text_duration: show_text = False screen.fill(BLACK) if show_text: screen.blit(text, text_rect) else: if random.randint(1, 20) == 1: fireworks.append(Firework(random.randint(100, width - 100), height, random.choice([RED, GREEN, BLUE, YELLOW]))) for firework in fireworks: firework.update() firework.draw(screen) fireworks = [fw for fw in fireworks if not (fw.exploded and len(fw.particles) == 0)] pygame.display.flip() pygame.time.delay(30)pygame.quit()。天总!