2

I'm having trouble with the framerate in my game. I've set it to 60 but it only goes to ~25fps. This was not an issue before displaying the background (was fine with only win.fill(WHITE)). Here is enough of the code to reproduce:

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()
Låne Book
  • 73
  • 7
  • 3
    It is sufficient to create the font once, rather than every time when a text is drawn. Move `font = pygame.font.SysFont('Comic Sans MS', 25, True)` to the begin of the application (somewhere after `pygame.init()` and before the main application loop) – Rabbid76 Dec 12 '19 at 20:17
  • Definitely helped, don't know why I didn't do that to start with. Displaying the image still cripples the framerate t(get up to 16x more with win.fill). – Låne Book Dec 13 '19 at 08:42
  • 1
    Does it change if you do `bg = pygame.image.load('images/bg.jpg').convert()`? – Rabbid76 Dec 13 '19 at 08:45
  • Did now, works perfectly. Thanks – Låne Book Dec 13 '19 at 10:02
  • 1
    Is the issue solved? Is the answer acceptable? (check mark to the left of the answer). – Rabbid76 Dec 20 '19 at 16:39
  • Forgot about it, and stack overflow isn't on my main Email. Makred all the helpfull respenses, wil make sure to remember in the future :) – Låne Book Dec 21 '19 at 22:22

2 Answers2

3

Ensure that the background Surface has the same format as the display Surface. Use convert() to create a Surface that has the same pixel format. That should improve the performance, when the background is blit to the display, because the formats are compatible and blit do not have to do an implicit transformation.

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

Furthermore, it is sufficient to create the font once, rather than every time when a text is drawn. Move font = pygame.font.SysFont('Comic Sans MS', 25, True) to the begin of the application (somewhere after pygame.init() and before the main application loop)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Good advice. Here's another one: cache the Surfaces you create with `font.render()`. Rendering text is quite expensive in terms of performance cost. – sloth Dec 13 '19 at 11:23
  • 1
    @sloth Yes of course. But sadly the text seems to be dynamic (fps), at least in this case. – Rabbid76 Dec 13 '19 at 11:31
0

Instead use screen.blit(pygame.image.load(picture.png)) Just image = pygame.image.load(picture.png) then screen.blit(image)

( if you keep loading your pictures continuously it will get lag )

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 05 '21 at 11:32