当win.blit()后台pygame时滞后



我在游戏中遇到帧率问题。我已将其设置为 60,但它只设置为 ~25fps。在显示背景之前这不是问题(只有win.fill(WHITE)很好(。以下是足以重现的代码:

import os, pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50, 50)
pygame.init()
bg = pygame.image.load('images/bg.jpg')
FPS = pygame.time.Clock()
fps = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
winW = 1227
winH = 700
win = pygame.display.set_mode((winW, winH))
win.fill(WHITE)
pygame.display.set_icon(win)

def redraw_window():
#win.fill(WHITE)
win.blit(bg, (0, 0))
win.blit(text_to_screen('FPS: {}'.format(FPS.get_fps()), BLUE), (25, 50))
pygame.display.update()

def text_to_screen(txt, col):
font = pygame.font.SysFont('Comic Sans MS', 25, True)
text = font.render(str(txt), True, col)
return text

run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_window()
FPS.tick(fps)
pygame.quit()

确保背景图面与显示图面具有相同的格式。使用convert()创建具有相同像素格式的 Surface。当背景blit显示器时,这应该会提高性能,因为格式是兼容的,blit不必进行隐式转换。

bg = pygame.image.load('images/bg.jpg').convert()

此外,创建字体一次就足够了,而不是每次绘制文本时。font = pygame.font.SysFont('Comic Sans MS', 25, True)移动到应用程序的开头(pygame.init()之后和主应用程序循环之前的某个位置(

改用screen.blit(pygame.image.load(picture.png))只是image = pygame.image.load(picture.png)然后screen.blit(image)

(如果您不断加载图片,则会延迟(

最新更新