"""
这次把上次编写的代码又稍微改进了一下,做成了一个自我介绍的多媒体视频。音效效果可是相当的振撼哟,坚定信念_Python无敌。
亮点,部分采用了“中文”编程!还有就是每次启动窗口一定在屏幕中央,并且没有边框。
"""
import pygame
import os
import winsound
from pygame.locals import *
class Dream():
info =[ '李兴球简介',
'生于1976年9月',
'祖籍江西萍乡福田镇双源村',
'高中时代开始学习编程',
'2010年开始致力于儿童编程的普及',
'曾在北京,杭州某教育集团做课程研发',
'原创近千例编程案例,著有多部Python书籍',
'有感于一线城市Python教师,亦是严重匮乏',
'放弃一线城市高薪工作,回到老家萍乡',
'致力于Python,人工智能语言的普及',
'新的文化,叫编程! ',
'新的旅程,已开始',
'谢谢大家,本视频用Python编程制作。',
'坚定信念,Python无敌',
'做学问,我选Python,你呢?'
]
tips = []
tips_index = 0
tips_amounts = len(info)
def __init__(self,size):
"""
size:屏幕大小
"""
self.width = size[0]
self.height = size[1]
self.screen = pygame.display.set_mode(size,NOFRAME,0)
self.clock = pygame.time.Clock()
self.end = False
self.title("做学问,我选Python,你呢?")
self.myfont = pygame.font.Font("msyh.ttf",42)
for string in Dream.info:
tip = self.myfont.render(string,True,(0,255,255))
tip_width = tip.get_width()
tip_render_pos = (self.width//2 - tip_width//2 ),200
Dream.tips.append([tip,tip_render_pos])
def title(self,title):
pygame.display.set_caption(title)
def playmusic(self,wavfile,loop = True):
"""播放背景音乐,loop为非零值表示重复播放,其它为播放一次"""
try:
if loop :
winsound.PlaySound(wavfile,winsound.SND_ASYNC|winsound.SND_LOOP) # 循环异步播放
else:
winsound.PlaySound(wavfile,winsound.SND_ASYNC) # 异步播放一次
except:
pass
def 成真(self):
self.end = True
try:
pygame.quit()
except:
pass
def fadeshow(self,imagefile,speed=1,time=3):
"""淡入淡出显示图像,参数说明:
imagefile:图像文件
speed:速度,1-255的值
time:显示时间,秒为单位
"""
if not os.path.exists(imagefile):return
extname = os.path.splitext(imagefile)[-1]
extname = extname.lower()[1:]
if not extname in ('jpg','jpeg','png','gif','bmp','pcx','tga','tif','lbm','pbm','pgm','ppm','xpm'):return
speed = abs(speed)
time = int(time)
picture = pygame.image.load(imagefile)
self.tip = Dream.tips[Dream.tips_index][0]
self.tip_render_pos = Dream.tips[Dream.tips_index][1]
Dream.tips_index = Dream.tips_index + 1
Dream.tips_index = Dream.tips_index % Dream.tips_amounts
"淡入的代码段,通过设置透明度实现"
for i in range(0,256,speed): # 从0到255变化,0 是全透明,255是不透明.
self.clock.tick(60)
event = pygame.event.poll() # 加这个不会有无响应
if event.type == QUIT:self.成真();return
picture.set_alpha(i) # 设置透明度
self.screen.fill((0,0,0)) # 渲染screen为黑色
self.screen.blit(picture,(0,0) ) # 在screen上画picture
self.screen.blit(self.tip,self.tip_render_pos)
pygame.display.flip() # 更新显示
"这个for循环是等待一定的时间"
for i in range(60 * time): # 由于fps是60,即1/60秒显示一幅画,所以重复的次数要乘以60
self.clock.tick(60)
event = pygame.event.poll()
if event.type == QUIT:self.成真();return
self.screen.fill((0,0,0))
self.screen.blit(picture,(0,0) )
self.screen.blit(self.tip,self.tip_render_pos)
pygame.display.flip()
"淡出的代码段"
for i in range(255,-1,-speed): # 从255到0
self.clock.tick(60) # 设置fps为60
event = pygame.event.poll() # 获取一个事件
if event.type == QUIT:self.成真();return # 如果是退出事件,就退出
picture.set_alpha(i) # 设置图像透明度
self.screen.fill((0,0,0))
self.screen.blit(picture,(0,0) ) # 把picture画在screen上
self.screen.blit(self.tip,self.tip_render_pos)
pygame.display.flip() # 更新显示
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
-
- 2026 年 3 月
- 2026 年 2 月
- 2026 年 1 月
- 2025 年 12 月
- 2025 年 11 月
- 2025 年 10 月
- 2025 年 9 月
- 2025 年 6 月
- 2025 年 5 月
- 2025 年 3 月
- 2025 年 2 月
- 2025 年 1 月
- 2024 年 12 月
- 2024 年 8 月
- 2024 年 6 月
- 2024 年 5 月
- 2024 年 4 月
- 2024 年 3 月
- 2024 年 2 月
- 2023 年 11 月
- 2023 年 9 月
- 2023 年 6 月
- 2023 年 5 月
- 2023 年 4 月
- 2023 年 3 月
- 2023 年 2 月
- 2023 年 1 月
- 2022 年 12 月
- 2022 年 11 月
- 2022 年 10 月
- 2022 年 9 月
- 2022 年 8 月
- 2022 年 7 月
- 2022 年 6 月
- 2022 年 5 月
- 2022 年 4 月
- 2022 年 3 月
- 2022 年 2 月
- 2022 年 1 月
- 2021 年 12 月
- 2021 年 11 月
- 2021 年 10 月
- 2021 年 9 月
- 2021 年 8 月
- 2021 年 7 月
- 2021 年 6 月
- 2021 年 5 月
- 2021 年 4 月
- 2021 年 3 月
- 2021 年 2 月
- 2021 年 1 月
- 2020 年 12 月
- 2020 年 11 月
- 2020 年 10 月
- 2020 年 9 月
- 2020 年 8 月
- 2020 年 7 月
- 2020 年 6 月
- 2020 年 5 月
- 2020 年 4 月
- 2020 年 3 月
- 2020 年 2 月
- 2020 年 1 月
- 2019 年 12 月
- 2019 年 11 月
- 2019 年 10 月
- 2019 年 9 月
- 2019 年 8 月
- 2019 年 7 月
- 2019 年 6 月
- 2019 年 5 月
- 2019 年 4 月
- 2019 年 3 月
- 2019 年 2 月
- 2018 年 3 月
- 2018 年 1 月
- 2017 年 9 月
- 2017 年 5 月
- 2017 年 1 月
