制作自已的屏保动态
创始人
2024-04-26 18:55:37
0

制作自已的屏保动态

  • 我的环境
  • 第一步:编写自己的屏保程序
    • 1、代码准备
    • 2、编译
  • 第二步:有了可运行程序,使用RAR压缩工具将资源和程序打包成独立可执行exe
  • 第三步:将dist.exe配置成系统屏幕保护
  • 参考

我的环境

win10
python3.X
pycharm

第一步:编写自己的屏保程序

注意:屏保程序打开就是全屏,可自动循环播放

我的样子如图
Alt

1、代码准备

Gitee下载

import os# 必须在加载 加之前
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" % (0, 30)import random
import pygame
from pygame.locals import *
from math import pi, sin, cospygame.init()
# 获取显示器大小
screen_width, screen_height = pygame.display.get_desktop_sizes()[0]ICON = "./icon.png"
TITLE = "见到你时我的心"
WIDTH = 800
HEIGHT = 800
main_loops = True
# 心形中心点
center_x = screen_width / 2
center_y = screen_height / 2
#
screen = pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN)
pygame.display.set_caption(TITLE)
pygame.mouse.set_visible(False)
try:pygame.display.set_icon(pygame.image.load(ICON))
except:passbottomlefttip_h = "[f:全屏/窗口][s:闪烁][t:跳动][+/-:频率][esc:退出]: "
bottomlefttip = bottomlefttip_h
bottomrighttip_h = "[鼠标位置]: "
bottomrighttip = bottomrighttip_h
HOT_PINK = (255,105,180)class Particle():def __init__(self, pos, size, f):# (left, top, width, height)self.pos = pos.copy()self.pos0 = pos.copy()self.size = sizeself.f = fdef draw(self, center_x, center_y):"""Rect((left, top), (width, height)) -> Rect:return:"""pygame.draw.rect(screen, HOT_PINK,pygame.Rect((self.size * self.f * self.pos[0] + center_x, -self.size * self.f * self.pos[1] + center_y),(self.pos[2], self.pos[3])),0)def update(self, t):# 全部一个呼吸系数# df = 1 + (2 - 1.5 ) * sin(t * 3) / 8# df = 1 +  (heartbeatmplitude )*sin(t * 3) / 8# 外内,内快,参数外小内大df = 1 + (2 - 1.5 * self.f) * sin(t * 3) / 8self.pos[0] = self.pos0[0] * dfself.pos[1] = self.pos0[1] * dfclass MouseParticle():def __init__(self, pos):# (left, top, width, height)self.pos = pos.copy()self.particles = []self.xiaoshishudu = .8self.xiaoshishuduxishu = 1.2self.show = .5no_p = 50# dt 离散点数dt = 2 * pi / no_pt = 0while t <= 2 * pi:# 正向随机分面l = mu - abs(random.gauss(mu, sigma) - mu)# 双向分布# l=random.gauss(mu, sigma)# l=1,表示画一个线# l=1xleft = l * 16 * sin(t) ** 3ytop = l * (13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))t += dtself.particles.append(Particle([xleft, ytop, static_wh[0], static_wh[1]], 1, l))def draw(self):"""Rect((left, top), (width, height)) -> Rect:return:"""if not self.show:returnif self.xiaoshishudu < 0.000005:self.show = 0for p in self.particles:p.draw(self.pos[0], self.pos[1])self.update()def update(self):self.xiaoshishudu = self.xiaoshishudu ** self.xiaoshishuduxishufor p in self.particles:p.update(self.xiaoshishudu)def jiashudu(self):if self.xiaoshishuduxishu < 3:self.xiaoshishuduxishu += .1def jianshudu(self):if self.xiaoshishuduxishu > 1.1:self.xiaoshishuduxishu -= .1mouseParticleList = []particles = []"""
若随机变量X服从一个数学期望为μ、方差为σ^2的正态分布,记为N(μ,σ^2)
期望值μ决定了其位置,其标准差σ决定了分布的幅度。当μ = 0,σ = 1时的正态分布是标准正态分布心形公式x=16*sin(t)**3y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
"""
# 均值,心形的大小
mu = 1.1
# 是标准差,辐射范围
sigma = .15# 静态心形点的大小
static_wh = (1.5, 1.5)
# 动态心形点大小,
dynamic_wh = (1, 2)
# 心跳幅度
heartbeatmplitude = 1.2# 心形大小
size = 15# 外部开关
waiweikaiguan = True
# 跳动开关
tiaodongkaiguan = True
# 窗口,全屏
fullscreenkaiguan = False
# 跳动频率
jumpfreq=30no_p = 10000
# dt 离散点数
dt = 2 * pi / no_p
t = 0#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 3万个点def init_dynamic_particles():# dt 离散点数global t# 初始化跳动心形点while t <= 2 * pi:# 正向随机分面l = mu - abs(random.gauss(mu, sigma) - mu)# 双向分布# l=random.gauss(mu, sigma)# l=1,表示画一个线# l=1xleft = l * 16 * sin(t) ** 3ytop = l * (13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))t += dtparticles.append(Particle([xleft, ytop, static_wh[0], static_wh[1]], size, l))# def draw():
#     screen.clear()
#     for i in range(len(x)):
#         screen.draw.filled_rect(Rect((x[i]*10+center_x, -y[i]*10+center_y), (4, 4)), 'pink')def show_ynamic_particles():for p in particles:p.draw(center_x, center_y)def show_static_particles():# 3万个点# no_p = 20000# dt 离散点数t = 0while waiweikaiguan and t < 2 * pi:f = random.gauss(mu, sigma * 2)x = 16 * sin(t) ** 3y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)# uniform成下一个实数,它在 [x,y] 范围内pygame.draw.rect(screen, HOT_PINK,Rect((17 * f * x + center_x, -17 * f * y + center_y), (random.uniform(.5, 3), random.uniform(.5, 3))),0)# screen.draw.filled_rect(#     Rect((17 * f * x + center_x, -17 * f * y + center_y), (random.uniform(.5, 3), random.uniform(.5, 3))),#     'hot pink')t += dt * 2def show_mouse_particles():global mouseParticleListt = []for p in mouseParticleList:if p.show:t.append(p)p.draw()else:breakmouseParticleList = tdef add_mouse_particles(pos):global mouseParticleListmouseParticleList = [MouseParticle(pos)] + mouseParticleListdef draw_text(sc, str, position, pos:tuple , color, background="black", fontsize=24, name=None):text = pygame.font.SysFont(name, fontsize).render(str, True, color, background)textRect = text.get_rect()if position.startswith("c"):textRect.center = poselif position.startswith("m"):passelif position.startswith("bottomleft"):textRect.bottomleft=poselif position.startswith("bottomright"):textRect.bottomright=poselif position.startswith("topleft"):textRect.topleft=poselif position.startswith("topright"):textRect.topright=poselse:try:raise AttributeError("position")  # 假装这里有异常,一般针对难以复现的异常except:print("""postion# bottomleft=(100, 100)# topleft=(100, 100)# topright=(100, 100)# bottomright=(100, 100)## midtop=(100, 100)# midleft=(100, 100)# midbottom=(100, 100)# midright=(100, 100)# center=(100, 100)""")sc.blit(text, textRect)# bottomleft=(100, 100)# topleft=(100, 100)# topright=(100, 100)# bottomright=(100, 100)## midtop=(100, 100)# midleft=(100, 100)# midbottom=(100, 100)# midright=(100, 100)# center=(100, 100)# centerx# centerydef draw():# 清空全部内容screen.fill("black")draw_text(screen, "心动", "center", (center_x, center_y), HOT_PINK, "black", 24, "SimSun")draw_text(screen, bottomlefttip, "bottomleft", (0, center_y * 2), HOT_PINK, "black", 12, "SimSun")draw_text(screen, bottomrighttip, "bottomright", (center_x * 2, center_y * 2), HOT_PINK, "black", 12, "SimSun")# 显示动态心形show_ynamic_particles()"""初始化外部心形情况"""show_static_particles()# 显示鼠标show_mouse_particles()"""screen.draw.text("ccccccccc\nbbbbbbbbb", center=(100, 100), color='hot pink', background="black", fontsize=24)screen.draw.text("1", bottomleft=(100, 100), color=(200, 200, 200), background="black")screen.draw.text("2", topleft=(100, 100), color=(200, 200, 200), background="black")screen.draw.text("3", topright=(100, 100), color=(200, 200, 200), background="black")screen.draw.text("4", bottomright=(100, 100), color=(200, 200, 200), background="black")screen.draw.text("5", midtop=(100, 100), color=(200, 200, 200), background="black")screen.draw.text("6", midleft=(100, 100), color=(200, 200, 200), background="black")screen.draw.text("7", midbottom=(100, 100), color=(200, 200, 200), background="black")screen.draw.text("8", midright=(100, 100), color=(200, 200, 200), background="black")"""#刷新一下画面,将画的东西显示到画面上pygame.display.update()def update(dt):# dt 1/fps 两帧之间的时间间隔 单位是秒global tt += dtif tiaodongkaiguan:for p in particles:p.update(t)# 加载背景音乐
def musicloops(path):pygame.mixer.init()try:pygame.mixer.music.load(path)pygame.mixer.music.play(-1)except:passdef on_mouse_down(pos, button):# print(pos, button)global bottomrighttipbottomrighttip = bottomrighttip_h + str(pos) + str(button)def on_mouse_up(pos, button):passdef on_mouse_move(pos, rel, buttons):# print(pos, rel, buttons)global bottomrighttipbottomrighttip = bottomrighttip_h + str(pos)# 更新状态add_mouse_particles([pos[0], pos[1]])def on_key_down(key):global screenglobal bottomlefttip, fullscreenkaiguan, waiweikaiguan, tiaodongkaiguanbottomlefttip = bottomlefttip_h + pygame.key.name(key)global center_x, center_yglobal jumpfreqif key == K_f:if fullscreenkaiguan:# screen =pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)# 发现从pygame.FULLSCREEN,到pygame.RESIZABLE调用一次不起作用pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)center_x = WIDTH / 2center_y = HEIGHT / 2passpygame.mouse.set_visible(True )else:# pygame.display.set_mode((screen_width, screen_height), pygame.NOFRAME)# screen =pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN)pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN)center_x = screen_width / 2center_y = screen_height / 2pygame.mouse.set_visible(False)# , pygame.NOFRAMEfullscreenkaiguan = not fullscreenkaiguanbottomlefttip += " 全屏"+str(fullscreenkaiguan)elif key == K_ESCAPE:global main_loopsmain_loops=Falseelif key == K_SPACE:passelif key == K_s:waiweikaiguan = not waiweikaiguanbottomlefttip += " 闪烁"+str(waiweikaiguan)elif key == K_t:tiaodongkaiguan = not tiaodongkaiguanbottomlefttip += " 跳动"+str(tiaodongkaiguan)elif key == K_KP_PLUS or key == K_PLUS:if jumpfreq>5:jumpfreq-=5bottomlefttip += " 频率=" + str(jumpfreq)elif key == K_KP_MINUS or key == K_MINUS:if jumpfreq<60:jumpfreq+=5bottomlefttip += " 频率=" + str(jumpfreq)elif key == K_MENU:passelse:bottomlefttip += " 无动作 "# pgzrun.go()def event():global center_x, center_yfor event in pygame.event.get():# if event.type not in [KEYDOWN, MOUSEMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP]:#     print(event)if event.type == QUIT:global main_loopsmain_loops = Falseelif event.type == KEYDOWN:# 键盘被按下 unicode 、key 、modon_key_down(event.key)# https://blog.csdn.net/qq_41556318/article/details/86304649# http://t.zoukankan.com/liquancai-p-13235734.htmlelif event.type == MOUSEMOTION:# MOUSEMOTION 鼠标移动  pos 、rel 、buttons# on_mouse_move(event.pos, event.rel, event.buttons)elif event.type == MOUSEBUTTONDOWN:# MOUSEBUTTONDOWN 鼠标被按下pos 、button# on_mouse_down(event.pos, event.button)elif event.type == MOUSEBUTTONUP:# MOUSEBUTTONUP鼠标被放开pos 、buttonon_mouse_up(event.pos, event.button)elif event.type == VIDEORESIZE:center_x = event.w / 2center_y = event.h / 2elif event.type == WINDOWMAXIMIZED:# 窗口最大化print(event)elif event.type == WINDOWMINIMIZED:# 窗口最大化print(event)pygame.mixer.music.pause()elif event.type == WINDOWRESTORED:# 重新显示pygame.mixer.music.unpause()elif event.type == WINDOWSHOWN:print(event)elif event.type == ACTIVEEVENT:# print(pygame.mixer.music.get_busy())# try:#     if event.gain and not pygame.mixer.music.get_busy():#         #显示内容#         pygame.mixer.music.pause()#     elif not event.gain and pygame.mixer.music.get_busy():#         pygame.mixer.music.pause()# except:#     passpassif __name__ == '__main__':musicloops("bfa.mp3")# 初始化动态心形点,只执行一次init_dynamic_particles()# Run the game loop.while main_loops:event()update(1/jumpfreq)draw()pygame.quit()# pyinstaller -F -c -w -i favicon.ico --clean xx-pygame.py
# cxfreeze xg.py --target-dir x --base-name=win32gui

2、编译

1)新建一个虚拟环境安装pygame,pyinstaller两个库
Alt
2)使用pyinstaller打包
说明一下,pyinstaller打包,会加载环境里的全部内容,所以需要单独新建环境,这样在dist生成的exe文件会比较小。
pyinstaller 参数和使用说明:https://zhuanlan.zhihu.com/p/470301078
不用看.spec文件格式,用不到。
Alt
3)生成结果
说明:运行程序是没有窗口图标和声音的,需要dist中放一个bfa.mp3.这个是在465行。可以修改成自己的内容。
Alt

第二步:有了可运行程序,使用RAR压缩工具将资源和程序打包成独立可执行exe

1)将声音,图标,python打包生成的exe 打成一个rar
Alt
2)打开 dist.rar ,工具拦选择“自解压格式”

Alt

Alt
Alt

Alt
Alt
完成以上配置后选确定,两次。这时在目录下会生成 dist.exe。这时可以运行查下一下效果。
Alt

第三步:将dist.exe配置成系统屏幕保护

1)将dist.exe 修改成 dist.scr 复制到 C:\Windows目录下双击运行
2)回到电脑桌面,鼠标右击,选择个性化打开如下图:
Alt

参考

最早是看到这个文章:https://new.qq.com/rain/a/20221121A07JGJ00
pyzero: https://pygame-zero.readthedocs.io/zh_CN/latest/
pygame:https://www.pygame.org/docs/
Pygame详解(七):key 模块:https://blog.csdn.net/qq_41556318/article/details/86304649
pygame 的键盘和鼠标事件的处理:http://t.zoukankan.com/liquancai-p-13235734.html
rar生成exe当时看的不是这个,引一下:https://blog.csdn.net/weixin_42436161/article/details/123114690

相关内容

热门资讯

安卓系统通知管理,全面解析与优... 你有没有发现,手机里的通知就像是一群调皮的小精灵,时不时地跳出来和你互动?没错,说的就是安卓系统的通...
安卓系统手机哪买,揭秘哪里购买... 你有没有想过,拥有一部安卓系统手机是多么酷的事情呢?想象你可以自由安装各种应用,不受限制地探索各种功...
安卓系统 ipv4,基于安卓系... 你知道吗?在智能手机的世界里,有一个系统可是无人不知、无人不晓,那就是安卓系统。而在这个庞大的安卓家...
目前安卓是什么系统,探索安卓系... 亲爱的读者,你是否曾好奇过,如今安卓系统究竟是什么模样?在这个科技飞速发展的时代,操作系统如同人体的...
安卓6.0系统比5.0,从5.... 你有没有发现,自从手机更新了安卓6.0系统,感觉整个人都清爽了不少呢?没错,今天咱们就来聊聊这个话题...
安卓2.36系统升级,功能革新... 你知道吗?最近安卓系统又来了一次大变身,那就是安卓2.36系统升级!这可不是一个小打小闹的更新,而是...
安卓系统源码怎么打开,并可能需... 你有没有想过,安卓系统的源码就像是一扇神秘的门,隐藏着无数的技术秘密?想要打开这扇门,你得掌握一些小...
安卓8.0系统体验视频,智能革... 你有没有听说安卓8.0系统最近可是火得一塌糊涂啊!作为一个紧跟科技潮流的数码达人,我当然要来给你好好...
宣传系统漫画app安卓,探索安... 亲爱的读者们,你是否曾在某个午后,百无聊赖地打开手机,想要寻找一些轻松愉悦的读物?今天,我要给你介绍...
鸿蒙替换安卓系统吗,开启智能生... 你知道吗?最近科技圈里可是炸开了锅,因为华为的新操作系统鸿蒙系统,据说要大举进军手机市场,替换掉安卓...
手机安卓系统深度清理,解锁手机... 手机里的东西是不是越来越多,感觉就像一个装满了杂物的储物柜?别急,今天就来教你一招——手机安卓系统深...
安卓上的windows系统,融... 你有没有想过,在安卓手机上也能体验到Windows系统的魅力呢?没错,这就是今天我要跟你分享的神奇故...
安卓系统焦点变化事件,Andr... 你知道吗?在安卓系统的世界里,最近发生了一件超级有趣的事情——焦点变化事件。这可不是什么小打小闹,它...
一加系统安卓降级,轻松还原经典... 你有没有想过,你的手机系统升级后,突然发现某些功能变得不那么顺心了?别急,今天就来聊聊一加系统安卓降...
日本最好的安卓系统,体验非凡 亲爱的读者们,你是否曾想过,在遥远的东方,有一个国家,他们的智能手机系统独具特色,让人眼前一亮?没错...
荣耀安卓11 系统证书,保障安... 你知道吗?最近手机圈里可是炸开了锅,荣耀安卓11系统证书成了大家热议的话题。这不,我就迫不及待地来和...
安卓手机开机升级系统,体验流畅... 你有没有发现,每次你的安卓手机开机,总会有那么一刹那,屏幕上跳出一个升级系统的提示?是不是觉得这就像...
真正的安卓系统手机,安卓系统手... 你有没有想过,为什么有些人对安卓系统手机情有独钟?是不是觉得市面上的安卓手机千篇一律,缺乏个性?别急...
安卓怎么用定位系统,轻松实现精... 你有没有想过,手机里的定位系统竟然这么神奇?它不仅能帮你找到回家的路,还能在茫茫人海中找到你的好友。...
安卓的哪个系统流畅,探索新一代... 你有没有想过,为什么你的安卓手机有时候像蜗牛一样慢吞吞的,而别人的手机却像风一样快?今天,就让我带你...