52

Just like the title implies, is there any difference? I was using pygame.display.flip and I saw on the Internet that instead of using flip they used pygame.display.update. Which one is faster?

Danis
  • 344
  • 2
  • 13
Yubin Lee
  • 814
  • 2
  • 9
  • 26

4 Answers4

84

The main difference between pygame.display.flip and pygame.display.update is, that

  • display.flip() will update the contents of the entire display
  • display.update() allows to update a portion of the screen, instead of the entire area of the screen. Passing no arguments, updates the entire display

To tell PyGame which portions of the screen it should update (i.e. draw on your monitor) you can pass a single pygame.Rect object, or a sequence of them to the display.update() function. A Rect in PyGame stores a width and a height as well as a x- and y-coordinate for the position.

PyGame's built-in dawning functions and the .blit() method for instance return a Rect, so you can simply pass it to the display.update() function in order to update only the "new" drawn area.

Due to the fact that display.update() only updates certain portions of the whole screen in comparison to display.flip(), display.update() is faster in most cases.

elegent
  • 3,857
  • 3
  • 23
  • 36
  • 3
    I wonder why they didn't just make it `update()` with optional arguments. – Robo Robok Sep 07 '20 at 16:16
  • 1
    Because flip might actually work faster due to the use of OpenGL/hardware acceleration. For example, for a simple loop with 3 sprites on a MacBook Pro 2019, pygame's flip gives "insane" 6 FPS compared to 3 FPS for an update of rect regions. – Maximus Sep 27 '20 at 04:08
11

flip will always update the entire screen. update also updates the entire screen, if you don't give arguments. But, if you give surface(s) as arguments, it will update only those surfaces. So it can be faster, depending on how many surfaces you give it and their width and height.

lain
  • 448
  • 4
  • 8
  • 1
    So if I have more than 1 surface object, `update` will be faster than `flip` ? – Yubin Lee Mar 28 '15 at 12:41
  • 1
    Imagine you have an image and you blit it. Instead of updating the whole screen, if you update the surface corresponding to the image, it will be faster (if the image is smaller than the screen). – lain Mar 28 '15 at 17:56
1
  • pygame.display.flip() updates the whole screen.
  • pygame.display.update() updates only specific section but with no arguments works similar to the pygame.display.flip().
0

If you are doing double-buffering then you want to be using flip(). With only a single buffer either will work, and single buffering is what you are using unless you specifically create a double buffered window, such as this:

pygame.display.set((w, h), pygame.DOUBLEBUF)

Speed will be the same really if you are doing a single, full display update once a frame, so doesn't matter really which you use in single buffer mode.

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
Jason Hoffoss
  • 81
  • 1
  • 2