pygame仿office的页面切换功能(完整代码)

一、最简单的切换功能

(一)源码

import sys, pygame
import os
import random

pygame.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 = 0
runimage = None
nextimage = None

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

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
    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, 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

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(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, 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

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(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页面切换的资料请关注我们其它相关文章!

(0)

相关推荐

  • pygame多种方式实现屏保操作(自动切换、鼠标切换、键盘切换)

    pygame多种方式实现屏保操作(自动切换.鼠标切换.键盘切换),欢迎往下观看. 一.图片处理 (一)图片采集 我认为有三种方法: 其一:使用爬虫技术从网上下载图片,可以开一个子线程负责采集网上图片,然后加载到list列表中: 其二:可以直接对电脑中所有的盘进行自动检索,然后加载到list列表中: 其三:指定目录,然后加载到list列表中: 我这里偷个懒,选择第三种方法实现.具体代码如下: path = './image/' files = [] dirs = os.listdir(path)

  • pygame仿office的页面切换功能(完整代码)

    一.最简单的切换功能 (一)源码 import sys, pygame import os import random pygame.init() # 初始化pygame类 screen = pygame.display.set_mode((600, 600)) # 设置窗口大小 pygame.display.set_caption('美丽的屏保') # 设置窗口标题 tick = pygame.time.Clock() fps = 10 # 设置刷新率,数字越大刷新率越高 fcclock =

  • 微信小程序实现tab页面切换功能

    效果图如下所示: wxml <scroll-view scroll-x="true" class="ip_tab_comtainer"> <view class="ip_tab_comtainer_padd"></view> <block wx:for="{{ips}}" wx:for-item="ip" wx:key="{{ip.id}}"&g

  • 微信小程序自定义可滑动顶部TabBar选项卡实现页面切换功能示例

    本文实例讲述了微信小程序自定义可滑动顶部TabBar选项卡实现页面切换功能.分享给大家供大家参考,具体如下: 顶部滚动选项卡 话不多说,直接上代码 pages/home/home.wxml <scroll-view scroll-x="true" style='width: 100%; white-space:nowrap; '> <!-- tab --> <view class="tab"> <view class=&qu

  • 微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能图文详解

    本文实例讲述了微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能.分享给大家供大家参考,具体如下: 微信小程序开发中选项卡.在android中选项卡一般用fragment,到了小程序这里瞬间懵逼了. 总算做出来了.分享出来看看. 先看效果: 再上代码: 1.index.wxml <!--index.wxml--> <view class="swiper-tab"> <view class="swiper-tab-list {{c

  • 微信小程序中的列表切换功能实例代码详解

    感觉这列表切换有点类似于轮播图,而且感觉这代码直接可以拿来用,稍微改一改样式什么的就OK了,列表切换也是用到的地方也很多 wxml中的代码如下: <!-- 标签页面标题 --> <view class="tab"> <view class="tab-item {{tab==0?'active':''}}" bindtap="changeItem" data-item="0">音乐推荐<

  • Android ViewPager撤消左右滑动切换功能实现代码

    最近做项目要求某种情况下ViewPager不能滑动,那么我们只需要重写这个方法就可以禁止ViewPager滑动.下面通过本文给大家ViewPager取消左右滑动切换功能的实例代码,具体代码如下所示: IndexViewPager.Java: <span style="background-color: rgb(255, 255, 255);">import android.content.Context; import android.support.v4.view.Vie

  • Android 实现无网络页面切换的示例代码

    本文介绍了Android 实现无网络页面切换的示例代码,分享给大家,具体如下: 实现思路 需求是在无网络的时候显示特定的页面,想到要替换页面的地方,大多都是recyclerview或者第三方recyclerview这种需要显示数据的地方,因此决定替换掉页面中所有的recyclerview为无网络页面 实现过程 1 在BaseActivity中,当加载布局成功以后,通过id找到要替换的view,通过indexOfChild()方法,找到要替换的view的位置,再通过remove和add view来

  • Android 仿余额宝数字跳动动画效果完整代码

    一:想都不用想的,有图有真相,看着爽了,在看下面源码 二:实例源码分析 ①:首先定义接口 package com.demo.tools.view; /** * 数字动画自定义 * * @author zengtao 2015年7月17日 上午11:48:27 * */ public interface RiseNumberBase { public void start(); public RiseNumberTextView withNumber(float number); public R

  • Android仿滴滴出行验证码输入框功能实例代码

    最近公司项目中有一个类似滴滴出行填写验证码的弹框,下面是我撸出来的效果: 中间的那个输入密码的6个框框其实就是用shape画的背景,通过监听EditText获取焦点来改变背景,废话少说,直接上代码吧. 2.效果实现 代码内容比较简单,所以大家可以直接看代码 VerificationCodeInput.java /** * @author hydCoder * @date 2017/9/22 14:39 * @desc 输入验证码的自定义view * @email hyd_coder@163.co

  • JavaScript实现简单的图片切换功能(实例代码)

    代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图片切换</title> <style> *{ margin: 0; padding: 0; } .box{ width: 200px; height: 300px; margin: 50px auto; padding: 20p

随机推荐