为什么pygame窗口变得无反应?

发布于 2025-02-11 05:30:08 字数 763 浏览 0 评论 0原文

在“ pygame循环”中,我正在尝试询问用户输入,但是当我运行程序时,如果将鼠标悬停在它上面或单击任何地方,则pygame窗口会变得无响应。有人知道怎么了吗?

import pygame

win = pygame.display.set_mode((600, 600))
win.fill((240, 240, 240))  #white
pygame.display.update()

#Game loop
quit = False
while quit == False:

    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            exit()

    u_input = input("Enter 'q' to quit or 'n' to fill the window with navy: ")

    if u_input == 'q':
        quit = True

    elif u_input == 'n':
        win.fill((60, 55, 100))  #navy
        pygame.display.update()

无反应的pygame窗口窗口映像

我使用的是Visual Studio代码

In a 'Pygame loop', I'm trying to ask for user input but when I run the program, the pygame window becomes unresponsive if I hover my mouse over it or click anywhere. Does anyone know what's going wrong?

import pygame

win = pygame.display.set_mode((600, 600))
win.fill((240, 240, 240))  #white
pygame.display.update()

#Game loop
quit = False
while quit == False:

    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            exit()

    u_input = input("Enter 'q' to quit or 'n' to fill the window with navy: ")

    if u_input == 'q':
        quit = True

    elif u_input == 'n':
        win.fill((60, 55, 100))  #navy
        pygame.display.update()

Unresponsive pygame window image

The IDE I'm using is Visual Studio Code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2025-02-18 05:30:08

不幸的是,这只是Pygame的本质。当您要求输入时,程序将停止并等待用户输入某些内容,以防止pygame.diplay.flip()发生。

我可以想到两种解决此问题的方法。使用,线程,一个用于PowerShell(终端),一个用于Pygame应该可以使用,但是我根本不熟悉这一点,因此您需要自己研究。

另一个解决方案是聆听用户输入,而不是使用终端提示

#Game loop
quit = False
while quit == False:

    for e in pygame.event.get():

        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_q:
                quit = True
            if e.key == pygame.K_n:
                win.fill((60, 55, 100))  #navy

pygame.display.update()

Unfortunately, this is just a nature of pygame. When you ask for an input, the program stops and waits for the user to input something, preventing the pygame.diplay.flip() from occuring.

There is two ways I can think of to fix this. Using, threads, one for powershell (terminal) and one for pygame should work, however I'm not familiar with that at all, so you would need to research for yourself.

A different solution is to listen for user input instead of using terminal prompts

#Game loop
quit = False
while quit == False:

    for e in pygame.event.get():

        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_q:
                quit = True
            if e.key == pygame.K_n:
                win.fill((60, 55, 100))  #navy

pygame.display.update()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文