gpt4 book ai didi

python - 在pygame中制作图像 "fit to screen"?

转载 作者:太空宇宙 更新时间:2023-11-03 14:20:34 26 4
gpt4 key购买 nike

所以基本上我在一个设计漫画书查看器的项目中使用 pygame。我已经能够加载图像、显示它、修改它的尺寸,但无法在调整大小时将其适合窗口。

到目前为止,我只是将图像拉伸(stretch)到 Canvas 上,当我调整大小时,它保持在原位,并且一切看起来“有问题”。这是我的来源:https://github.com/averyre/ComicSnake/blob/master/comicsnake.py

具体来说,这个 block :

## The GUI loop.
while 1:
screenWidth, screenHeight = screen.get_size();
pygame.event.wait()
screen.fill(black)
page = pygame.transform.scale(page,[screenWidth, screenHeight]);
screen.blit(page, pagerect)
pygame.display.flip()

任何帮助将不胜感激!

最佳答案

screenSurface (内存中的缓冲区),它在启动时创建并且不会更改大小 - 它不是您调整大小的窗口。

当您调整窗口大小时,它会发送事件 VIDEORESIZE,其中包含字段 event.sizeevent.wevent。 h 是调整大小后窗口的大小。

参见文档:event

来自 pygame.org 维基的示例代码:WindowResizing .
它展示了如何使用VIDEORESIZE来调整屏幕和图像的大小。

import pygame
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((500,500), HWSURFACE|DOUBLEBUF|RESIZABLE)
pic = pygame.image.load("example.png") #You need an example picture in the same folder as this file!
screen.blit(pygame.transform.scale(pic, (500,500)), (0,0))
pygame.display.flip()

while True:
pygame.event.pump()
event = pygame.event.wait()
if event.type == QUIT:
pygame.display.quit()
elif event.type == VIDEORESIZE:
screen = pygame.display.set_mode(event.dict['size'], HWSURFACE|DOUBLEBUF|RESIZABLE)
screen.blit(pygame.transform.scale(pic, event.dict['size']), (0,0))
pygame.display.flip()

关于python - 在pygame中制作图像 "fit to screen"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47975482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com