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

Posted dhjabc_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了提高篇pygame实现类似office的页面切换功能,也许你也能学会!(其二)相关的知识,希望对你有一定的参考价值。

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

文章目录

一、直接上源码

(一)左右切换核心代码

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):
               

以上是关于提高篇pygame实现类似office的页面切换功能,也许你也能学会!(其二)的主要内容,如果未能解决你的问题,请参考以下文章

pygame实现类似office的页面切换功能,也许你也能学会!

pygame实现类似office的页面切换功能,也许你也能学会!

微软逼迫Office客户切换成年度付费会员:否则月度订阅价格将提高20%

类似tabBar的切换页面效果(微信小程序)

python(pygame)滑稽大战(类似飞机大战) 教程

Office之PPT