Python 如何在 Pygame 中将图像缩放到屏幕大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20002242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:13:47  来源:igfitidea点击:

How to scale images to screen size in Pygame

pythonimagebackgroundpygamescaling

提问by Ilmiont

I was wondering how I would go about scaling the size of images in pygameprojects to the resolution of the screen. For example, envisage the following scenario assuming windowed display mode for the time being; I assume full screen will be the same:

我想知道如何将pygame项目中的图像大小缩放到屏幕分辨率。例如,假设以下场景暂时假设窗口显示模式;我假设全屏将是相同的:

  • I have a 1600x900background image which of course displays natively in a 1600x900window

  • In a 1280x720window I can obviously just scale this images' rect to 1280x720

  • What happens, however if I need to add, say a 300x300 pximage at x,y 1440,860(example sizes) that is sized to fit with the original 1600x900background? Of course for the 1600x900I can of course use the image natively but what about the smaller/larger window sizes?

  • 我有一个1600x900背景图像,它当然显示在一个1600x900窗口中

  • 在一个1280x720窗口中,我显然可以将这个图像的矩形缩放到1280x720

  • 但是,如果我需要添加(示例尺寸)的300x300 px图像,该图像的x,y 1440,860尺寸适合原始1600x900背景,会发生什么情况?当然,对于1600x900我当然可以本地使用图像但是更小/更大的窗口大小呢?

Basically, how do I scale images to the window size and then position them accordingly? I guess there must be a REALLY easy automated method but right now I can't figure it out and quite frankly haven't got time to search for it...

基本上,如何将图像缩放到窗口大小,然后相应地定位它们?我想一定有一个非常简单的自动化方法,但现在我无法弄清楚,坦率地说,没有时间去寻找它......

Thanks in advance, Ilmiont

提前致谢, Ilmiont

回答by unutbu

You can scale the image with pygame.transform.scale:

您可以使用以下方法缩放图像pygame.transform.scale

import pygame
picture = pygame.image.load(filename)
picture = pygame.transform.scale(picture, (1280, 720))

You can then get the bounding rectangle of picturewith

然后,您可以得到的边框picture

rect = picture.get_rect()

and move the picture with

并移动图片

rect = rect.move((x, y))
screen.blit(picture, rect)

where screenwas set with something like

在哪里screen设置了类似的东西

screen = pygame.display.set_mode((1600, 900))


To allow your widgets to adjust to various screen sizes, you could make the display resizable:

为了让您的小部件适应各种屏幕尺寸,您可以调整显示大小

import os
import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((500, 500), HWSURFACE | DOUBLEBUF | RESIZABLE)
pic = pygame.image.load("image.png")
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()

回答by furas

If you scale 1600x900 to 1280x720 you have

如果您将 1600x900 缩放到 1280x720

scale_x = 1280.0/1600
scale_y = 720.0/900

Than you can use it to find button size, and button position

比您可以使用它来查找按钮大小和按钮位置

button_width  = 300 * scale_x
button_height = 300 * scale_y

button_x = 1440 * scale_x
button_y = 860  * scale_y

If you scale 1280x720 to 1600x900 you have

如果您将 1280x720 缩放到 1600x900

scale_x = 1600.0/1280
scale_y = 900.0/720

and rest is the same.

休息是一样的。



I add .0to value to make float- otherwise scale_x, scale_ywill be rounded to integer- in this example to 0(zero) (Python 2.x)

我添加.0值以生成float- 否则scale_xscale_y将四舍五入为integer- 在本例中为0(零)(Python 2.x)