pygame多种方式实现屏保操作(自动切换、鼠标切换、键盘切换),欢迎往下观看。
我认为有三种方法:
其一:使用爬虫技术从网上下载图片,可以开一个子线程负责采集网上图片,然后加载到list列表中;
其二:可以直接对电脑中所有的盘进行自动检索,然后加载到list列表中;
其三:指定目录,然后加载到list列表中;
我这里偷个懒,选择第三种方法实现。具体代码如下:
path = './image/'
files = []
dirs = os.listdir(path)
for diretion in dirs:
files.append(path + diretion)
我为什么在初始化的时候就进行装载呢?
原因是:解决效率问题,无需每次使用时重复加载,而且在初始化的时候就适配屏幕大小进行图片缩放。
因此,我把这个过程打包成一个函数,方便后续调用,而且参数传递为:屏幕的大小。然后返回bglist对象。
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 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.init() # 初始化pygame类
# 设置窗口大小
pygame.display.set_caption('美丽的屏保') # 设置窗口标题
tick = pygame.time.Clock()
fps = 10 # 设置刷新率,数字越大刷新率越高
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
screen = pygame.display.set_mode((1440, 900),flags=pygame.FULLSCREEN)
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() # 刷新窗口
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()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
reset()
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
runimage.set_alpha(255-flag*2)
封装成函数:
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() # 刷新窗口
def reset():
global flag,runimage
flag = 0
runimage = random.choice(bglist)
if __name__ == '__main__':
init_image()
run()
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()
好吧,写完了,希望对你有帮助,请持续关注。
感谢你的支持,比心。