Screen only updates when I check for user input pygame

后端 未结 2 1012
余生分开走
余生分开走 2021-01-25 13:04

I\'m using pygame and updating to the screen every loop of the main loop. What I don\'t understand is nothing will update until I add a for loop looking for events, then suddenl

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 13:12

    When you call pygame.event.get() (or pump()), pygame processes all events that your window manager send to the window managed by pygame.

    You don't see these events as they are not returned by get(), but pygame handles them internally. These events could be WM_PAINT on Windows or Expose on Linux (IIRC pygame uses Xlib), or other events (I guess you could look them up in pygame's source code).

    E.g. if you run pygame on Windows, Pygame has to call Windows' GetMessage function, otherwise:

    If a top-level window stops responding to messages for more than several seconds, the system considers the window to be not responding and replaces it with a ghost window that has the same z-order, location, size, and visual attributes. This allows the user to move it, resize it, or even close the application. However, these are the only actions available because the application is actually not responding.

    So the typical behaviour if you don't let pygame process the events is that it will basically run, but the mouse cursor will change to the busy cursor and you can't move the window before it will eventually freeze.

    If you run pygame on other systems, e.g. Linux, you only see a black screen. I don't know the internals of the message loop when pygame runs on Linux, but it's similiar to the Windows message loop: you have to process the events in the queue to have pygame call Xlib's XNextEvent function (IIRC) to give the window manager a chance to draw the window.

    See e.g. Message loop in Microsoft Windows and/or Xlib for more information on that topic.

提交回复
热议问题