0

我正在制作游戏,我希望能够调整窗口大小,所以我将屏幕设置为screen = pygame.display.set_mode((width, height), pygame.RESIZABLE). 我有一个函数调用resize()也可以更新所有图像的位置。

def resize():
    width, height = pygame.display.get_surface().get_size()
    cookie_surface = pygame.image.load('/xxx/xxx/xxximages/cookie.png')
    cookie_surface = pygame.transform.scale(cookie_surface, (275, 275))
    cookie_rect = cookie_surface.get_rect(center = (width/2, height/2))

#auto clicker image
    auto_clicker = pygame.image.load('xxx/xxx/xxx/images/mouse_cursor.png')
    auto_clicker = pygame.transform.scale(auto_clicker, (480,360))
    auto_clicker_rect = auto_clicker.get_rect(topleft = (width-350, -100))

#farm image
    farm_img = pygame.image.load('/xx/xxx/xxx/images/farm.png')
    farm_img = pygame.transform.scale(farm_img, (480,360))
    farm_rect = farm_img.get_rect(topleft = (width-350, -85))

#save button image
    save_surface = pygame.image.load('/xxx/xxx/xxx/images/save.png')
    save_surface = pygame.transform.scale(save_surface, (480,360))
    save_rect = save_surface.get_rect(center = (130, height+78))

#bg
    bg = pygame.image.load("/xxx/xxx/xxx/images/bg.jpg")
    bg = pygame.transform.scale(bg, (width, height))

当我运行代码时,我收到此错误: pygame.error: Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface

我究竟做错了什么?(如果你想要我所有的代码,请告诉我)

4

1 回答 1

1

没有看到你的代码,很难猜出你做错了什么。我的猜测是您没有使用pygame.display.set_mode(…). 如果您使用的是 Pygame 2 的开发版本,则无需重新创建主表面。

这是一个显示如何调整窗口大小的示例:

import pygame

pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
image = pygame.Surface((120, 100), pygame.SRCALPHA)
image.fill(pygame.color.Color("royalblue"))
center_pos = (width // 2, height // 2)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.VIDEORESIZE:
            width, height = event.dict["size"]
            # recreate screen object required for pygame version 1
            screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
            # TODO: resize image if needed
            center_pos = (width // 2, height // 2)
    screen.fill(0)
    screen.blit(image, image.get_rect(center=center_pos))
    pygame.display.update()
于 2020-10-20T13:30:42.277 回答