当前位置: 首页 > news >正文

嫦娥奔月庆中秋~Python语言实现

图片路径:D:/MyProject/Python/Alvin/change.jpg

在这里插入图片描述

实现效果

在这里插入图片描述

在这里插入图片描述

代码实现

import pygame
import sys
import math
import random
import os
from pygame import mixer# 初始化pygame
pygame.init()# 检查图片文件是否存在
image_path = "D:/MyProject/Python/Alvin/change.jpg"
if not os.path.exists(image_path):print(f"错误: 找不到图片文件 {image_path}")print("请确保图片路径正确,或者将图片放在程序同目录下并修改代码中的路径")sys.exit()# 窗口设置
WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("中秋快乐 - 嫦娥奔月")# 尝试加载背景音乐
try:mixer.init()# 这里可以添加背景音乐文件,例如:# mixer.music.load("midautumn_music.mp3")# mixer.music.play(-1)  # -1表示循环播放
except:print("无法加载音频系统,将继续无音乐运行")# 颜色定义
BLACK = (0, 0, 0)
DARK_BLUE = (10, 10, 40)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
GOLD = (255, 215, 0)
RED = (255, 50, 50)
PINK = (255, 182, 193)
LIGHT_BLUE = (173, 216, 230)
COLORS = [RED, (255, 165, 0), (50, 205, 50), (138, 43, 226), (255, 192, 203),(0, 191, 255), (255, 105, 180), (50, 205, 50), (255, 69, 0)]# 加载嫦娥图片
try:change_img = pygame.image.load(image_path)# 调整图片大小change_img = pygame.transform.scale(change_img, (120, 160))
except pygame.error as e:print(f"无法加载图片: {e}")print("将使用默认图形代替")change_img = None# 烟花粒子类
class Particle:def __init__(self, x, y, color):self.x = xself.y = yself.color = colorself.radius = random.randint(1, 3)self.speed = random.uniform(2, 6)self.angle = random.uniform(0, 2 * math.pi)self.vx = math.cos(self.angle) * self.speedself.vy = math.sin(self.angle) * self.speedself.gravity = 0.1self.life = 100  # 粒子寿命self.trail = []  # 粒子轨迹def update(self):# 保存轨迹点if len(self.trail) > 5:self.trail.pop(0)self.trail.append((self.x, self.y))self.x += self.vxself.y += self.vyself.vy += self.gravityself.life -= 1def draw(self, surface):alpha = min(255, int(255 * (self.life / 100)))color = (self.color[0], self.color[1], self.color[2])# 绘制轨迹for i, (trail_x, trail_y) in enumerate(self.trail):trail_alpha = alpha * (i / len(self.trail))pygame.draw.circle(surface, color, (int(trail_x), int(trail_y)),max(1, int(self.radius * (i / len(self.trail)))))# 绘制粒子pygame.draw.circle(surface, color, (int(self.x), int(self.y)), self.radius)# 添加光晕效果glow_surf = pygame.Surface((self.radius * 4, self.radius * 4), pygame.SRCALPHA)pygame.draw.circle(glow_surf, (*color, alpha // 3),(self.radius * 2, self.radius * 2), self.radius * 2)surface.blit(glow_surf, (int(self.x - self.radius * 2), int(self.y - self.radius * 2)))def is_dead(self):return self.life <= 0# 绘制月亮
def draw_moon():# 主月亮pygame.draw.circle(screen, YELLOW, (moon_x, moon_y), moon_radius)# 月亮上的环形山pygame.draw.circle(screen, GOLD, (moon_x - 20, moon_y - 20), 15)pygame.draw.circle(screen, GOLD, (moon_x + 30, moon_y + 10), 10)pygame.draw.circle(screen, GOLD, (moon_x + 10, moon_y + 30), 8)# 月亮光晕for i in range(3):radius = moon_radius + 10 + i * 5alpha = 100 - i * 30s = pygame.Surface((radius * 2, radius * 2), pygame.SRCALPHA)pygame.draw.circle(s, (255, 255, 200, alpha), (radius, radius), radius)screen.blit(s, (moon_x - radius, moon_y - radius))# 绘制星星背景
def draw_stars():for _ in range(3):  # 每帧添加几颗新星星x = random.randint(0, WIDTH)y = random.randint(0, HEIGHT // 2)size = random.uniform(0.5, 2)brightness = random.randint(150, 255)twinkle = random.random() > 0.7  # 30%的星星会闪烁if twinkle:size *= 1.5  # 闪烁的星星更大pygame.draw.circle(screen, (brightness, brightness, brightness), (x, y), size)# 绘制嫦娥(使用图片或默认图形)
def draw_change(x, y, progress):if change_img:# 使用图片# 根据飞行进度旋转图片,模拟飞行姿态rotation = math.sin(progress * 0.1) * 5rotated_img = pygame.transform.rotate(change_img, rotation)img_rect = rotated_img.get_rect(center=(int(x), int(y)))screen.blit(rotated_img, img_rect)else:# 使用默认图形size = 30 + progress * 20brightness = 100 + progress * 155color = (brightness, brightness, 255)# 身体pygame.draw.circle(screen, color, (int(x), int(y)), int(size / 2))# 飘带pygame.draw.arc(screen, (255, 100, 200),(x - size, y - size, size * 2, size * 2),math.pi / 4, math.pi / 2, 3)# 飞行轨迹星星for i in range(3):star_x = x - 50 - i * 20 + random.randint(-5, 5)star_y = y + random.randint(-10, 10)pygame.draw.circle(screen, WHITE, (int(star_x), int(star_y)), 2)# 显示祝福文字
def show_blessing(progress):font_large = pygame.font.SysFont('simhei', 60)font_small = pygame.font.SysFont('simhei', 36)font_poem = pygame.font.SysFont('simhei', 24)# 渐入效果alpha = min(255, int(progress * 255))# 主标题text1 = font_large.render("中秋快乐", True, GOLD)text1.set_alpha(alpha)screen.blit(text1, (WIDTH // 2 - text1.get_width() // 2, HEIGHT - 200))# 副标题text2 = font_small.render("嫦娥奔月庆团圆", True, WHITE)text2.set_alpha(alpha)screen.blit(text2, (WIDTH // 2 - text2.get_width() // 2, HEIGHT - 130))# 祝福语text3 = font_small.render("花好月圆人长久", True, WHITE)text3.set_alpha(alpha)screen.blit(text3, (WIDTH // 2 - text3.get_width() // 2, HEIGHT - 80))# 诗句if progress > 0.5:poem_alpha = min(255, int((progress - 0.5) * 2 * 255))poem1 = font_poem.render("但愿人长久,千里共婵娟", True, LIGHT_BLUE)poem1.set_alpha(poem_alpha)screen.blit(poem1, (WIDTH // 2 - poem1.get_width() // 2, 50))poem2 = font_poem.render("——苏轼《水调歌头》", True, LIGHT_BLUE)poem2.set_alpha(poem_alpha)screen.blit(poem2, (WIDTH // 2 - poem2.get_width() // 2, 80))# 创建烟花爆炸
def create_firework(x, y):color = random.choice(COLORS)particles = []for _ in range(100):particles.append(Particle(x, y, color))return particles# 创建上升的烟花
def create_rising_firework():x = random.randint(100, WIDTH - 100)y = HEIGHTcolor = random.choice(COLORS)# 上升粒子rising_particle = Particle(x, y, color)rising_particle.vy = -random.uniform(5, 8)  # 向上速度rising_particle.vx = random.uniform(-0.5, 0.5)  # 轻微水平移动rising_particle.gravity = 0.05rising_particle.life = 80  # 较短寿命return rising_particle# 主程序
def main():global moon_x, moon_y, moon_radius# 月亮位置和大小moon_x = WIDTH - 150moon_y = 100moon_radius = 80# 嫦娥初始位置change_x = -100change_y = HEIGHT - 100change_speed = 5# 烟花列表fireworks = []rising_fireworks = []# 动画状态change_arrived = Falseanimation_progress = 0celebration_started = False# 星星列表(固定一些星星)stars = []for _ in range(100):stars.append((random.randint(0, WIDTH), random.randint(0, HEIGHT // 2),random.uniform(0.5, 2), random.randint(100, 255)))# 主循环clock = pygame.time.Clock()running = Truewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:running = Falseelif event.key == pygame.K_SPACE:# 空格键触发额外烟花x = random.randint(100, WIDTH - 100)y = random.randint(100, HEIGHT - 200)fireworks.append(create_firework(x, y))# 清屏screen.fill(DARK_BLUE)# 绘制固定星星for x, y, size, brightness in stars:pygame.draw.circle(screen, (brightness, brightness, brightness), (x, y), size)# 绘制动态星星draw_stars()# 绘制月亮draw_moon()# 嫦娥奔月动画if not change_arrived:# 抛物线轨迹change_x += change_speedt = change_x / WIDTH# 抛物线公式:y = 起始y - 上升高度 + 下落高度change_y = HEIGHT - 100 - (t * 1.5) * (HEIGHT - 200) + 0.5 * (t * 1.5) * (HEIGHT - 200)animation_progress = tif change_x > moon_x - 50:change_arrived = Truecelebration_started = Truecelebration_start_time = pygame.time.get_ticks()# 绘制嫦娥draw_change(change_x, change_y, animation_progress)# 生成上升的烟花(仅在庆祝阶段)if celebration_started and random.random() < 0.05:rising_fireworks.append(create_rising_firework())# 更新上升的烟花for rising_particle in rising_fireworks[:]:rising_particle.update()# 绘制上升轨迹pygame.draw.circle(screen, rising_particle.color,(int(rising_particle.x), int(rising_particle.y)), 2)# 如果上升粒子到达一定高度或生命结束,爆炸成烟花if rising_particle.y < 200 or rising_particle.is_dead():fireworks.append(create_firework(rising_particle.x, rising_particle.y))rising_fireworks.remove(rising_particle)# 庆祝阶段 - 生成更多烟花if celebration_started:# 在嫦娥周围生成烟花if random.random() < 0.2:offset_x = random.randint(-50, 50)offset_y = random.randint(-50, 50)fireworks.append(create_firework(change_x + offset_x, change_y + offset_y))# 在屏幕随机位置生成烟花if random.random() < 0.1:x = random.randint(100, WIDTH - 100)y = random.randint(100, HEIGHT - 200)fireworks.append(create_firework(x, y))# 更新和绘制烟花for particles in fireworks[:]:for particle in particles[:]:particle.update()particle.draw(screen)if particle.is_dead():particles.remove(particle)if not particles:fireworks.remove(particles)# 显示祝福文字(在嫦娥到达后渐入)if change_arrived:time_since_celebration = pygame.time.get_ticks() - celebration_start_timetext_progress = min(1.0, time_since_celebration / 3000)  # 3秒内完成渐入show_blessing(text_progress)# 显示操作提示if time_since_celebration > 5000:  # 5秒后显示提示font = pygame.font.SysFont('simhei', 20)hint = font.render("按空格键发射更多烟花", True, LIGHT_BLUE)screen.blit(hint, (WIDTH // 2 - hint.get_width() // 2, HEIGHT - 30))pygame.display.flip()clock.tick(60)pygame.quit()sys.exit()if __name__ == "__main__":main()
http://www.dtcms.com/a/450546.html

相关文章:

  • 内蒙古赤峰市建设局网站青海企业网站开发定制
  • 算法-试填法
  • 云建站步骤国内人做韩国网站一般都卖什么东西
  • 营销软件网站七星彩网站开发公司
  • 网站如何做问卷调查微信公众号微网站制作
  • 陕西煤业化工建设集团有限公司网站微网站开发怎么写
  • 建设网站需要什么样的服务器广州网站开发招聘信息
  • 营销型网站建设ppt模板下载百度在全国有哪些代理商
  • 网站建设 招聘国外有哪些网站做推广的比较好
  • UNIX下C语言编程与实践35-UNIX 守护进程编写:后台执行、脱离终端、清除掩码与信号处理
  • 漳州做网站优化全网有哪些网站可以做淘客
  • 晋江网站建设山东省建设局拖欠工资网站
  • 命令行创建https证书详解
  • Linux 基础命令的 7 大核心模块
  • 沐川移动网站建设设计网站私单价格
  • 杭州网站建设 博客南京做网站品牌
  • 大连专业模板网站制作福州网站提升排名
  • AI-RAN 开发者套件,使用指南
  • 网站自动加水印四川哪家网站推广做的好
  • 泸友科技网站购物网站图片素材
  • 汕头建站模板厂家十堰网站建设u2028
  • C语言进阶知识--字符和字符串函数
  • C++中指针传递与引用传递的区别
  • 云南建设局网站首页网页浏览器主要通过ftp协议
  • 网站建设先航科技贵阳制作网站的公司
  • 做网站建设有哪些公司好耒阳住房与建设局网站
  • Helm入门
  • SpringBoot项目搭建
  • 网站导航菜单兰品牌建设助力高质量发展
  • 小游戏网站网址今天的头条新闻