我在游戏中遇到帧率问题。我将其设置为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()
答案 0 :(得分:2)
确保背景Surface与显示Surface具有相同的格式。使用convert()
创建具有相同像素格式的Surface。当显示背景为blit
时,应该可以提高性能,因为格式兼容并且blit
不必进行隐式转换。
bg = pygame.image.load('images/bg.jpg').convert()
此外,一次创建字体就足够了,而不是每次绘制文本时就创建一次。将font = pygame.font.SysFont('Comic Sans MS', 25, True)
移动到应用程序的开头(在pygame.init()
之后和主应用程序循环之前的某个位置)