pygame 오 피 스 를 모방 한 페이지 전환 기능(전체 코드)

1.가장 간단 한 전환 기능
(1)소스 코드

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()
(2)효과
在这里插入图片描述
(3)해석
실제 runimage 와 nextimage 를 사용 하여 두 개의 그림 을 저장 한 다음 nextimage 를 붙 이 고 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)
2.동적 스크린 절단 기능 실현
 (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

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()
(2)왼쪽으로 전환 효과
在这里插入图片描述
3.랜 덤 효과 실현
상하 좌우 효과 실현

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()
효과
在这里插入图片描述
다섯 번 째 버 전
(1)핵심 코드 수정

 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))
(2)전체 코드

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()
(3)다른 효과
在这里插入图片描述
소결
Ok,V1 과 V2 버 전,두 가지 버 전,임 군 이 선택 하 는 것 이 비교적 간단 합 니 다.여러분 이 아 쉬 운 대로 보 세 요.뒤에 수정 과 더 많은 재 미 있 는 사례 가 있 을 것 입 니 다.관심 가 져 주 셔 서 감사합니다.
이상 은 pygame 이 office 와 유사 한 페이지 전환 기능 을 실현 하 는 상세 한 내용 입 니 다.pygame 페이지 전환 에 관 한 자 료 는 다른 관련 글 을 주목 하 십시오!

좋은 웹페이지 즐겨찾기