pygame怎样做页面切换功能,思路方法是什么
Admin 2022-07-30 群英技术资讯 1252 次浏览
这篇文章主要介绍了pygame怎样做页面切换功能,思路方法是什么相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇pygame怎样做页面切换功能,思路方法是什么文章都会有所收获,下面我们一起来看看吧。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版本,两个版本,任君选择,比较简单,大家将就着看看啊。后面会有修订和更多有趣的案例,欢迎关注,感谢支持!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
题目[1]:格式输出练习。在交互式状态下完成以下练习。运行结果截图:题目[2]:格式输出练习。在.py的文件中完成以下练习代码:num=100print('%dtohexis%x'%(
不管是在学习还是工作过程中,人都会犯错。虽然Python的语法简单、灵活,但也一样存在一些不小的坑,一不小心,初学者和资深Python
Pandas 是 Python 语言的一个扩展程序库,能用来数据分析。而且pandas还提供了大量能帮助我们快速便捷地处理数据的函数和方法。我们有时候需要对excel表的列做操作,使用pandas就是能实现我们想要的功能。下面我们就一起来看看使用pandas如何调整列的顺序。
这篇文章主要介绍了python语言的优势是什么,从各个方面做了分析,需要的朋友们可以参考下
类(class)是python中很重要的一个概念,也是我们面象对象编程中最重要的概念主之一,这篇文章主要给大家介绍了关于python中class类与方法用法的相关资料,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008