【问题标题】:Making an image "fit to screen" in pygame?在pygame中制作图像“适合屏幕”?
【发布时间】:2017-12-26 07:47:29
【问题描述】:

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

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

具体来说,这个块:

## 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()

任何帮助将不胜感激!

【问题讨论】:

    标签: python image user-interface pygame


    【解决方案1】:

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

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

    见文档:event

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

    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()
    

    【讨论】:

    • 抱歉,我在将其集成到我的代码中时遇到了一些问题。我了解“退出”事件类型及其运作方式,但我无法让我的图像根据窗口调整大小。任何尝试重新创建类似的东西,我都有同样的问题。
    • QUIT 事件并不重要 - 您必须使用 VIDEORESIZE 获取窗口大小并重新创建 screen 并调整图像大小。您可以使用不同的变量将所有代码从我的while True 复制到您的whileloop。
    猜你喜欢
    • 2016-08-09
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2018-09-12
    • 2014-11-04
    相关资源
    最近更新 更多