pygame.time.wait() 使窗口冻结 [英] pygame.time.wait() makes the window freez

查看:46
本文介绍了pygame.time.wait() 使窗口冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的动画来处理绘制矩形的位置,当我单击表面时,矩形向左滑动并等待 2 秒钟,然后以向右滑动的方式重新绘制它,但是如果我缩小或移动窗口并尝试在等待时刻窗口冻结.

I have a simple animation to handle where I draw a rectangle and when I click on the surface, the rectangle slid left and wait for 2 seconds and then redraw it with a slid motion to the right, but if I reduce or move the window and make a try the window freezes on the waiting moment.

代码如下:

import sys, pygame
from pygame.locals import *

pygame.init()

FPS       = 30
BGCOLOR   = (255, 255, 255)
RECTCOLOR = (10, 0, 199)
DS        = pygame.display.set_mode( (200,200) )
CLOCK     = pygame.time.Clock()
pygame.display.set_caption('Demo')

def main():
    DS.fill(BGCOLOR)
    pygame.draw.rect(DS, RECTCOLOR, (50, 50, 100, 100))
    pygame.display.update()

    while 1:
        ev = pygame.event.wait()
        if ev.type == QUIT:
            pygame.quit()
            sys.exit()
        elif ev.type == MOUSEBUTTONUP:
            animation()


def animation():
    pygame.event.set_blocked(MOUSEBUTTONUP)
    for x in range(10, 101, 10):
        pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))
        pygame.display.update()
        CLOCK.tick(FPS)
    pygame.time.wait(2000) # 2 seconds
    for x in range(10, 101, 10):
        pygame.draw.rect(DS, RECTCOLOR, (50,50,x,100))
        pygame.display.update()
        CLOCK.tick(FPS)
    pygame.event.set_allowed(MOUSEBUTTONUP)

if __name__ == '__main__':
    main()

为什么我们有这种行为?,也许系统在暂停时刻和第二个 for 循环中的滴答声之间没有同步

Why we have this sort of behavior ?, maybe the system don't synchronize between the pause moment and the ticks in the second for loop

PS:我使用的是 Windows 7,抱歉我的英语不是我的母语

PS: i'm on windows 7 and sorry for my english it's not my native language

推荐答案

两件事:

当你像这样运行一个循环时:

When you run a loop like this:

for x in range(10, 101, 10):
        pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))
        pygame.display.update()

您绘制动画,但 pygame 没有机会处理它从您的操作系统获取的消息.为避免这种情况,您应该在循环内调用 pygame.event.pump()pygame.event.get().

you draw your animation, but pygame has no opportunity to handle the messages it gets from your operation system. To avoid that, you should call pygame.event.pump() or pygame.event.get() inside the loop.

来自文档:

pygame.event.pump()

对于游戏的每一帧,您都需要对事件队列进行某种调用.这确保您的程序可以在内部与操作系统的其余部分进行交互.如果您没有在游戏中使用其他事件函数,则应调用 pygame.event.pump() 以允许 pygame 处理内部操作.

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.

如果您的程序通过其他 pygame.event 函数持续处理队列上的事件,则不需要此函数.

This function is not necessary if your program is consistently processing events on the queue through the other pygame.event functions.

有一些重要的事情必须在事件队列内部处理.主窗口可能需要重新绘制或响应系统.如果您长时间未调用事件队列,系统可能会判断您的程序已锁定.

There are important things that must be dealt with internally in the event queue. The main window may need to be repainted or respond to the system. If you fail to make a call to the event queue for too long, the system may decide your program has locked up.

此外,在移动窗口时,您的游戏会冻结.这是 SDL 的一个已知限制(请记住,pygame 只是 SDL 的一个薄包装)并且由 windows 消息循环引起:当您移动或调整窗口大小时,windows 使用模态循环,这将阻止您的游戏运行的线程.

Also, while moving your window around, your game will freeze. This is a known limitation of SDL (remember that pygame is just a thin wrapper around SDL) and caused by the windows message loop: when you move or resize a window, windows uses a modal loop, which will block the thread your game runs in.

这篇关于pygame.time.wait() 使窗口冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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