pygame多种⽅式实现屏保操作(⾃动切换、⿏标切换、
键盘切换)
⽬录
⼀、图⽚处理
(⼀)图⽚采集
(⼆)图⽚装载
(三)完整代码
⼆、初始化pygame相关参数
(⼀)设置初始化参数
(⼆)设置为全屏显⽰:
三、核⼼模块
(⼀)⾃动切换
(⼆)键盘切换
(三)⿏标切换
(四)退出操作
(五)设置透明⾊
治疗的英文(六)完整的核⼼代码:
四、其他相关函数:
五、运⾏效果:
六、完整的代码
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 = ansform.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 = ansform.scale(pygame.image.load(file), (1440, 900)) dSurface = picture
# dSurface = pygame.image.load(file).convert()
bglist.append(dSurface)
合肥市公积金中心
⼆、初始化pygame相关参数
(⼀)设置初始化参数
pygame.init() # 初始化pygame类
# 设置窗⼝⼤⼩
pygame.display.t_caption('美丽的屏保') # 设置窗⼝标题
tick = pygame.time.Clock()
fps = 10 # 设置刷新率,数字越⼤刷新率越⾼
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
(⼆)设置为全屏显⽰:
screen = pygame.display.t_mode((1440, 900),flags=pygame.FULLSCREEN) 三、核⼼模块
(⼀)⾃动切换
def run():
flag = 0
runimage = random.choice(bglist)
while True:
for event in ():
pe == pygame.QUIT:
screen.fill((255, 255, 255)) # 设置背景为⽩⾊
教师职业道德基本内容
screen.vert(), (0, 0))
if flag % 100 == 1:
runimage = random.choice(bglist)
flag += 1
fcclock.tick(fps)
pygame.display.flip() # 刷新窗⼝
(⼆)键盘切换
for event in ():
pe == pygame.KEYDOWN:
梁丰生态园if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_SPACE:
ret()
(三)⿏标切换
for event in ():
pe == pygame.MOUSEBUTTONDOWN:
ret()
(四)退出操作
for event in ():
pe == pygame.QUIT pe == pygame.K_F1:
pygame.quit()
(五)设置透明⾊
runimage.t_alpha(255-flag*2)
(六)完整的核⼼代码:
封装成函数:
def run():
global flag,runimage
runimage = random.choice(bglist)
while True:
for event in ():
pe == pygame.QUIT pe == pygame.K_F1:
pygame.quit()
pe == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_SPACE:
ret()
pe == pygame.MOUSEBUTTONDOWN:
ret()
screen.fill((255, 255, 255)) # 设置背景为⽩⾊
screen.blit(runimage, (0, 0))
if flag % 100 == 1:
ret()
flag += 3
runimage.t_alpha(255-(flag*2) % 250)
fcclock.tick(fps)
pygame.display.flip() # 刷新窗⼝
四、其他相关函数:
(⼀)ret函数
def ret():
global flag,runimage
flag = 0
水果塔
runimage = random.choice(bglist)
(⼆)main函数
if __name__ == '__main__':
init_image()
run()
五、运⾏效果:
六、完整的代码
import sys, pygame
import os
import random
import time
pygame.init() # 初始化pygame类
screen = pygame.display.t_mode((1440, 900),flags=pygame.FULLSCREEN) # 设置窗⼝⼤⼩pygame.display.t_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 = ansform.scale(pygame.image.load(file), (1440, 900))
dSurface = picture
# dSurface = pygame.image.load(file).convert()
bglist.append(dSurface)
def ret():
global flag,runimage
flag = 0
runimage = random.choice(bglist)
def run():
global flag,runimage
runimage = random.choice(bglist)
while True:
for event in ():
pe == pygame.QUIT pe == pygame.K_F1:
pygame.quit()
pe == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_SPACE:
ret()
pe == pygame.MOUSEBUTTONDOWN:
ret()
screen.fill((255, 255, 255)) # 设置背景为⽩⾊
screen.blit(runimage, (0, 0))
if flag % 100 == 1:
ret()
胃气不足
flag += 3
runimage.t_alpha(255-(flag*2) % 250)
fcclock.tick(fps)
pygame.display.flip() # 刷新窗⼝
# time.sleep(10)
if __name__ == '__main__':
init_image()
run()
到此这篇关于pygame多种⽅式实现屏保操作(⾃动切换、⿏标切换、键盘切换)的⽂章就介绍到这了,更多相关pygame 屏保操作内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!