python自适应图片大小_python – 如何在Pygame中将图像缩放到屏幕尺寸

您可以使用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()

你可能感兴趣的:(python自适应图片大小)