如何解决如何在pygame中调整窗口大小python3
我正在制作游戏,并且希望能够调整窗口的大小,因此将屏幕设置为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,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,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
我在做什么错? (如果您想要我所有的代码,请告诉我)
解决方法
没有看到您的代码,很难猜测您做错了什么。我的猜测是您尚未使用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,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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。