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

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版本,两个版本,任君选择,比较简单,大家将就着看看啊。后面会有修订和更多有趣的案例,欢迎关注,感谢支持!

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现Pygame中的对话框功能,可以按照以下步骤进行: 1.创建一个WordsOutput类,该类包含对话框的文本,名称和头像等属性。 2.在WordsOutput类的构造函数中,初始化文本,名称和头像等属性,并计算出对话框的起始位置。 3.在游戏循环中,使用pygame.draw.rect()函数绘制对话框的背景,并使用pygame.font.Font()函数创建字体对象。 4.使用字体对象的render()方法将文本渲染为Surface对象,并使用blit()方法将其绘制到对话框中。 5.如果有名称和头像,则使用blit()方法将它们绘制到对话框中。 下面是一个简单的示例代码,演示了如何在Pygame实现对话框功能: ```python import pygame class WordsOutput(): def __init__(self, text, name=None, portrait=None): self.text = text self.name = name self.portrait = portrait self.start_x = 0 self.start_y = 0 if self.portrait != None: self.start_x = self.portrait.get_size()[0] if self.name != None: self.start_y = self.name.get_size()[1] pygame.init() screen = pygame.display.set_mode((640, 480)) font = pygame.font.Font(None, 30) white = (255, 255, 255) black = (0, 0, 0) text = "Hello, world!" name = font.render("Player", True, black) portrait = pygame.image.load("portrait.png") dialogue = WordsOutput(text, name, portrait) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.fill(white) pygame.draw.rect(screen, black, (10, 10, 620, 100), 2) text_surface = font.render(dialogue.text, True, black) screen.blit(text_surface, (20 + dialogue.start_x, 20 + dialogue.start_y)) if dialogue.name != None: screen.blit(dialogue.name, (20, 20)) if dialogue.portrait != None: screen.blit(dialogue.portrait, (20, 20 + dialogue.start_y)) pygame.display.update() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值