如何缩放图像Pygame的屏幕大小 [英] How to scale images to screen size in Pygame

查看:3442
本文介绍了如何缩放图像Pygame的屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我怎么会去缩放在Pygame的项目图像的大小屏幕的分辨率。例如,设想以下情形假定窗口显示模式暂且;我想全屏幕将是相同的:

I was wondering how I would go about scaling the size of images in Pygame projects 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:

- 我有一个1600x900的背景图像这当然显示本机在1600x900的窗口

-I have a 1600x900 background image which of course displays natively in a 1600x900 window

- 在1280×720的窗口,我可以明明只是扩展这个图片矩形为1280×720

-In a 1280x720 window I can obviously just scale this images' Rect to 1280x720

- 什么会发生,但是如果我需要补充,说在x一个300×300像素的图片,Y 1440,860(例如大小)的大小以适应与原来的1600x900的背景是什么?当然了1600x900的我当然可以使用图像本身但对于更小/大的窗口大小?

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

基本上,我怎么缩放图像窗口大小,然后相应的位置呢?我想必须有一个非常简单的自动化方法但现在我不出去,很坦率地说没有时间寻找它...

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...

在此先感谢,
Ilmiont

Thanks in advance, Ilmiont

推荐答案

您可以缩放与图像 pygame.transform.scale

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

您可以再拿到的边框图片

rect = picture.get_rect()

和与移动图片

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

其中,屏幕设置的东西,如

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


要允许您的小部件,以适应不同的屏幕尺寸,
你可以使显示器
可调整大小

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

这篇关于如何缩放图像Pygame的屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆