您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关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 = Nonescreen = 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() # 刷新窗口
(一)reset函数
def reset(): 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.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如何实现屏保操作”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。