主頁(yè) > 知識(shí)庫(kù) > Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無敵好看的百葉窗動(dòng)態(tài)效果

Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無敵好看的百葉窗動(dòng)態(tài)效果

熱門標(biāo)簽:智能手機(jī) 呼叫中心市場(chǎng)需求 檢查注冊(cè)表項(xiàng) 美圖手機(jī) 鐵路電話系統(tǒng) 服務(wù)器配置 網(wǎng)站文章發(fā)布 銀行業(yè)務(wù)

一、案例知識(shí)點(diǎn)概述

(一)使用到的python庫(kù)

使用pygame庫(kù)、random庫(kù)和os、sys等系統(tǒng)庫(kù)。

其中:
pygame庫(kù)實(shí)現(xiàn)主體功能,提供窗口界面顯示、動(dòng)態(tài)效果展示等
random庫(kù)實(shí)現(xiàn)隨機(jī)數(shù)的生成,通過隨機(jī)數(shù)實(shí)現(xiàn)動(dòng)態(tài)百葉窗的上下左右選擇、百葉窗的數(shù)量選擇等功能。 os庫(kù)實(shí)現(xiàn)圖片資源的裝載和讀取。
sys庫(kù)實(shí)現(xiàn)退出操作等。

(二) 整體實(shí)現(xiàn)邏輯

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

二、準(zhǔn)備工作

(一)實(shí)現(xiàn)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()  # 刷新窗口

黑黑的框,不截圖了。大家都懂。

(二)貼個(gè)圖顯示得好看點(diǎn)

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()  # 刷新窗口

(三)圖片從哪里來

這里建議直接通過網(wǎng)絡(luò)上下載免費(fèi)的、好看的圖片,并保存在指定的文件夾,用于過程中展現(xiàn)。

我認(rèn)為有三種方法:

其一:使用爬蟲技術(shù)從網(wǎng)上下載圖片,可以開一個(gè)子線程負(fù)責(zé)采集網(wǎng)上圖片,然后加載到list列表中;
其二:可以直接對(duì)電腦中所有的盤進(jìn)行自動(dòng)檢索,然后加載到list列表中; 其三:指定目錄,然后加載到list列表中;
我這里偷個(gè)懶,選擇第三種方法實(shí)現(xiàn)。

具體實(shí)現(xiàn)代碼如下:

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

(四)圖片裝載

我為什么在初始化的時(shí)候就進(jìn)行裝載呢?

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

因此,我把這個(gè)過程打包成一個(gè)函數(shù),方便后續(xù)調(diào)用,而且參數(shù)傳遞為:屏幕的大小。然后返回bglist對(duì)象。

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,圖片有了,窗口有了,那么就開始實(shí)現(xiàn)我們的業(yè)務(wù)邏輯吧。

三、核心功能模塊

(一)實(shí)現(xiàn)init_image函數(shù)初始化加載圖片到surface對(duì)象

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)

(二)初始化相關(guān)變量

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

(四)實(shí)現(xiàn)百葉窗動(dòng)態(tài)切換的run函數(shù)

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))  # 設(shè)置背景為白色
        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()  # 刷新窗口

(五)主函數(shù)

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))  # 設(shè)置窗口大小
pygame.display.set_caption('美麗的屏保')  # 設(shè)置窗口標(biāo)題
tick = pygame.time.Clock()
fps = 60  # 設(shè)置刷新率,數(shù)字越大刷新率越高
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))  # 設(shè)置背景為白色
        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()

五、運(yùn)行效果

OK,寫完,其實(shí)還是蠻有趣的,大家可以自動(dòng)動(dòng)手敲敲,也許比我寫的更好。

到此這篇關(guān)于Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無敵好看的百葉窗動(dòng)態(tài)效果的文章就介紹到這了,更多相關(guān)pygame實(shí)現(xiàn)百葉窗動(dòng)態(tài)效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python趣味挑戰(zhàn)之教你用pygame畫進(jìn)度條
  • Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果
  • Python3+Pygame實(shí)現(xiàn)射擊游戲完整代碼
  • python 基于pygame實(shí)現(xiàn)俄羅斯方塊
  • python pygame 憤怒的小鳥游戲示例代碼
  • Python3.9.0 a1安裝pygame出錯(cuò)解決全過程(小結(jié))
  • python之pygame模塊實(shí)現(xiàn)飛機(jī)大戰(zhàn)完整代碼
  • Python使用Pygame繪制時(shí)鐘
  • Python3.8安裝Pygame教程步驟詳解
  • python pygame入門教程

標(biāo)簽:長(zhǎng)治 沈陽(yáng) 紅河 滄州 新疆 上海 河南 樂山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無敵好看的百葉窗動(dòng)態(tài)效果》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266