Python趣味挑戰之pygame實現無敵好看的百葉窗動態效果

一、案例知識點概述

(一)使用到的python庫

使用pygame庫、random庫和os、sys等系統庫。

其中:
pygame庫實現主體功能,提供窗口界面顯示、動態效果展示等
random庫實現隨機數的生成,通過隨機數實現動態百葉窗的上下左右選擇、百葉窗的數量選擇等功能。 os庫實現圖片資源的裝載和讀取。
sys庫實現退出操作等。

(二) 整體實現邏輯

通過WIDTH = 600HEIGHT = 600設置窗口的高度和寬度
通過runimagenextimage 設置當前顯示的圖像和下一張要顯示的圖像
通過num_part = random.randint(3,8)來設置要顯示的百葉窗的數量
通過num_list = []保存當前runimage拆分出來的百葉窗的surface資源,用於在百葉窗動態效果過程中顯示。
通過choose來設置是上下運動還是左右運動。

二、準備工作

(一)實現pygame的主窗口

import pygame,sys

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('大小框展示')
fcclock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.K_F1:
            pygame.quit()
            sys.exit()
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口

黑黑的框,不截圖瞭。大傢都懂。

(二)貼個圖顯示得好看點

import pygame,sys

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('大小框展示')
fcclock = pygame.time.Clock()
img = pygame.image.load('./image/aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg').convert_alpha()
img = pygame.transform.scale(img, (500, 500))
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.K_F1:
            pygame.quit()
            sys.exit()
    screen.blit(img,(0,0))
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口

在這裡插入圖片描述

(三)圖片從哪裡來

這裡建議直接通過網絡上下載免費的、好看的圖片,並保存在指定的文件夾,用於過程中展現。

我認為有三種方法:

其一:使用爬蟲技術從網上下載圖片,可以開一個子線程負責采集網上圖片,然後加載到list列表中;
其二:可以直接對電腦中所有的盤進行自動檢索,然後加載到list列表中; 其三:指定目錄,然後加載到list列表中;
我這裡偷個懶,選擇第三種方法實現。

具體實現代碼如下:

  path = './image/'
    files = []
    dirs = os.listdir(path)
    for diretion in dirs:
        files.append(path + diretion)

(四)圖片裝載

我為什麼在初始化的時候就進行裝載呢?

原因是:解決效率問題,無需每次使用時重復加載,而且在初始化的時候就適配屏幕大小進行圖片縮放。

因此,我把這個過程打包成一個函數,方便後續調用,而且參數傳遞為:屏幕的大小。然後返回bglist對象。

for file in files:
    picture = pygame.transform.scale(pygame.image.load(file), (1440, 900))
    dSurface = picture
    # dSurface = pygame.image.load(file).convert()
    bglist.append(dSurface)

OK,圖片有瞭,窗口有瞭,那麼就開始實現我們的業務邏輯吧。

三、核心功能模塊

(一)實現init_image函數初始化加載圖片到surface對象

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)

(二)初始化相關變量

runimage = None
nextimage = None
flag = False   # FALSE沒有切屏 TRUE 切屏
flag2 = False
choose = 6

num_part = random.randint(3,8)  # 記錄分成多少塊矩形框
num_list = []
num_increse = 1
inc = random.choice([-1,1])
while num_increse<=num_part:
    inc = -inc
    num_list.append(inc)
    num_increse += 1

這裡,建議大傢思考一下為什麼要引入變量flag和flag2

(三)每次百葉窗切換完之後重置

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

    num_part = random.randint(3,8)  # 記錄分成多少塊矩形框
    num_list = []
    num_increse = 1
    inc = random.choice([-1,1])
    while num_increse <= num_part:
        inc = -inc
        num_list.append(inc)
        num_increse += 1

(四)實現百葉窗動態切換的run函數

def run():
    global flag,runimage,flag2,nextimage,i,j,choose,num_part,num_list
    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沒有切屏 TRUE 切屏
                        flag = True
                        flag2 = False
        screen.fill((255, 255, 255))  # 設置背景為白色
        if flag:
            if choose==6:
                select_rect = []
                kk = 0
                while kk < num_part:
                    tmp_rect = pygame.Rect(kk * WIDTH/num_part,0,WIDTH/num_part,HEIGHT)
                    select_rect.append(runimage.subsurface(tmp_rect).copy())
                    kk += 1
                screen.blit(nextimage, (0, 0))
                mm = 0
                for each in zip(select_rect,num_list):
                    if each[1]==1:
                        screen.blit(each[0], (i+mm*WIDTH/num_part, -j))
                    else:
                        screen.blit(each[0], (i+mm*WIDTH/num_part, j))
                    mm += 1
                j += step
                if j >= HEIGHT:
                    flag2 = True
            elif choose==7:
                select_rect = []
                kk = 0
                while kk < num_part:
                    tmp_rect = pygame.Rect(0,kk * HEIGHT/num_part,WIDTH,HEIGHT/num_part)
                    select_rect.append(runimage.subsurface(tmp_rect).copy())
                    kk += 1
                screen.blit(nextimage, (0, 0))
                mm = 0
                for each in zip(select_rect,num_list):
                    if each[1]==1:
                        screen.blit(each[0], (-i, j+mm*HEIGHT/num_part))
                    else:
                        screen.blit(each[0], (i, j+mm*HEIGHT/num_part))
                    mm += 1
                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()  # 刷新窗口

(五)主函數

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

四、完整代碼

import sys, pygame
import os
import random

pygame.init()  # 初始化pygame類
WIDTH = 600
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))  # 設置窗口大小
pygame.display.set_caption('美麗的屏保')  # 設置窗口標題
tick = pygame.time.Clock()
fps = 60  # 設置刷新率,數字越大刷新率越高
fcclock = pygame.time.Clock()
runimage = None
nextimage = None
flag = False   # FALSE沒有切屏 TRUE 切屏
flag2 = False
choose = 6

num_part = random.randint(3,8)  # 記錄分成多少塊矩形框
num_list = []
num_increse = 1
inc = random.choice([-1,1])
while num_increse<=num_part:
    inc = -inc
    num_list.append(inc)
    num_increse += 1

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
        bglist.append(dSurface)

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

    num_part = random.randint(3,8)  # 記錄分成多少塊矩形框
    num_list = []
    num_increse = 1
    inc = random.choice([-1,1])
    while num_increse <= num_part:
        inc = -inc
        num_list.append(inc)
        num_increse += 1


def run():
    global flag,runimage,flag2,nextimage,i,j,choose,num_part,num_list
    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沒有切屏 TRUE 切屏
                        flag = True
                        flag2 = False
        screen.fill((255, 255, 255))  # 設置背景為白色
        if flag:
            if choose==6:
                select_rect = []
                kk = 0
                while kk < num_part:
                    tmp_rect = pygame.Rect(kk * WIDTH/num_part,0,WIDTH/num_part,HEIGHT)
                    select_rect.append(runimage.subsurface(tmp_rect).copy())
                    kk += 1
                screen.blit(nextimage, (0, 0))
                mm = 0
                for each in zip(select_rect,num_list):
                    if each[1]==1:
                        screen.blit(each[0], (i+mm*WIDTH/num_part, -j))
                    else:
                        screen.blit(each[0], (i+mm*WIDTH/num_part, j))
                    mm += 1
                j += step
                if j >= HEIGHT:
                    flag2 = True
            elif choose==7:
                select_rect = []
                kk = 0
                while kk < num_part:
                    tmp_rect = pygame.Rect(0,kk * HEIGHT/num_part,WIDTH,HEIGHT/num_part)
                    select_rect.append(runimage.subsurface(tmp_rect).copy())
                    kk += 1
                screen.blit(nextimage, (0, 0))
                mm = 0
                for each in zip(select_rect,num_list):
                    if each[1]==1:
                        screen.blit(each[0], (-i, j+mm*HEIGHT/num_part))
                    else:
                        screen.blit(each[0], (i, j+mm*HEIGHT/num_part))
                    mm += 1
                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()  # 刷新窗口

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

五、運行效果

在這裡插入圖片描述

OK,寫完,其實還是蠻有趣的,大傢可以自動動手敲敲,也許比我寫的更好。

到此這篇關於Python趣味挑戰之pygame實現無敵好看的百葉窗動態效果的文章就介紹到這瞭,更多相關pygame實現百葉窗動態效果內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: