英文:
Why is pygame.QUIT being checked when the 'X' button can be clicked and the window would be closed?
问题
I am learning Python's pygame module so I can make games and I'm fairly new to it. I am watching a tutorial and in the following code, when the for loop is checking for a pygame.QUIT event the run variable is turned to false and pygame.quit() quits pygame.
My question is, why is the pygame.QUIT being checked when the 'X' button will close the window?
英文:
I am learning Python's pygame module so I can make games and I'm fairly new to it. I am watching a tutorial and in the following code, when the for loop is checking for a pygame.QUIT event the run variable is turned to false and pygame.quit() quits pygame.
My question is, why is the pygame.QUIT being checked when the 'X' button will close the window?
答案1
得分: 3
当按下X按钮时,会创建一个事件对象并添加到事件队列中。可以使用pygame.event.get()
来检索事件对象。type
属性表示事件的类型。如果不检查event.type == pygame.QUIT
并且不设置run = False
,循环将永远不会结束(无限循环),应用程序将永远不会终止,窗口也不会关闭。pygame.quit()
会取消初始化所有pygame模块,这也会取消初始化显示模块并导致窗口关闭。在脚本完成后退出应用程序时,窗口也会关闭。
英文:
When the X button is pressed, an event object is created and added to the event queue. The event objects are retrieved with pygame.event.get()
. The type
property indicates the type of the event.
If you do not check event.type == pygame.QUIT
and do not set run = False
, the loop will never end (endless loop), the application will never terminate and the window will not close.
pygame.quit()
deinitializes all pygame modules, which also deinitializes the display module and causes the window to close. The window is also closed when the application exits after your script is finished.
答案2
得分: 1
"The "X" button does not automatically force the window to close. All it does is send a message to the window saying "the user would like to close you now". It is entirely up to the application to check for that message (in this case, pygame.QUIT
) and take the appropriate action to close itself."
英文:
The "X" button does not automatically force the window to close. All it does is send a message to the window saying "the user would like to close you now". It is entirely up to the application to check for that message (in this case, pygame.QUIT
) and take the appropriate action to close itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论