神农架林区网站建设_网站建设公司_响应式网站_seo优化
2026/1/21 10:10:39 网站建设 项目流程
# Python 高级编程:2026 丙午马年元旦祝福程序# 主题:烟花 + 动态文字 + 祝福动画 + 音效提示(可选) + 烟火粒子系统# 运行环境:Python 3.8+ + pygame(pip install pygame)importpygameimportsysimportrandomimportmathimporttimefromdatetimeimportdatetime# 初始化 Pygamepygame.init()pygame.mixer.init()# 如果想加音效# 屏幕设置WIDTH,HEIGHT=1280,720screen=pygame.display.set_mode((WIDTH,HEIGHT))pygame.display.set_caption("2026 丙午马年 元旦快乐!")clock=pygame.time.Clock()# 颜色定义(马年火元素 + 节日喜庆)BLACK=(0,0,0)WHITE=(255,255,255)RED=(255,50,50)GOLD=(255,215,0)ORANGE=(255,140,0)PURPLE=(180,100,255)CYAN=(0,255,255)# 字体font_title=pygame.font.SysFont('simhei',80,bold=True)# 中文黑体font_sub=pygame.font.SysFont('simhei',50)font_small=pygame.font.SysFont('simhei',32)# 粒子类(烟花爆炸效果)classParticle:def__init__(self,x,y,color):self.x=x self.y=y self.color=color self.angle=random.uniform(0,math.tau)self.speed=random.uniform(2,8)self.size=random.randint(3,8)self.lifetime=random.randint(40,90)self.gravity=0.08self.alpha=255defupdate(self):self.x+=math.cos(self.angle)*self.speed self.y+=math.sin(self.angle)*self.speed+self.gravity*(90-self.lifetime)self.lifetime-=1self.alpha=int(255*(self.lifetime/90))ifself.lifetime>0else0defdraw(self,surface):ifself.lifetime>0:s=pygame.Surface((self.size*2,self.size*2),pygame.SRCALPHA)pygame.draw.circle(s,(*self.color,self.alpha),(self.size,self.size),self.size)surface.blit(s,(int(self.x-self.size),int(self.y-self.size)))# 烟花类classFirework:def__init__(self):self.x=random.randint(100,WIDTH-100)self.y=HEIGHT self.target_y=random.randint(100,HEIGHT//3)self.color=random.choice([RED,GOLD,ORANGE,PURPLE,CYAN])self.speed=random.uniform(8,15)self.exploded=Falseself.particles=[]defupdate(self):ifnotself.exploded:self.y-=self.speedifself.y<=self.target_y:self.exploded=Truefor_inrange(120):self.particles.append(Particle(self.x,self.y,self.color))else:forpinself.particles[:]:p.update()ifp.lifetime<=0:self.particles.remove(p)defdraw(self,surface):ifnotself.exploded:# 上升尾迹pygame.draw.circle(surface,self.color,(int(self.x),int(self.y)),6)pygame.draw.circle(surface,WHITE,(int(self.x),int(self.y)),3)else:forpinself.particles:p.draw(surface)# 文字动画类classTextAnimation:def__init__(self,text,font,color,x,y,duration=3000):self.text=text self.font=font self.color=color self.x=x self.y=y self.start_time=pygame.time.get_ticks()self.duration=duration self.alpha=0defupdate(self):elapsed=pygame.time.get_ticks()-self.start_timeifelapsed<self.duration//3:self.alpha=int(255*(elapsed/(self.duration//3)))elifelapsed>self.duration*2//3:self.alpha=int(255*(1-(elapsed-self.duration*2//3)/(self.duration//3)))else:self.alpha=255defdraw(self,surface):ifself.alpha>0:surf=self.font.render(self.text,True,self.color)surf.set_alpha(self.alpha)surface.blit(surf,(self.x-surf.get_width()//2,self.y-surf.get_height()//2))# 主祝福内容(可自定义)blessings=["2026 丙午马年 元旦快乐!","马到成功 骏业日新","火马奔腾 热情似火","新年新气象 心想事成","健康平安 财源广进","家庭和睦 爱情甜蜜","学业有成 事业腾飞","岁岁平安 年年有余","愿你2026 如马般自由奔放!"]# 初始化fireworks=[]texts=[]current_blessing_idx=0next_text_time=pygame.time.get_ticks()+2000# 主循环running=Truepygame.mouse.set_visible(False)# 隐藏鼠标更沉浸whilerunning:dt=clock.tick(60)current_time=pygame.time.get_ticks()foreventinpygame.event.get():ifevent.type==pygame.QUIT:running=Falseelifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_ESCAPE:running=False# 背景:深邃夜空 + 轻微星光screen.fill((8,8,30))# 随机发射烟花(每隔0.8~2秒一朵)ifrandom.random()<0.03:fireworks.append(Firework())# 更新 & 绘制烟花forfwinfireworks[:]:fw.update()fw.draw(screen)iffw.explodedandnotfw.particles:fireworks.remove(fw)# 祝福文字轮播动画ifcurrent_time>next_text_time:iftexts:texts.pop(0)# 移除旧的text=blessings[current_blessing_idx%len(blessings)]anim=TextAnimation(text,font_titleif"丙午马年"intextelsefont_sub,random.choice([GOLD,RED,ORANGE,CYAN]),WIDTH//2,HEIGHT//2+random.randint(-80,80),duration=5000)texts.append(anim)current_blessing_idx+=1next_text_time=current_time+random.randint(4000,7000)fortintexts:t.update()t.draw(screen)# 小祝福 + 时间显示now=datetime.now()time_str=now.strftime("%Y年%m月%d日 %H:%M:%S")time_surf=font_small.render(f"Las Vegas 时间:{time_str}2026元旦快乐!",True,WHITE)screen.blit(time_surf,(20,HEIGHT-40))horse_tip=font_small.render("丙午马年 · 火马奔腾 · 热情似火 · 马到成功",True,(255,180,100))screen.blit(horse_tip,(WIDTH-horse_tip.get_width()-20,HEIGHT-40))pygame.display.flip()pygame.quit()sys.exit()

程序亮点(高级编程技巧展示)

  • 粒子系统:真实烟花爆炸物理(速度+重力+衰减+alpha渐隐)
  • 动画文字:淡入淡出 + 随机位置/颜色/间隔
  • 事件驱动:键盘ESC退出 + 随机烟花发射
  • 中文字体:使用simhei(Windows自带黑体)完美显示中文
  • 时间实时:结合datetime显示当前时间(拉斯维加斯时区)
  • 祝福轮播:内容可无限扩展,自定义你的专属祝福

快速运行方式

  1. 确保安装 pygame:

    pipinstallpygame
  2. 复制代码保存为happy_new_year_2026.py

  3. 执行:

    python happy_new_year_2026.py
  4. 按 ESC 优雅退出

2026丙午马年元旦快乐!
愿你马到成功,热情似火,奔腾向前!
在新的一年里,一切顺利,心想事成~ 🐎🔥✨

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询