簡體   English   中英

僅當我檢查用戶輸入 pygame 時屏幕才會更新

[英]Screen only updates when I check for user input pygame

我正在使用 pygame 並在主循環的每個循環中更新到屏幕。 我不明白的是,在我添加一個尋找事件的 for 循環之前,什么都不會更新,然后突然所有更新都發生了。 為什么是這樣?

   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()


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

您看不到這些事件,因為它們不是由get()返回的,但 pygame 在內部處理它們。 這些事件可能是 Windows 上的WM_PAINT或 Linux 上的Expose (IIRC pygame 使用 Xlib),或其他游戲事件(我猜你可以在源代碼中查找它們)

例如,如果您在 Windows 上運行 pygame,則 Pygame 必須調用 Windows 的GetMessage ZC1C425260,否則 4C17A:4C17A

如果頂級 window 停止響應消息超過幾秒鍾,系統會認為 window 沒有響應,並將其替換為具有相同 z 順序、大小和視覺屬性位置的幽靈 window。 這允許用戶移動它、調整它的大小,甚至關閉應用程序。 但是,這些是唯一可用的操作,因為應用程序實際上沒有響應。

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.

如果您在其他系統上運行 pygame,例如 Linux,您只會看到黑屏。 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有機會畫出 window。

有關該主題的更多信息,請參見Microsoft Windows和/或Xlib中的消息循環。

不知道為什么它對你不起作用,但是當我跑步時

 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()

一切正常更新明智。 時鍾每秒滴答一次,因此可能與您的 python 或 pygame 版本有關。 嘗試更新它們。 或者,您如何通過 pygame 使用 run(self) 和 self._dimensions 傳遞 window 的尺寸可能是一個問題。 嘗試像我上面所做的那樣使用 static 尺寸,看看這是否適用於您。 遺憾的是,沒有更多代碼來查看您如何調用 run() 很難完全調試出什么問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM