pygame多种方式实现屏保操作(自动切换、鼠标切换、键盘切换),欢迎往下观看。
一、图片处理
(一)图片采集
我认为有三种方法:
其一:使用爬虫技术从网上下载图片,可以开一个子线程负责采集网上图片,然后加载到list列表中;
其二:可以直接对电脑中所有的盘进行自动检索,然后加载到list列表中;
其三:指定目录,然后加载到list列表中;
我这里偷个懒,选择第三种方法实现。具体代码如下:
1
2
3
4
5
|
path = './image/'
files = []
dirs = os.listdir(path)
for diretion in dirs:
files.append(path + diretion)
|
(二)图片装载
我为什么在初始化的时候就进行装载呢?
原因是:解决效率问题,无需每次使用时重复加载,而且在初始化的时候就适配屏幕大小进行图片缩放。
因此,我把这个过程打包成一个函数,方便后续调用,而且参数传递为:屏幕的大小。然后返回bglist对象。
1
2
3
4
5
|
for file in files:
picture = pygame.transform.scale(pygame.image.load( file ), ( 1440 , 900 ))
dSurface = picture
# dSurface = pygame.image.load(file).convert()
bglist.append(dSurface)
|
(三)完整代码
封装成函数:
1
2
3
4
5
6
7
8
9
10
11
12
|
def init_image():
path = './image/'
files = []
dirs = os.listdir(path)
for diretion in dirs:
files.append(path + diretion)
for file in files:
picture = pygame.transform.scale(pygame.image.load( file ), ( 1440 , 900 ))
dSurface = picture
# dSurface = pygame.image.load(file).convert()
bglist.append(dSurface)
|
二、初始化pygame相关参数
(一)设置初始化参数
1
2
3
4
5
6
7
8
9
|
pygame.init() # 初始化pygame类
# 设置窗口大小
pygame.display.set_caption( '美丽的屏保' ) # 设置窗口标题
tick = pygame.time.Clock()
fps = 10 # 设置刷新率,数字越大刷新率越高
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
|
(二)设置为全屏显示:
1
|
screen = pygame.display.set_mode(( 1440 , 900 ),flags = pygame.FULLSCREEN)
|
三、核心模块
(一)自动切换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
def run():
flag = 0
runimage = random.choice(bglist)
while True :
for event in pygame.event.get():
if event. type = = pygame.QUIT:
sys.exit()
screen.fill(( 255 , 255 , 255 )) # 设置背景为白色
screen.blit(runimage.convert(), ( 0 , 0 ))
if flag % 100 = = 1 :
runimage = random.choice(bglist)
flag + = 1
fcclock.tick(fps)
pygame.display.flip() # 刷新窗口
|
(二)键盘切换
1
2
3
4
5
6
7
|
for event in pygame.event.get():
if event. type = = pygame.KEYDOWN:
if event.key = = pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key = = pygame.K_SPACE:
reset()
|
(三)鼠标切换
1
2
3
|
for event in pygame.event.get():
if event. type = = pygame.MOUSEBUTTONDOWN:
reset()
|
(四)退出操作
1
2
3
4
|
for event in pygame.event.get():
if event. type = = pygame.QUIT or event. type = = pygame.K_F1:
pygame.quit()
sys.exit()
|
(五)设置透明色
1
|
runimage.set_alpha( 255 - flag * 2 )
|
(六)完整的核心代码:
封装成函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
def run():
global flag,runimage
runimage = random.choice(bglist)
while True :
for event in pygame.event.get():
if event. type = = pygame.QUIT or event. type = = pygame.K_F1:
pygame.quit()
sys.exit()
if event. type = = pygame.KEYDOWN:
if event.key = = pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key = = pygame.K_SPACE:
reset()
if event. type = = pygame.MOUSEBUTTONDOWN:
reset()
screen.fill(( 255 , 255 , 255 )) # 设置背景为白色
screen.blit(runimage, ( 0 , 0 ))
if flag % 100 = = 1 :
reset()
flag + = 3
runimage.set_alpha( 255 - (flag * 2 ) % 250 )
fcclock.tick(fps)
pygame.display.flip() # 刷新窗口
|
四、其他相关函数:
(一)reset函数
1
2
3
4
|
def reset():
global flag,runimage
flag = 0
runimage = random.choice(bglist)
|
(二)main函数
1
2
3
|
if __name__ = = '__main__' :
init_image()
run()
|
五、运行效果:
六、完整的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import sys, pygame
import os
import random
import time
pygame.init() # 初始化pygame类
screen = pygame.display.set_mode(( 1440 , 900 ),flags = pygame.FULLSCREEN) # 设置窗口大小
pygame.display.set_caption( '美丽的屏保' ) # 设置窗口标题
tick = pygame.time.Clock()
fps = 10 # 设置刷新率,数字越大刷新率越高
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
def init_image():
path = './image/'
files = []
dirs = os.listdir(path)
for diretion in dirs:
files.append(path + diretion)
for file in files:
picture = pygame.transform.scale(pygame.image.load( file ), ( 1440 , 900 ))
dSurface = picture
# dSurface = pygame.image.load(file).convert()
bglist.append(dSurface)
def reset():
global flag,runimage
flag = 0
runimage = random.choice(bglist)
def run():
global flag,runimage
runimage = random.choice(bglist)
while True :
for event in pygame.event.get():
if event. type = = pygame.QUIT or event. type = = pygame.K_F1:
pygame.quit()
sys.exit()
if event. type = = pygame.KEYDOWN:
if event.key = = pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key = = pygame.K_SPACE:
reset()
if event. type = = pygame.MOUSEBUTTONDOWN:
reset()
screen.fill(( 255 , 255 , 255 )) # 设置背景为白色
screen.blit(runimage, ( 0 , 0 ))
if flag % 100 = = 1 :
reset()
flag + = 3
runimage.set_alpha( 255 - (flag * 2 ) % 250 )
fcclock.tick(fps)
pygame.display.flip() # 刷新窗口
# time.sleep(10)
if __name__ = = '__main__' :
init_image()
run()
|
到此这篇关于pygame多种方式实现屏保操作(自动切换、鼠标切换、键盘切换)的文章就介绍到这了,更多相关pygame 屏保操作 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/dhjabc_1/article/details/116207115