pygame 오 피 스 를 모방 한 페이지 전환 기능(전체 코드)
(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 페이지 전환 에 관 한 자 료 는 다른 관련 글 을 주목 하 십시오!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
완전히 처음부터 시작하는 보드 게임의 프로그래밍과 기계 학습 [4 일째]평소 Python의 기본적인 사용법을 배웠으므로, 드디어 Pygame을 사용하여 게임 제작의 입구로 발길을 가고 싶습니다. Pygame은 Simple DirectMedia Layer (SDL)라고 불리는 다른 게임 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.