最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python制作自已的動態(tài)屏保

 更新時間:2022年12月20日 16:05:00   作者:Brickie-liu  
這篇文章主要為大家詳細(xì)介紹了如何利用Python制作自已的動態(tài)屏保,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下

我的環(huán)境

win10

python3.X

pycharm

1.編寫自己的屏保程序

注意:屏保程序打開就是全屏,可自動循環(huán)播放

我的樣子如圖

1、代碼準(zhǔn)備

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, cos

pygame.init()
# 獲取顯示器大小
screen_width, screen_height = pygame.display.get_desktop_sizes()[0]

ICON = "./icon.png"
TITLE = "見到你時我的心"
WIDTH = 800
HEIGHT = 800
main_loops = True
# 心形中心點(diǎn)
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 = "[鼠標(biāo)位置]: "
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):
        # 全部一個呼吸系數(shù)
        # df = 1 + (2 - 1.5 ) * sin(t * 3) / 8
        # df = 1 +  (heartbeatmplitude )*sin(t * 3) / 8

        # 外內(nèi),內(nèi)快,參數(shù)外小內(nèi)大
        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 離散點(diǎn)數(shù)
        dt = 2 * pi / no_p
        t = 0
        while t <= 2 * pi:
            # 正向隨機(jī)分面
            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 = []

"""
若隨機(jī)變量X服從一個數(shù)學(xué)期望為μ、方差為σ^2的正態(tài)分布,記為N(μ,σ^2)
期望值μ決定了其位置,其標(biāo)準(zhǔn)差σ決定了分布的幅度。當(dāng)μ = 0,σ = 1時的正態(tài)分布是標(biāo)準(zhǔn)正態(tài)分布

心形公式
    x=16*sin(t)**3
    y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
"""
# 均值,心形的大小
mu = 1.1
# 是標(biāo)準(zhǔn)差,輻射范圍
sigma = .15

# 靜態(tài)心形點(diǎn)的大小
static_wh = (1.5, 1.5)
# 動態(tài)心形點(diǎn)大小,
dynamic_wh = (1, 2)
# 心跳幅度
heartbeatmplitude = 1.2

# 心形大小
size = 15

# 外部開關(guān)
waiweikaiguan = True
# 跳動開關(guān)
tiaodongkaiguan = True
# 窗口,全屏
fullscreenkaiguan = False
# 跳動頻率
jumpfreq=30

no_p = 10000
# dt 離散點(diǎn)數(shù)
dt = 2 * pi / no_p
t = 0

#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 3萬個點(diǎn)

def init_dynamic_particles():
    # dt 離散點(diǎn)數(shù)
    global t
    # 初始化跳動心形點(diǎn)
    while t <= 2 * pi:
        # 正向隨機(jī)分面
        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萬個點(diǎn)
    # no_p = 20000
    # dt 離散點(diǎn)數(shù)
    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成下一個實數(shù),它在 [x,y] 范圍內(nèi)
        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")  # 假裝這里有異常,一般針對難以復(fù)現(xiàn)的異常
        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():
    # 清空全部內(nèi)容
    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")

    # 顯示動態(tài)心形
    show_ynamic_particles()
    """
        初始化外部心形情況
    """
    show_static_particles()
    # 顯示鼠標(biāo)
    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 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)
    # 更新狀態(tài)
    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)
            # 發(fā)現(xiàn)從pygame.FULLSCREEN,到pygame.RESIZABLE調(diào)用一次不起作用
            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 鼠標(biāo)移動  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 鼠標(biāo)被按下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鼠標(biāo)被放開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():
            #         #顯示內(nèi)容
            #         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")
    # 初始化動態(tài)心形點(diǎn),只執(zhí)行一次
    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)新建一個虛擬環(huán)境安裝pygame,pyinstaller兩個庫

2)使用pyinstaller打包

說明一下,pyinstaller打包,會加載環(huán)境里的全部內(nèi)容,所以需要單獨(dú)新建環(huán)境,這樣在dist生成的exe文件會比較小。

pyinstaller 參數(shù)和使用說明可以參考:使用Pyinstaller打包exe文件詳細(xì)圖文教程

不用看.spec文件格式,用不到。

3)生成結(jié)果

說明:運(yùn)行程序是沒有窗口圖標(biāo)和聲音的,需要dist中放一個bfa.mp3.這個是在465行??梢孕薷某勺约旱膬?nèi)容。

2.有了可運(yùn)行程序,使用RAR壓縮工具將資源和程序打包成獨(dú)立可執(zhí)行exe

1)將聲音,圖標(biāo),python打包生成的exe 打成一個rar

2)打開 dist.rar ,工具攔選擇“自解壓格式”

完成以上配置后選確定,兩次。這時在目錄下會生成 dist.exe。這時可以運(yùn)行查下一下效果。

3.將dist.exe配置成系統(tǒng)屏幕保護(hù)

1)將dist.exe 修改成 dist.scr 復(fù)制到 C:\Windows目錄下雙擊運(yùn)行

2)回到電腦桌面,鼠標(biāo)右擊,選擇個性化打開如下圖:

到此這篇關(guān)于利用Python制作自已的動態(tài)屏保的文章就介紹到這了,更多相關(guān)Python動態(tài)屏保內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式

    spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式

    這篇文章主要介紹了spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • macbook安裝環(huán)境chatglm2-6b的詳細(xì)過程

    macbook安裝環(huán)境chatglm2-6b的詳細(xì)過程

    這篇文章主要介紹了macbook安裝chatglm2-6b的過程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 詳解Django之a(chǎn)dmin組件的使用和源碼剖析

    詳解Django之a(chǎn)dmin組件的使用和源碼剖析

    本篇文章主要介紹了詳解Django之a(chǎn)dmin的使用和源碼剖析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 探秘TensorFlow 和 NumPy 的 Broadcasting 機(jī)制

    探秘TensorFlow 和 NumPy 的 Broadcasting 機(jī)制

    這篇文章主要介紹了探秘TensorFlow 和 NumPy 的 Broadcasting 機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 使用python創(chuàng)建生成動態(tài)鏈接庫dll的方法

    使用python創(chuàng)建生成動態(tài)鏈接庫dll的方法

    這篇文章主要介紹了使用python創(chuàng)建生成動態(tài)鏈接庫dll的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Python中xmltodict庫的使用方法詳解

    Python中xmltodict庫的使用方法詳解

    在Python編程中,處理XML數(shù)據(jù)是一項常見且重要的任務(wù),XML(可擴(kuò)展標(biāo)記語言)是一種用于存儲和傳輸數(shù)據(jù)的標(biāo)記語言,Python的標(biāo)準(zhǔn)庫并不直接提供處理XML的便捷方法,因此我們需要借助第三方庫來實現(xiàn)這一功能,本文將詳細(xì)介紹xmltodict庫的使用,需要的朋友可以參考下
    2024-11-11
  • 使用Python進(jìn)行情感分析并可視化展示結(jié)果

    使用Python進(jìn)行情感分析并可視化展示結(jié)果

    情感分析是一種通過自然語言處理技術(shù)來識別、提取和量化文本中的情感傾向的方法,Python在這一領(lǐng)域有著豐富的庫和工具,本文將介紹如何使用Python進(jìn)行情感分析,并通過可視化展示結(jié)果,需要的朋友可以參考下
    2024-05-05
  • python分割和拼接字符串

    python分割和拼接字符串

    python分割和拼接字符串的實例,使用了string的split和join 方法,并對這二個方法做說明。
    2013-11-11
  • windows下安裝python的C擴(kuò)展編譯環(huán)境(解決Unable to find vcvarsall.bat)

    windows下安裝python的C擴(kuò)展編譯環(huán)境(解決Unable to find vcvarsall.bat)

    這篇文章主要介紹了windows下安裝python的C擴(kuò)展編譯環(huán)境(解決Unable to find vcvarsall.bat),需要的朋友可以參考下
    2018-02-02
  • python入門前的第一課 python怎樣入門

    python入門前的第一課 python怎樣入門

    人工智能這么火,0基礎(chǔ)能學(xué)python嗎?python該怎么選擇編輯器?怎么搭建python運(yùn)行環(huán)境?python好學(xué)嗎,怎么學(xué)?這是所有python入門前同學(xué)都會提出的疑問,這篇文章和大家一起學(xué)習(xí)python,感興趣的小伙伴們可以加入
    2018-03-03

最新評論

东安县| 绍兴市| 崇阳县| 洛宁县| 崇信县| 厦门市| 大余县| 鄂托克旗| 凤山县| 砚山县| 时尚| 黑龙江省| 定兴县| 富川| 介休市| 镇原县| 和平县| 开平市| 东台市| 本溪市| 金昌市| 建昌县| 华阴市| 泸溪县| 辉南县| 田林县| 乾安县| 巴青县| 武山县| 苍山县| 南充市| 育儿| 章丘市| 兴海县| 衡东县| 蚌埠市| 安多县| 扶沟县| 临汾市| 上杭县| 东海县|