<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
我的環境
win10
python3.X
pycharm
1.編寫自己的屏保程式
注意:屏保程式開啟就是全螢幕,可自動迴圈播放
我的樣子如圖
1、程式碼準備
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, cos pygame.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: pass bottomlefttip_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 = size self.f = f def 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) / 8 self.pos[0] = self.pos0[0] * df self.pos[1] = self.pos0[1] * df class MouseParticle(): def __init__(self, pos): # (left, top, width, height) self.pos = pos.copy() self.particles = [] self.xiaoshishudu = .8 self.xiaoshishuduxishu = 1.2 self.show = .5 no_p = 50 # dt 離散點數 dt = 2 * pi / no_p t = 0 while t <= 2 * pi: # 正向隨機分面 l = mu - abs(random.gauss(mu, sigma) - mu) # 雙向分佈 # l=random.gauss(mu, sigma) # l=1,表示畫一個線 # l=1 xleft = l * 16 * sin(t) ** 3 ytop = l * (13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)) t += dt self.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: return if self.xiaoshishudu < 0.000005: self.show = 0 for p in self.particles: p.draw(self.pos[0], self.pos[1]) self.update() def update(self): self.xiaoshishudu = self.xiaoshishudu ** self.xiaoshishuduxishu for p in self.particles: p.update(self.xiaoshishudu) def jiashudu(self): if self.xiaoshishuduxishu < 3: self.xiaoshishuduxishu += .1 def jianshudu(self): if self.xiaoshishuduxishu > 1.1: self.xiaoshishuduxishu -= .1 mouseParticleList = [] particles = [] """ 若隨機變數X服從一個數學期望為μ、方差為σ^2的正態分佈,記為N(μ,σ^2) 期望值μ決定了其位置,其標準差σ決定了分佈的幅度。當μ = 0,σ = 1時的正態分佈是標準正態分佈 心形公式 x=16*sin(t)**3 y=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=30 no_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=1 xleft = l * 16 * sin(t) ** 3 ytop = l * (13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)) t += dt particles.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 = 0 while waiweikaiguan and t < 2 * pi: f = random.gauss(mu, sigma * 2) x = 16 * sin(t) ** 3 y = 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 * 2 def show_mouse_particles(): global mouseParticleList t = [] for p in mouseParticleList: if p.show: t.append(p) p.draw() else: break mouseParticleList = t def add_mouse_particles(pos): global mouseParticleList mouseParticleList = [MouseParticle(pos)] + mouseParticleList def 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 = pos elif position.startswith("m"): pass elif position.startswith("bottomleft"): textRect.bottomleft=pos elif position.startswith("bottomright"): textRect.bottomright=pos elif position.startswith("topleft"): textRect.topleft=pos elif position.startswith("topright"): textRect.topright=pos else: 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 # centery def 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("cccccccccnbbbbbbbbb", 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 t t += dt if 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: pass def on_mouse_down(pos, button): # print(pos, button) global bottomrighttip bottomrighttip = bottomrighttip_h + str(pos) + str(button) def on_mouse_up(pos, button): pass def on_mouse_move(pos, rel, buttons): # print(pos, rel, buttons) global bottomrighttip bottomrighttip = bottomrighttip_h + str(pos) # 更新狀態 add_mouse_particles([pos[0], pos[1]]) def on_key_down(key): global screen global bottomlefttip, fullscreenkaiguan, waiweikaiguan, tiaodongkaiguan bottomlefttip = bottomlefttip_h + pygame.key.name(key) global center_x, center_y global jumpfreq if 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 / 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 = not fullscreenkaiguan bottomlefttip += " 全螢幕"+str(fullscreenkaiguan) elif key == K_ESCAPE: global main_loops main_loops=False elif key == K_SPACE: pass elif key == K_s: waiweikaiguan = not waiweikaiguan bottomlefttip += " 閃爍"+str(waiweikaiguan) elif key == K_t: tiaodongkaiguan = not tiaodongkaiguan bottomlefttip += " 跳動"+str(tiaodongkaiguan) elif key == K_KP_PLUS or key == K_PLUS: if jumpfreq>5: jumpfreq-=5 bottomlefttip += " 頻率=" + str(jumpfreq) elif key == K_KP_MINUS or key == K_MINUS: if jumpfreq<60: jumpfreq+=5 bottomlefttip += " 頻率=" + str(jumpfreq) elif key == K_MENU: pass else: bottomlefttip += " 無動作 " # pgzrun.go() def event(): global center_x, center_y for event in pygame.event.get(): # if event.type not in [KEYDOWN, MOUSEMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP]: # print(event) if event.type == QUIT: global main_loops main_loops = False elif event.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 elif event.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) elif event.type == MOUSEBUTTONDOWN: # MOUSEBUTTONDOWN 滑鼠被按下pos 、button # <Event(1025-MouseButtonDown {'pos': (289, 464), 'button': 1, 'touch': False, 'window': None})> on_mouse_down(event.pos, event.button) elif event.type == MOUSEBUTTONUP: # MOUSEBUTTONUP滑鼠被放開pos 、button on_mouse_up(event.pos, event.button) elif event.type == VIDEORESIZE: center_x = event.w / 2 center_y = event.h / 2 elif 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: # pass pass if __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兩個庫
2)使用pyinstaller打包
說明一下,pyinstaller打包,會載入環境裡的全部內容,所以需要單獨新建環境,這樣在dist生成的exe檔案會比較小。
pyinstaller 引數和使用說明可以參考:使用Pyinstaller打包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製作自已的動態屏保的文章就介紹到這了,更多相關Python動態屏保內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45