几秒钟后Pygame窗口没有响应 [英] Pygame Window not Responding after few seconds

查看:90
本文介绍了几秒钟后Pygame窗口没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码使窗口在启动时没有响应..我想做刽子手游戏,我完成了逻辑,我只是​​想让窗口弹出而它没有响应.同样,当我运行程序时,当我输入字母并输入另一个字母时,它会删除前一个字母,并用下划线写下新字母.我怎样才能让它保留上一个字母并用新字母打印它?

The below code makes the window not respond when started.. I wanted to make hangman game and I got the logic done and I was just trying to make the window pop up and its not responding. Also when I run the program, when I type in the letter and type another one then it erases the previous letter a writes the new letter with the underscores. How can I make it so that it keeps the previous letter and prints it with the new letter?

import pygame
pygame.init()

running  = True

window_width = 600
window_height = 600
window = pygame.display.set_mode((window_width, window_height))

clock = pygame.time.Clock()

word = "something"
while running:
    dt = clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        


    answer = ""
    guessed = []

    guessed.append(input("write your letter here -> "))

    for i in word:
        
        if i in guessed:
            answer += i + " "
        else:
            answer += "_ "

    print(answer)
    answer = ""
pygame.quit()

推荐答案

您的游戏没有响应,因为您要求 input 在应用程序循环内.input 停止应用程序循环并等待输入被确认.如果停止应用程序循环,窗口将停止响应.使用 KEYDOWN 事件在 PyGame 中获取输入(参见 <代码>pygame.event):

Your game is not responding, because you ask for an input inside the application loop. input stops the application loop and waits for input to be confirmed. If you stop the application loop, the window stops responding. Use the KEYDOWN event to get an input in PyGame (see pygame.event):

for event in pygame.event.get():
    # [...]

    if event.type == pygame.KEYDOWN:
        guessed.append(event.unicode)

guessed 必须在应用程序循环之前初始化.不要在循环中重置它:

guessed has to be initialized before the application loop. Don't reset it in the loop:

import pygame
pygame.init()

running  = True

window_width = 600
window_height = 600
window = pygame.display.set_mode((window_width, window_height))

clock = pygame.time.Clock()

word = "something"
guessed = []

answer = ""
for c in word:
    answer += c + " " if c in guessed else "_ "
print(answer)

while running:
    dt = clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            guessed.append(event.unicode)
            answer = ""
            for c in word:
                answer += c + " " if c in guessed else "_ "
            print(answer)
   
pygame.quit()

这篇关于几秒钟后Pygame窗口没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆