您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:pygame仿office的页面切换功能(完整代码)

51自学网 2021-10-30 22:37:06
  python
这篇教程pygame仿office的页面切换功能(完整代码)写得很实用,希望能帮到您。

一、最简单的切换功能

(一)源码

import sys, pygameimport osimport randompygame.init()  # 初始化pygame类screen = pygame.display.set_mode((600, 600))  # 设置窗口大小pygame.display.set_caption('美丽的屏保')  # 设置窗口标题tick = pygame.time.Clock()fps = 10  # 设置刷新率,数字越大刷新率越高fcclock = pygame.time.Clock()bglist = []flag = 0runimage = Nonenextimage = Nonedef 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))  # 设置背景为白色        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()

(二)效果

在这里插入图片描述

(三)解析

实际就是使用了runimage和nextimage保存两个图片,然后先黏贴nextimage,再黏贴runimage,让runimage显示在最前端。
并通过监听鼠标和键盘操作,每点击一次,切换一次页面。
并调用reset函数

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)

二、实现动态切屏功能

 (一)向左切换

import sys, pygameimport osimport randompygame.init()  # 初始化pygame类WIDTH = 600HEIGHT = 600screen = pygame.display.set_mode((WIDTH, HEIGHT))  # 设置窗口大小pygame.display.set_caption('美丽的屏保')  # 设置窗口标题tick = pygame.time.Clock()fps = 60  # 设置刷新率,数字越大刷新率越高fcclock = pygame.time.Clock()bglist = []flag = 0runimage = Nonenextimage = Noneflag = False   # FALSE没有切屏 TRUE 切屏flag2 = Falsei = 0j = 0step = 10def 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没有切屏 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没有切屏 TRUE 切屏                        flag = True                        flag2 = False            # if event.type == pygame.MOUSEBUTTONDOWN:            #     reset()        screen.fill((255, 255, 255))  # 设置背景为白色        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()

(二)向左切换效果

在这里插入图片描述

三、随机效果实现

实现上下左右效果

import sys, pygameimport osimport randompygame.init()  # 初始化pygame类WIDTH = 600HEIGHT = 600screen = pygame.display.set_mode((WIDTH, HEIGHT))  # 设置窗口大小pygame.display.set_caption('美丽的屏保')  # 设置窗口标题tick = pygame.time.Clock()fps = 60  # 设置刷新率,数字越大刷新率越高fcclock = pygame.time.Clock()bglist = []flag = 0runimage = Nonenextimage = Noneflag = False   # FALSE没有切屏 TRUE 切屏flag2 = Falsei = 0j = 0step = 10choose = 0def 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(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没有切屏 TRUE 切屏                        flag = True                        flag2 = False        screen.fill((255, 255, 255))  # 设置背景为白色        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()

四、效果展现

在这里插入图片描述

五、第二个版本

(一)修改了核心代码

 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, pygameimport osimport randompygame.init()  # 初始化pygame类WIDTH = 600HEIGHT = 600screen = pygame.display.set_mode((WIDTH, HEIGHT))  # 设置窗口大小pygame.display.set_caption('美丽的屏保')  # 设置窗口标题tick = pygame.time.Clock()fps = 60  # 设置刷新率,数字越大刷新率越高fcclock = pygame.time.Clock()bglist = []flag = 0runimage = Nonenextimage = Noneflag = False   # FALSE没有切屏 TRUE 切屏flag2 = Falsei = 0j = 0step = 10choose = 0def 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(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没有切屏 TRUE 切屏                        flag = True                        flag2 = False        screen.fill((255, 255, 255))  # 设置背景为白色        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()

(三)另一种效果

在这里插入图片描述

六、小结

Ok,V1和V2版本,两个版本,任君选择,比较简单,大家将就着看看啊。后面会有修订和更多有趣的案例,欢迎关注,感谢支持!

以上就是pygame实现类似office的页面切换功能的详细内容,更多关于pygame页面切换的资料请关注51zixue.net其它相关文章!


解决Pytorch内存溢出,Ubuntu进程killed的问题
Python爬虫之爬取我爱我家二手房数据
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。