|
10鱼币
本帖最后由 库库的斯蒂芬 于 2022-5-11 14:40 编辑
跟随小甲鱼的视频教程学习pygame,但是遇到了边框调整与全屏的问题。边框调整的功能可以运行,但是无法全屏了,只要全屏就会删一下然后回到窗口化。不知道什么原因有没有大佬指点一二。import pygame
import sys
from pygame.locals import *
pygame.init()
size=1200,800
width,height=size
speed=[-2,1]
bg=(255,255,255)
screen=pygame.display.set_mode(size,RESIZABLE)
pygame.display.set_caption('你好')
font=pygame.font.Font(None, 20)
#加载图片
fig=pygame.image.load('curry.jpg')
#获取图像的位置矩形
pos=fig.get_rect()
r_head=fig
l_head=pygame.transform.flip(fig, True, False)
fsc=False
while(True):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
if event.type==KEYDOWN:
if event.key==K_LEFT:
speed=[-1,0]
fig=l_head
if event.key==K_RIGHT:
fig=r_head
speed=[1,0]
if event.key==K_UP:
speed=[0,-1]
if event.key==K_DOWN:
speed=[0,1]
#全屏
if event.key==K_0:
fsc=not fsc
if fsc:
screen=pygame.display.set_mode((1920,1080),FULLSCREEN | HWSURFACE,RESIZABLE)
size=(1902,1080)
width,height=size
else:
screen=pygame.display.set_mode(size)
#用户修改边框
if event.type==VIDEORESIZE:
size=event.size
width,height=size
screen=pygame.display.set_mode(size,RESIZABLE)
#移动图像
pos=pos.move(speed)
print('bbb',size)
if pos.left<0 or pos.right>width:
fig=pygame.transform.flip(fig, True, False)
speed[0]=-speed[0]
if pos.top<0 or pos.bottom>height:
speed[1]=-speed[1]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(fig,pos)
#更新画面
pygame.display.flip()
#延迟
pygame.time.delay(100)
边框调整那代码有点问题,删掉就可以正常大小缩放,但是没办法调整窗口了,参考代码:
import pygame
import sys
from pygame.locals import *
pygame.init()
size = 1200, 800
width, height = size
speed = [-2, 1]
bg = (255, 255, 255)
screen = pygame.display.set_mode(size, RESIZABLE)
pygame.display.set_caption('你好')
font = pygame.font.Font(None, 20)
# 加载图片
fig = pygame.image.load('curry.jpg')
# 获取图像的位置矩形
pos = fig.get_rect()
r_head = fig
l_head = pygame.transform.flip(fig, True, False)
fsc = False
while (True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
speed = [-1, 0]
fig = l_head
if event.key == K_RIGHT:
fig = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]
# 全屏
if event.key == K_0:
fsc = not fsc
if fsc:
screen = pygame.display.set_mode((1920, 1080), FULLSCREEN | HWSURFACE, RESIZABLE)
size = (1200, 800)
width, height = size
else:
screen = pygame.display.set_mode(size)
# 移动图像
pos = pos.move(speed)
print('bbb', size)
if pos.left < 0 or pos.right > width:
fig = pygame.transform.flip(fig, True, False)
speed[0] = -speed[0]
if pos.top < 0 or pos.bottom > height:
speed[1] = -speed[1]
# 填充背景
screen.fill(bg)
# 更新图像
screen.blit(fig, pos)
# 更新画面
pygame.display.flip()
# 延迟
pygame.time.delay(100)
|
最佳答案
查看完整内容
边框调整那代码有点问题,删掉就可以正常大小缩放,但是没办法调整窗口了,参考代码:
|