surface.fill 不适用于 pygame 中的 pygame.Surface 变量

surface.fill doesn't work in with a pygame.Surface variable in pygame

所以,我一直在尝试创建一个小代码,可以使用 pygame.RESIZABLE 缩放我屏幕上的图像,以便能够让我的游戏 windows 随心所欲地调整大小而不会破坏任何垫代码里面的东西。我发现解决这个问题的方法是将我游戏中的所有内容都放在 pygame.Surface 上,然后我会缩放以适应我的 pygame.display 一切正常但是当我使用 game.fill('black') 它似乎没有填满我的屏幕,我在屏幕上绘制的用于测试缩放比例的正方形也没有移动。我知道是 game.fill('black') 行导致了问题,因为如果我删除该行,正方形会自行缩放,但屏幕不会刷新。请帮忙。

# Setup
import pygame
from sys import exit

pygame.init()

# Settings
tile_size = 64
game_width = 1200
game_height = 700
screen_width = game_width
screen_height = game_height

screen = pygame.display.set_mode((screen_width,screen_height),pygame.RESIZABLE)
game = pygame.Surface((game_width,game_height))
pygame.display.set_caption('Image resize')
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.VIDEORESIZE:
            screen_width = screen.get_height() * screen_width / screen_height
            screen_height = screen.get_height()
            game = pygame.transform.scale(game,(screen_width,screen_height))

    screen.fill('black')
    game.fill('black') # This is the line that I think isn't working try commenting it out to see that the rest of the code is working fine just not refreshing which is the purpose of this line

    pygame.draw.rect(game,'red',(tile_size,tile_size,tile_size,tile_size))
    screen.blit(game,(0,0))

    pygame.display.update()
    clock.tick(60)

当 window 大小发生变化时,仅缩放 game Surface 是不够的 您必须缩放 game _Surface每一帧。
场景在 1200 x 700 Surface 上的每一帧中绘制。您必须将此 Surface 缩放到每个帧中 window 的大小。更准确地说,您必须在场景发生变化的每一帧中缩放和 blit 游戏表面。

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.VIDEORESIZE:
            screen_width = screen.get_height() * screen_width / screen_height
            screen_height = screen.get_height()
            screen = pygame.display.get_surface()
            
    game.fill('black')
    pygame.draw.rect(game,'red',(tile_size,tile_size,tile_size,tile_size))
    
    scaled_game = pygame.transform.scale(game,(screen_width,screen_height))
    screen.blit(scaled_game, (0,0))

    pygame.display.update()
    clock.tick(60