如何使用Python制作自已的动态屏保
导读:本文共8965.5字符,通常情况下阅读需要30分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:接下来,请跟着小编一起来学习吧!我的环境win10python3.Xpycharm1.编写自己的屏保程序注意:屏保程序打开就是全屏,可自动循环播放我的样子如图1、代码准备Gitee下载importos必须在加载加之前os.environ['SDL_VIDEO_WINDOW_POS']="%d,%d"%(0,30)importrandomimportpyg... ...
目录
(为您整理了一些要点),点击可以直达。接下来,请跟着小编一起来学习吧!
我的环境
win10
python3.X
pycharm
1.编写自己的屏保程序
注意:屏保程序打开就是全屏,可自动循环播放
我的样子如图
1、代码准备
Gitee下载
importos必须在加载加之前
os.environ['SDL_VIDEO_WINDOW_POS']="%d,%d"%(0,30)
importrandom
importpygame
frompygame.localsimport*
frommathimportpi,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)classParticle():
definit(self,pos,size,f):(left,top,width,height)
self.pos=pos.copy()
self.pos0=pos.copy()
self.size=size
self.f=fdefdraw(self,center_x,center_y):
"""
Rect((left,top),(width,height))->Rect
:return:
"""pygame.draw.rect(screen,HOT_PINK,
pygame.Rect((self.sizeself.fself.pos[0]+center_x,-self.sizeself.fself.pos[1]+center_y),
(self.pos[2],self.pos[3])),
0)defupdate(self,t):
全部一个呼吸系数
df=1+(2-1.5)sin(t3)/8
df=1+(heartbeatmplitude)sin(t3)/8
外内,内快,参数外小内大
df=1+(2-1.5self.f)sin(t3)/8
self.pos[0]=self.pos0[0]df
self.pos[1]=self.pos0[1]*dfclassMouseParticle():
definit(self,pos):
(left,top,width,height)
self.pos=pos.copy()
self.particles=[]
self.xiaoshishudu=.8
self.xiaoshishuduxishu=1.2self.show=.5
no_p=50dt离散点数
dt=2pi/no_p
t=0
whilet<=2pi:正向随机分面
l=mu-abs(random.gauss(mu,sigma)-mu)
双向分布
l=random.gauss(mu,sigma)
l=1,表示画一个线
l=1
xleft=l16sin(t)*3
ytop=l(13cos(t)-5cos(2t)-2cos(3t)-cos(4t))
t+=dt
self.particles.append(Particle([xleft,ytop,static_wh[0],static_wh[1]],1,l))defdraw(self):
"""
Rect((left,top),(width,height))->Rect
:return:
"""
ifnotself.show:
return
ifself.xiaoshishudu<0.000005:
self.show=0forpinself.particles:
p.draw(self.pos[0],self.pos[1])self.update()
defupdate(self):
self.xiaoshishudu=self.xiaoshishudu**self.xiaoshishuduxishu
forpinself.particles:
p.update(self.xiaoshishudu)defjiashudu(self):
ifself.xiaoshishuduxishu<3:
self.xiaoshishuduxishu+=.1defjianshudu(self):
ifself.xiaoshishuduxishu>1.1:
self.xiaoshishuduxishu-=.1mouseParticleList=[]
particles=[]
"""
若随机变量X服从一个数学期望为μ、方差为σ^2的正态分布,记为N(μ,σ^2)
期望值μ决定了其位置,其标准差σ决定了分布的幅度。当μ=0,σ=1时的正态分布是标准正态分布心形公式
x=16*sin(t)*3
y=13cos(t)-5cos(2t)-2cos(3t)-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=30
no_p=10000
dt离散点数
dt=2*pi/no_p
t=0#
++++++++++++++++++++++++++++++++++++++++++++++++++++++
3万个点
definit_dynamic_particles():
dt离散点数
globalt
初始化跳动心形点
whilet<=2*pi:
正向随机分面
l=mu-abs(random.gauss(mu,sigma)-mu)
双向分布
l=random.gauss(mu,sigma)
l=1,表示画一个线
l=1
xleft=l16sin(t)*3
ytop=l(13cos(t)-5cos(2t)-2cos(3t)-cos(4t))
t+=dt
particles.append(Particle([xleft,ytop,static_wh[0],static_wh[1]],size,l))defdraw():
screen.clear()
foriinrange(len(x)):
screen.draw.filled_rect(Rect((x[i]10+center_x,-y[i]10+center_y),(4,4)),'pink')
defshow_ynamic_particles():
forpinparticles:
p.draw(center_x,center_y)defshow_static_particles():
3万个点
no_p=20000
dt离散点数
t=0
whilewaiweikaiguanandt<2pi:
f=random.gauss(mu,sigma2)
x=16*sin(t)*3
y=13cos(t)-5cos(2t)-2cos(3t)-cos(4*t)uniform成下一个实数,它在[x,y]范围内
pygame.draw.rect(screen,HOT_PINK,
Rect((17fx+center_x,-17fy+center_y),(random.uniform(.5,3),random.uniform(.5,3))),
0
)screen.draw.filled_rect(
Rect((17fx+center_x,-17fy+center_y),(random.uniform(.5,3),random.uniform(.5,3))),
'hotpink')
t+=dt*2
defshow_mouse_particles():
globalmouseParticleList
t=[]
forpinmouseParticleList:
ifp.show:
t.append(p)
p.draw()
else:
break
mouseParticleList=tdefadd_mouse_particles(pos):
globalmouseParticleList
mouseParticleList=[MouseParticle(pos)]+mouseParticleListdefdraw_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()
ifposition.startswith("c"):
textRect.center=pos
elifposition.startswith("m"):
pass
elifposition.startswith("bottomleft"):
textRect.bottomleft=pos
elifposition.startswith("bottomright"):
textRect.bottomright=pos
elifposition.startswith("topleft"):
textRect.topleft=pos
elifposition.startswith("topright"):
textRect.topright=pos
else:
try:
raiseAttributeError("position")#假装这里有异常,一般针对难以复现的异常
except:
print("""postionbottomleft=(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
centery
defdraw():
清空全部内容
screen.fill("black")
draw_text(screen,"心动","center",(center_x,center_y),HOT_PINK,"black",24,"SimSun")
draw_text(screen,bottomlefttip,"bottomleft",(0,center_y2),HOT_PINK,"black",12,"SimSun")
draw_text(screen,bottomrighttip,"bottomright",(center_x2,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='hotpink',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()
defupdate(dt):
dt1/fps两帧之间的时间间隔单位是秒
globalt
t+=dt
iftiaodongkaiguan:
forpinparticles:
p.update(t)加载背景音乐
defmusicloops(path):
pygame.mixer.init()
try:
pygame.mixer.music.load(path)
pygame.mixer.music.play(-1)
except:
passdefon_mouse_down(pos,button):
print(pos,button)
globalbottomrighttip
bottomrighttip=bottomrighttip_h+str(pos)+str(button)defon_mouse_up(pos,button):
passdefon_mouse_move(pos,rel,buttons):
print(pos,rel,buttons)
globalbottomrighttip
bottomrighttip=bottomrighttip_h+str(pos)更新状态
add_mouse_particles([pos[0],pos[1]])
defon_key_down(key):
globalscreen
globalbottomlefttip,fullscreenkaiguan,waiweikaiguan,tiaodongkaiguan
bottomlefttip=bottomlefttip_h+pygame.key.name(key)
globalcenter_x,center_y
globaljumpfreqifkey==K_f:
iffullscreenkaiguan:screen=
pygame.display.set_mode((WIDTH,HEIGHT),pygame.RESIZABLE)
发现从pygame.FULLSCREEN,到pygame.RESIZABLE调用一次不起作用
pygame.display.set_mode((WIDTH,HEIGHT),pygame.RESIZABLE)
center_x=WIDTH/2
center_y=HEIGHT/2
pass
pygame.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/2
center_y=screen_height/2
pygame.mouse.set_visible(False),pygame.NOFRAME
fullscreenkaiguan=notfullscreenkaiguan
bottomlefttip+="全屏"+str(fullscreenkaiguan)
elifkey==K_ESCAPE:
globalmain_loops
main_loops=False
elifkey==K_SPACE:
pass
elifkey==K_s:
waiweikaiguan=notwaiweikaiguan
bottomlefttip+="闪烁"+str(waiweikaiguan)elifkey==K_t:
tiaodongkaiguan=nottiaodongkaiguan
bottomlefttip+="跳动"+str(tiaodongkaiguan)elifkey==K_KP_PLUSorkey==K_PLUS:
ifjumpfreq>5:
jumpfreq-=5
bottomlefttip+="频率="+str(jumpfreq)
elifkey==K_KP_MINUSorkey==K_MINUS:
ifjumpfreq<60:
jumpfreq+=5
bottomlefttip+="频率="+str(jumpfreq)
elifkey==K_MENU:
pass
else:
bottomlefttip+="无动作"pgzrun.go()
defevent():
globalcenter_x,center_yforeventinpygame.event.get():
ifevent.typenotin[KEYDOWN,MOUSEMOTION,MOUSEBUTTONDOWN,MOUSEBUTTONUP]:
print(event)
ifevent.type==QUIT:
globalmain_loops
main_loops=Falseelifevent.type==KEYDOWN:
键盘被按下unicode、key、mod
on_key_down(event.key)
https://blog.csdn.net/qq_41556318/article/details/86304649
http://t.zoukankan.com/liquancai-p-13235734.html
elifevent.type==MOUSEMOTION:
MOUSEMOTION鼠标移动pos、rel、buttons
<Event(1024-MouseMotion{'pos':(289,464),'rel':(2,-5),'buttons':(0,0,0),'touch':False,'window':None})>
on_mouse_move(event.pos,event.rel,event.buttons)
elifevent.type==MOUSEBUTTONDOWN:MOUSEBUTTONDOWN鼠标被按下pos、button
<Event(1025-MouseButtonDown{'pos':(289,464),'button':1,'touch':False,'window':None})>
on_mouse_down(event.pos,event.button)
elifevent.type==MOUSEBUTTONUP:MOUSEBUTTONUP鼠标被放开pos、button
on_mouse_up(event.pos,event.button)
elifevent.type==VIDEORESIZE:
center_x=event.w/2
center_y=event.h/2elifevent.type==WINDOWMAXIMIZED:
窗口最大化
print(event)
elifevent.type==WINDOWMINIMIZED:窗口最大化
print(event)
pygame.mixer.music.pause()
elifevent.type==WINDOWRESTORED:重新显示
pygame.mixer.music.unpause()
elifevent.type==WINDOWSHOWN:
print(event)elifevent.type==ACTIVEEVENT:
print(pygame.mixer.music.get_busy())
try:
ifevent.gainandnotpygame.mixer.music.get_busy():
显示内容
pygame.mixer.music.pause()
elifnotevent.gainandpygame.mixer.music.get_busy():
pygame.mixer.music.pause()
except:
pass
pass
ifname=='main':
musicloops("bfa.mp3")初始化动态心形点,只执行一次
init_dynamic_particles()
Runthegameloop.
whilemain_loops:
event()
update(1/jumpfreq)
draw()pygame.quit()
pyinstaller-F-c-w-ifavicon.ico--cleanxx-pygame.py
cxfreezexg.py--target-dirx--base-name=win32gui
2、编译
1)新建一个虚拟环境安装pygame,pyinstaller两个库
2)使用pyinstaller打包
说明一下,pyinstaller打包,会加载环境里的全部内容,所以需要单独新建环境,这样在dist生成的exe文件会比较小。
不用看.spec文件格式,用不到。
3)生成结果
说明:运行程序是没有窗口图标和声音的,需要dist中放一个bfa.mp3.这个是在465行。可以修改成自己的内容。
2.有了可运行程序,使用RAR压缩工具将资源和程序打包成独立可执行exe
1)将声音,图标,python打包生成的exe 打成一个rar
2)打开 dist.rar ,工具拦选择“自解压格式”
完成以上配置后选确定,两次。这时在目录下会生成dist.exe。这时可以运行查下一下效果。
3.将dist.exe配置成系统屏幕保护
1)将dist.exe 修改成 dist.scr 复制到 C:\Windows目录下双击运行
2)回到电脑桌面,鼠标右击,选择个性化打开如下图:
若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!如何使用Python制作自已的动态屏保的详细内容,希望对您有所帮助,信息来源于网络。