简体   繁体   English

Pygame 计算时无响应

[英]Pygame not responding when Doing calculations

I am currently working on a AI v AI game in pygame, I am just finishing it but one of the functions doesn't work as expected.我目前正在 pygame 中开发一款 AI v AI 游戏,我刚刚完成它,但其中一个功能无法按预期工作。 It is something like this (this is in the pygame.event.get() loop):它是这样的(这是在 pygame.event.get() 循环中):

if event.type == right_input:
    for e in range(100):
        data = compute_data(variables)
        re_draw_window(data)

The compute_data function can take 0.5 seconds but after something like 30 times the pygame window stops to respond and after 20 seconds when it returns to the event loop it shows the final result. compute_data function 可能需要 0.5 秒,但在 pygame window 停止响应并在 20 秒后返回事件循环时,它会显示最终结果循环。

Th issue is causes by the loop in the loop.这个问题是由循环中的循环引起的。 compute_data and re_draw_window is executed 100 times in a row. compute_datare_draw_window执行 100 次。 Meanwhile the window does not respond, because the events are not handled.同时 window 没有响应,因为没有处理事件。 The inner loop blocks the application, till the loop terminates.内部循环阻塞应用程序,直到循环终止。

Change the setup and initialize a variable count before the application loop and increment count in the loop.在应用程序循环之前更改设置并初始化变量count ,并在循环中增加count
To start the process once by an input, then add a variable start to the application and set the variable in the input event:要通过输入启动一次流程,然后将变量start添加到应用程序并在输入事件中设置变量:

start = False
count = 0
while True:
    # [...]

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

        if event.type == right_input:
            # start the process
            start = True


    if start:
        # run the process
        if count < 100:
            data = compute_data(varibals)
            re_draw_window(data)
            count += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM