Screen only updates when I check for user input pygame

最后都变了- 提交于 2020-01-11 10:32:22

问题


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 suddenly all the updating does occur. Why is this?

   def run(self):

        two_pm = get_stand_up_timestamp()

        pygame.init()
        font = pygame.font.Font(None, 72)
        screen = pygame.display.set_mode(self._dimensions)



        before_two = True
        while before_two:

            # Blit the time to the window.
            # Update Screen.
            current_time = datetime.datetime.now()
            text = font.render(f'{current_time.hour} : {current_time.minute} : {current_time.second}', True, (0, 0, 0))
            blit_center = (
                self._dimensions[0] // 2 - (text.get_width() // 2),
                self._dimensions[1] // 2 - (text.get_height() // 2)
            )
            screen.fill((255, 255, 255))
            screen.blit(text, blit_center)
            pygame.display.flip()

            # Get events.
            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        sys.exit()



回答1:


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.




回答2:


No idea why it doesn't work on your end, however when I run

 def run():
    width = 500
    height = 500
    pygame.init()
    font = pygame.font.Font(None, 72)
    screen = pygame.display.set_mode((width, height))

    before_two = True
    while before_two:

        # Blit the time to the window.
        # Update Screen.
        current_time = datetime.datetime.now()
        text = font.render(f'{current_time.hour} : {current_time.minute} :      {current_time.second}', True, (0, 0, 0))
        blit_center = (
            width // 2 - (text.get_width() // 2),
            height // 2 - (text.get_height() // 2)
        )
        screen.fill((255, 255, 255))
        screen.blit(text, blit_center)
        pygame.display.flip()




run()

Everything works fine update wise. The clock ticks every second so it may be something with your version of python or pygame. Try updating them both. Alternately it could be a problem with how you get pass pygame the dimensions of the window with the run(self) and self._dimensions. Trying using static dimensions like I did above and see if that works on your end. Sadly without more code to see how you call run() its difficult to fully debug whats wrong.



来源:https://stackoverflow.com/questions/58560463/screen-only-updates-when-i-check-for-user-input-pygame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!