主頁(yè) > 知識(shí)庫(kù) > pygame仿office的頁(yè)面切換功能(完整代碼)

pygame仿office的頁(yè)面切換功能(完整代碼)

熱門標(biāo)簽:開(kāi)封語(yǔ)音外呼系統(tǒng)代理商 河北防封卡電銷卡 開(kāi)封自動(dòng)外呼系統(tǒng)怎么收費(fèi) 手機(jī)網(wǎng)頁(yè)嵌入地圖標(biāo)注位置 地圖標(biāo)注線上如何操作 應(yīng)電話機(jī)器人打電話違法嗎 電銷機(jī)器人的風(fēng)險(xiǎn) 天津電話機(jī)器人公司 400電話辦理哪種

一、最簡(jiǎn)單的切換功能

(一)源碼

import sys, pygame
import os
import random

pygame.init()  # 初始化pygame類
screen = pygame.display.set_mode((600, 600))  # 設(shè)置窗口大小
pygame.display.set_caption('美麗的屏保')  # 設(shè)置窗口標(biāo)題
tick = pygame.time.Clock()
fps = 10  # 設(shè)置刷新率,數(shù)字越大刷新率越高
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
nextimage = 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), (600, 600))
        dSurface = picture
        # dSurface = pygame.image.load(file).convert()
        bglist.append(dSurface)

def reset():
    global flag,runimage,nextimage
    flag = 0
    if nextimage is None:
        nextimage = random.choice(bglist)
    if runimage is None:
        runimage = random.choice(bglist)
    else:
        runimage = nextimage
        nextimage = random.choice(bglist)

def run():
    global flag,runimage
    reset()
    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))  # 設(shè)置背景為白色
        screen.blit(nextimage, (0, 0))
        screen.blit(runimage, (0, 0))
        fcclock.tick(fps)
        pygame.display.flip()  # 刷新窗口
        # time.sleep(10)

if __name__ == '__main__':
    init_image()
    run()

(二)效果

(三)解析

實(shí)際就是使用了runimage和nextimage保存兩個(gè)圖片,然后先黏貼nextimage,再黏貼runimage,讓runimage顯示在最前端。
并通過(guò)監(jiān)聽(tīng)鼠標(biāo)和鍵盤操作,每點(diǎn)擊一次,切換一次頁(yè)面。
并調(diào)用reset函數(shù)

def reset():
    global flag,runimage,nextimage
    flag = 0
    if nextimage is None:
        nextimage = random.choice(bglist)
    if runimage is None:
        runimage = random.choice(bglist)
    else:
        runimage = nextimage
        nextimage = random.choice(bglist)

二、實(shí)現(xiàn)動(dòng)態(tài)切屏功能

 (一)向左切換

import sys, pygame
import os
import random

pygame.init()  # 初始化pygame類
WIDTH = 600
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))  # 設(shè)置窗口大小
pygame.display.set_caption('美麗的屏保')  # 設(shè)置窗口標(biāo)題
tick = pygame.time.Clock()
fps = 60  # 設(shè)置刷新率,數(shù)字越大刷新率越高
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
nextimage = None
flag = False   # FALSE沒(méi)有切屏 TRUE 切屏
flag2 = False
i = 0
j = 0
step = 10

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), (WIDTH, HEIGHT))
        dSurface = picture
        # dSurface = pygame.image.load(file).convert()
        bglist.append(dSurface)

def reset():
    global flag,runimage,nextimage,flag2,i,j
    flag = False  # FALSE沒(méi)有切屏 TRUE 切屏
    flag2 = False
    i = 0
    j = 0
    if nextimage is None:
        nextimage = random.choice(bglist)
    if runimage is None:
        runimage = random.choice(bglist)
    else:
        runimage = nextimage
        nextimage = random.choice(bglist)

def run():
    global flag,runimage,flag2,nextimage,i,j
    reset()
    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:
                    if flag is False:# FALSE沒(méi)有切屏 TRUE 切屏
                        flag = True
                        flag2 = False
            # if event.type == pygame.MOUSEBUTTONDOWN:
            #     reset()
        screen.fill((255, 255, 255))  # 設(shè)置背景為白色
        if flag:
            screen.blit(nextimage, (0, 0))
            screen.blit(runimage, (i, j))
            i -= step
            if i = -WIDTH:
                flag2 = True
        else:
            screen.blit(nextimage, (0, 0))
            screen.blit(runimage, (0, 0))
        if flag2:
            reset()
        fcclock.tick(fps)
        pygame.display.flip()  # 刷新窗口
        # time.sleep(10)

if __name__ == '__main__':
    init_image()
    run()

(二)向左切換效果

三、隨機(jī)效果實(shí)現(xiàn)

實(shí)現(xiàn)上下左右效果

import sys, pygame
import os
import random

pygame.init()  # 初始化pygame類
WIDTH = 600
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))  # 設(shè)置窗口大小
pygame.display.set_caption('美麗的屏保')  # 設(shè)置窗口標(biāo)題
tick = pygame.time.Clock()
fps = 60  # 設(shè)置刷新率,數(shù)字越大刷新率越高
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
nextimage = None
flag = False   # FALSE沒(méi)有切屏 TRUE 切屏
flag2 = False
i = 0
j = 0
step = 10
choose = 0

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), (WIDTH, HEIGHT))
        dSurface = picture
        # dSurface = pygame.image.load(file).convert()
        bglist.append(dSurface)

def reset():
    global flag,runimage,nextimage,flag2,i,j,choose
    flag = False  # FALSE沒(méi)有切屏 TRUE 切屏
    flag2 = False
    i = 0
    j = 0
    choose = random.randint(0,3)
    if nextimage is None:
        nextimage = random.choice(bglist)
    if runimage is None:
        runimage = random.choice(bglist)
    else:
        runimage = nextimage
        nextimage = random.choice(bglist)

def run():
    global flag,runimage,flag2,nextimage,i,j,choose
    reset()
    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:
                    if flag is False:# FALSE沒(méi)有切屏 TRUE 切屏
                        flag = True
                        flag2 = False
        screen.fill((255, 255, 255))  # 設(shè)置背景為白色
        if flag:
            screen.blit(nextimage, (0,0))
            print(i+WIDTH,j+HEIGHT)
            screen.blit(runimage, (i, j))
            if choose==0:
                i -= step
                if i = -WIDTH:
                    flag2 = True
            elif choose==1:
                i += step
                if i >= WIDTH:
                    flag2 = True
            elif choose==2:
                j -= step
                if j = -HEIGHT:
                    flag2 = True
            elif choose==3:
                j += step
                if j >= HEIGHT:
                    flag2 = True
        else:
            screen.blit(nextimage, (0, 0))
            screen.blit(runimage, (0, 0))
        if flag2:
            reset()
            # print(choose)
        fcclock.tick(fps)
        pygame.display.flip()  # 刷新窗口
        # time.sleep(10)

if __name__ == '__main__':
    init_image()
    run()

四、效果展現(xiàn)

五、第二個(gè)版本

(一)修改了核心代碼

 if flag:
            if choose==0:
                i -= step
                screen.blit(nextimage, (i+WIDTH, 0))
                if i = -WIDTH:
                    flag2 = True
            elif choose==1:
                screen.blit(nextimage, (i-WIDTH, 0))
                i += step
                if i >= WIDTH:
                    flag2 = True
            elif choose==2:
                screen.blit(nextimage, (0, j+HEIGHT))
                j -= step
                if j = -HEIGHT:
                    flag2 = True
            elif choose==3:
                screen.blit(nextimage, (0, j-HEIGHT))
                j += step
                if j >= HEIGHT:
                    flag2 = True
            screen.blit(runimage, (i, j))
        else:
            screen.blit(nextimage, (0, 0))
            screen.blit(runimage, (0, 0))

(二)完整代碼

import sys, pygame
import os
import random

pygame.init()  # 初始化pygame類
WIDTH = 600
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))  # 設(shè)置窗口大小
pygame.display.set_caption('美麗的屏保')  # 設(shè)置窗口標(biāo)題
tick = pygame.time.Clock()
fps = 60  # 設(shè)置刷新率,數(shù)字越大刷新率越高
fcclock = pygame.time.Clock()
bglist = []
flag = 0
runimage = None
nextimage = None
flag = False   # FALSE沒(méi)有切屏 TRUE 切屏
flag2 = False
i = 0
j = 0
step = 10
choose = 0

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), (WIDTH, HEIGHT))
        dSurface = picture
        # dSurface = pygame.image.load(file).convert()
        bglist.append(dSurface)

def reset():
    global flag,runimage,nextimage,flag2,i,j,choose
    flag = False  # FALSE沒(méi)有切屏 TRUE 切屏
    flag2 = False
    i = 0
    j = 0
    choose = random.randint(0,3)
    if nextimage is None:
        nextimage = random.choice(bglist)
    if runimage is None:
        runimage = random.choice(bglist)
    else:
        runimage = nextimage
        nextimage = random.choice(bglist)

def run():
    global flag,runimage,flag2,nextimage,i,j,choose
    reset()
    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:
                    if flag is False:# FALSE沒(méi)有切屏 TRUE 切屏
                        flag = True
                        flag2 = False
        screen.fill((255, 255, 255))  # 設(shè)置背景為白色
        if flag:
            if choose==0:
                i -= step
                screen.blit(nextimage, (i+WIDTH, 0))
                if i = -WIDTH:
                    flag2 = True
            elif choose==1:
                screen.blit(nextimage, (i-WIDTH, 0))
                i += step
                if i >= WIDTH:
                    flag2 = True
            elif choose==2:
                screen.blit(nextimage, (0, j+HEIGHT))
                j -= step
                if j = -HEIGHT:
                    flag2 = True
            elif choose==3:
                screen.blit(nextimage, (0, j-HEIGHT))
                j += step
                if j >= HEIGHT:
                    flag2 = True
            screen.blit(runimage, (i, j))
        else:
            screen.blit(nextimage, (0, 0))
            screen.blit(runimage, (0, 0))
        if flag2:
            reset()
            # print(choose)
        fcclock.tick(fps)
        pygame.display.flip()  # 刷新窗口
        # time.sleep(10)

if __name__ == '__main__':
    init_image()
    run()

(三)另一種效果

六、小結(jié)

Ok,V1和V2版本,兩個(gè)版本,任君選擇,比較簡(jiǎn)單,大家將就著看看啊。后面會(huì)有修訂和更多有趣的案例,歡迎關(guān)注,感謝支持!

以上就是pygame實(shí)現(xiàn)類似office的頁(yè)面切換功能的詳細(xì)內(nèi)容,更多關(guān)于pygame頁(yè)面切換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • pygame多種方式實(shí)現(xiàn)屏保操作(自動(dòng)切換、鼠標(biāo)切換、鍵盤切換)

標(biāo)簽:蘭州 江蘇 六盤水 宿遷 常州 山東 成都 駐馬店

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pygame仿office的頁(yè)面切換功能(完整代碼)》,本文關(guān)鍵詞  pygame,仿,office,的,頁(yè)面,切換,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pygame仿office的頁(yè)面切換功能(完整代碼)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于pygame仿office的頁(yè)面切換功能(完整代碼)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章