【提高篇】pygame实现类似office的页面切换功能,也许你也能学会!(其二)

续pygame实现类似office的页面切换功能,也许你也能学会!
继续深入介绍pygame实现类似office的页面切换功能。
这次实现上下同时切换、左右同时切换,并多矩形框随机切换等功能,欢迎继续往下看。

文章目录

  • 一、直接上源码
    • (一)左右切换核心代码
    • (二)上下切换核心代码
    • (三)通过choose变量控制随机性
    • (四)完整代码
    • (五)运行效果
  • 二、多矩形框动态效果
    • (一)记录生成多少块矩形框
    • (二)上下矩形框动态
    • (三)左右矩形框动态
    • (四)每次reset函数调用
    • (五)完整代码
    • (六)运行效果
  • 三、整合完全的代码
    • (一)核心随机性
    • (二)完整代码
    • (三)运行效果

一、直接上源码

(一)左右切换核心代码

select_rect1 = pygame.Rect(0,0,WIDTH/2,HEIGHT)
select_rect2 = pygame.Rect(WIDTH/2,0,WIDTH/2,HEIGHT)
capture1 = runimage.subsurface(select_rect1).copy()
capture2 = runimage.subsurface(select_rect2).copy()
screen.blit(nextimage, (0, 0))
screen.blit(capture1, (-i, j))
screen.blit(capture2, (WIDTH/2+i, j))
i += step
if i >= WIDTH/2:
    flag2 = True

(二)上下切换核心代码

select_rect1 = pygame.Rect(0,0,WIDTH,HEIGHT/2)
select_rect2 = pygame.Rect(0,HEIGHT/2,WIDTH,HEIGHT/2)
capture1 = runimage.subsurface(select_rect1).copy()
capture2 = runimage.subsurface(select_rect2).copy()
screen.blit(nextimage, (0, 0))
screen.blit(capture1, (i, -j))
screen.blit(capture2, (i, HEIGHT/2+j))
j += step
if j >= HEIGHT/2:
    flag2 = True

(三)通过choose变量控制随机性

choose = random.randint(4,5)

(四)完整代码

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()
bglist = []
flag = 0
runimage = None
nextimage = None
flag = False   # FALSE没有切屏 TRUE 切屏
flag2 = False
i = 0
j = 0
step = 10
choose = 4

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没有切屏 TRUE 切屏
    flag2 = False
    i = 0
    j = 0
    choose = random.randint(4,5)
    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没有切屏 TRUE 切屏
                        flag = True
                        flag2 = False
        screen.fill((255, 255, 255))  # 设置背景为白色
        if flag:
            if choose==0:
                i -= step
                screen.blit(nextimage, (i+WIDTH, 0))
                screen.blit(runimage, (i, j))
                if i <= -WIDTH:
                    flag2 = True
            elif choose==1:
                screen.blit(nextimage, (i-WIDTH, 0))
                screen.blit(runimage, (i, j))
                i += step
                if i >= WIDTH:
                    flag2 = True
            elif choose==2:
                screen.blit(nextimage, (0, j+HEIGHT))
                screen.blit(runimage, (i, j))
                j -= step
                if j <= -HEIGHT:
                    flag2 = True
            elif choose==3:
                screen.blit(nextimage, (0, j-HEIGHT))
                screen.blit(runimage, (i, j))
                j += step
                if j >= HEIGHT:
                    flag2 = True
            elif choose==4:
                select_rect1 = pygame.Rect(0,0,WIDTH/2,HEIGHT)
                select_rect2 = pygame.Rect(WIDTH/2,0,WIDTH/2,HEIGHT)
                capture1 = runimage.subsurface(select_rect1).copy()
                capture2 = runimage.subsurface(select_rect2).copy()
                screen.blit(nextimage, (0, 0))
                screen.blit(capture1, (-i, j))
                screen.blit(capture2, (WIDTH/2+i, j))
                i += step
                if i >= WIDTH/2:
                    flag2 = True
            elif choose==5:
                select_rect1 = pygame.Rect(0,0,WIDTH,HEIGHT/2)
                select_rect2 = pygame.Rect(0,HEIGHT/2,WIDTH,HEIGHT/2)
                capture1 = runimage.subsurface(select_rect1).copy()
                capture2 = runimage.subsurface(select_rect2).copy()
                screen.blit(nextimage, (0, 0))
                screen.blit(capture1, (i, -j))
                screen.blit(capture2, (i, HEIGHT/2+j))
                j += step
                if j >= HEIGHT/2:
                    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()

(五)运行效果

二、多矩形框动态效果

(一)记录生成多少块矩形框

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

(二)上下矩形框动态

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

(三)左右矩形框动态

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

(四)每次reset函数调用

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
    # while num_increse<=num_part:
    #     num_list.append(random.randint(0,1))
    #     num_increse += 1

    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

(五)完整代码

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()
bglist = []
flag = 0
runimage = None
nextimage = None
flag = False   # FALSE没有切屏 TRUE 切屏
flag2 = False
i = 0
j = 0
step = 10
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
        # dSurface = pygame.image.load(file).convert()
        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
    # while num_increse<=num_part:
    #     num_list.append(random.randint(0,1))
    #     num_increse += 1

    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()
            # print(choose)
        fcclock.tick(fps)
        pygame.display.flip()  # 刷新窗口
        # time.sleep(10)

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

(六)运行效果

三、整合完全的代码

实现上、下、左、右、上下、左右、多矩形框等操作的完整代码。
分享给大家。
通过choose进行控制,随机性。

(一)核心随机性

if flag:
    if choose==0:
        i -= step
        screen.blit(nextimage, (i+WIDTH, 0))
        screen.blit(runimage, (i, j))
        if i <= -WIDTH:
            flag2 = True
    elif choose==1:
        screen.blit(nextimage, (i-WIDTH, 0))
        screen.blit(runimage, (i, j))
        i += step
        if i >= WIDTH:
            flag2 = True
    elif choose==2:
        screen.blit(nextimage, (0, j+HEIGHT))
        screen.blit(runimage, (i, j))
        j -= step
        if j <= -HEIGHT:
            flag2 = True
    elif choose==3:
        screen.blit(nextimage, (0, j-HEIGHT))
        screen.blit(runimage, (i, j))
        j += step
        if j >= HEIGHT:
            flag2 = True
    elif choose==4:
        select_rect1 = pygame.Rect(0,0,WIDTH/2,HEIGHT)
        select_rect2 = pygame.Rect(WIDTH/2,0,WIDTH/2,HEIGHT)
        capture1 = runimage.subsurface(select_rect1).copy()
        capture2 = runimage.subsurface(select_rect2).copy()
        screen.blit(nextimage, (0, 0))
        screen.blit(capture1, (-i, j))
        screen.blit(capture2, (WIDTH/2+i, j))
        i += step
        if i >= WIDTH/2:
            flag2 = True
    elif choose==5:
        select_rect1 = pygame.Rect(0,0,WIDTH,HEIGHT/2)
        select_rect2 = pygame.Rect(0,HEIGHT/2,WIDTH,HEIGHT/2)
        capture1 = runimage.subsurface(select_rect1).copy()
        capture2 = runimage.subsurface(select_rect2).copy()
        screen.blit(nextimage, (0, 0))
        screen.blit(capture1, (i, -j))
        screen.blit(capture2, (i, HEIGHT/2+j))
        j += step
        if j >= HEIGHT/2:
            flag2 = True

    elif 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()

(二)完整代码

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()
bglist = []
flag = 0
runimage = None
nextimage = None
flag = False   # FALSE没有切屏 TRUE 切屏
flag2 = False
i = 0
j = 0
step = 10
choose = 0

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
        # dSurface = pygame.image.load(file).convert()
        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(0,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
    # while num_increse<=num_part:
    #     num_list.append(random.randint(0,1))
    #     num_increse += 1

    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==0:
                i -= step
                screen.blit(nextimage, (i+WIDTH, 0))
                screen.blit(runimage, (i, j))
                if i <= -WIDTH:
                    flag2 = True
            elif choose==1:
                screen.blit(nextimage, (i-WIDTH, 0))
                screen.blit(runimage, (i, j))
                i += step
                if i >= WIDTH:
                    flag2 = True
            elif choose==2:
                screen.blit(nextimage, (0, j+HEIGHT))
                screen.blit(runimage, (i, j))
                j -= step
                if j <= -HEIGHT:
                    flag2 = True
            elif choose==3:
                screen.blit(nextimage, (0, j-HEIGHT))
                screen.blit(runimage, (i, j))
                j += step
                if j >= HEIGHT:
                    flag2 = True
            elif choose==4:
                select_rect1 = pygame.Rect(0,0,WIDTH/2,HEIGHT)
                select_rect2 = pygame.Rect(WIDTH/2,0,WIDTH/2,HEIGHT)
                capture1 = runimage.subsurface(select_rect1).copy()
                capture2 = runimage.subsurface(select_rect2).copy()
                screen.blit(nextimage, (0, 0))
                screen.blit(capture1, (-i, j))
                screen.blit(capture2, (WIDTH/2+i, j))
                i += step
                if i >= WIDTH/2:
                    flag2 = True
            elif choose==5:
                select_rect1 = pygame.Rect(0,0,WIDTH,HEIGHT/2)
                select_rect2 = pygame.Rect(0,HEIGHT/2,WIDTH,HEIGHT/2)
                capture1 = runimage.subsurface(select_rect1).copy()
                capture2 = runimage.subsurface(select_rect2).copy()
                screen.blit(nextimage, (0, 0))
                screen.blit(capture1, (i, -j))
                screen.blit(capture2, (i, HEIGHT/2+j))
                j += step
                if j >= HEIGHT/2:
                    flag2 = True

            elif 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()
            # print(choose)
        fcclock.tick(fps)
        pygame.display.flip()  # 刷新窗口
        # time.sleep(10)

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

(三)运行效果

后面会有修订和更多有趣的案例,欢迎关注,感谢支持!

你可能感兴趣的:(pygame游戏开发系列,python,pygame,游戏)