Fonts Pygame曲面大小调整

Fonts Pygame曲面大小调整,fonts,pygame,resize,surface,blit,Fonts,Pygame,Resize,Surface,Blit,当我调整窗口大小时,曲面边界不会像窗口那样调整大小。在起始窗口边界(600x340)的主曲面上进行文本渲染。为什么?另外还有一个渲染图像(为了简化问题,我剪切了这个代码部分)作为背景,它根据新窗口的边界正常渲染(直接渲染到主曲面(win))。所以我认为问题在于额外的表面(文本冲浪)。为什么它们不能调整大小 import pygame import time pygame.init() run=True screen_width=600 screen_height=340 black=(0,0,

当我调整窗口大小时,曲面边界不会像窗口那样调整大小。在起始窗口边界(600x340)的主曲面上进行文本渲染。为什么?另外还有一个渲染图像(为了简化问题,我剪切了这个代码部分)作为背景,它根据新窗口的边界正常渲染(直接渲染到主曲面(win))。所以我认为问题在于额外的表面(文本冲浪)。为什么它们不能调整大小

import pygame
import time
pygame.init()

run=True
screen_width=600
screen_height=340
black=(0,0,0)
white=(255,255,255)
font1=pygame.font.SysFont("arial",45)
text_press=font1.render("press any key to play!",0,(100,0,0))
win=pygame.display.set_mode((screen_width,screen_height),pygame.RESIZABLE)
text_surf=pygame.Surface((screen_width,screen_height),pygame.RESIZABLE)
background=pygame.Surface((screen_width,screen_height),pygame.RESIZABLE)

while run==True:
    pygame.time.delay(16)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run=False
        if event.type == pygame.VIDEORESIZE:
            surface = pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)
            screen_width=event.w
            screen_height=event.h

    win.fill((white))

    background.fill((120,120,100))
    win.blit(background,(0,0))

    text_surf.fill((black))
    text_surf.blit(text_press, (screen_width*0.5-50,screen_height*0.5+100))
    text_surf.set_colorkey(black)
    text_surf.set_alpha(150)
    win.blit(text_surf,(0,0))

    pygame.display.update()
pygame.quit()

text\u surf
background
的大小不会神奇地改变。您需要创建具有新尺寸的新曲面。
pygame.resizeable
标志对没有任何影响(或者它有一种您意想不到的效果)

pygame.Surface((屏幕宽度,屏幕高度),pygame.resizeable)
pygame.RESIZABLE
仅用于。
pygame.Surface
构造函数的flag参数的有效标志是
pygame.HWSURFACE
pygame.SRCALPHA

发生
pygame.VIDEORESIZE
事件时,使用新尺寸创建新曲面:

while run==True:
# [...]
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.VIDEORESIZE:
surface=pygame.display.set_模式((event.w,event.h),pygame.resizeable)
屏幕宽度=事件宽度
屏幕高度=事件高度
text\u surf=pygame.Surface((屏幕宽度、屏幕高度))
背景=pygame.Surface((屏幕宽度、屏幕高度))
# [...]

text\u surf的大小不会神奇地改变。你必须用新的尺寸创建一个新的曲面。谢谢!!!!!!现在它像我预期的那样工作。所以它需要在主循环中重新创建所有曲面。并从主循环之前的创建字符串中删除pygame.Resizeable。