Skip to main content
 首页 » 编程设计

python之窗口在 10 秒内未关闭

2025年05月04日20zhenyulu

我想在 10 秒后完成动画。为此,我在动画中添加了计时器 start_ticks = pygame.time.get_ticks()并在 for 中检查这个计时器循环 seconds > max_simulation_time .

但是,窗口不会在 10 秒内关闭。只有当我在屏幕上移动鼠标时它才会关闭。有点奇怪的行为。我究竟做错了什么? 如果它可能是一个重要的细节,我会在 Jupyter notebook 中运行代码。

class Environment(): 
 
    def __init__(self, title): 
 
        pygame.init() 
 
        self.screen = pygame.display.set_mode((800, 800)) 
        pygame.display.set_caption(title) 
 
        # ... 
 
 
    def run(self):    
        carryOn = True 
        max_simulation_time = 10 
        start_ticks = pygame.time.get_ticks() 
        while carryOn: 
            seconds=(pygame.time.get_ticks()-start_ticks)/1000 
            for event in pygame.event.get(): 
                if (seconds > max_simulation_time): 
                    carryOn = False 
                    pygame.display.quit() 
                    pygame.quit() 
                    quit() 
 
            agent_action = 1 
            self.all_sprites.update(self.screen, agent_action) 
            self.screen.fill((0, 40, 0)) 
            self.all_sprites.draw(self.screen) 
            pygame.display.flip() 
 
 
if __name__ == "__main__": 
    env = Environment("TEST") 
    env.run() 

另外,有没有办法只break 10 秒后的动画并从头开始(重置),不关闭窗口?

更新:

如果我这样做:

class Environment(): 
 
    def __init__(self, title): 
 
        pygame.init() 
 
        self.screen = pygame.display.set_mode((800, 800)) 
        pygame.display.set_caption(title) 
 
        # ... 
 
 
    def run(self):    
        carryOn = True 
        max_simulation_time = 10 
        start_ticks = pygame.time.get_ticks() 
        while carryOn: 
            seconds=(pygame.time.get_ticks()-start_ticks)/1000 
            if (seconds > max_simulation_time): 
                carryOn = False 
                pygame.display.quit() 
                pygame.quit() 
                quit() 
            for event in pygame.event.get(): 
                if (event.type == pygame.QUIT): 
                    carryOn = False 
                    pygame.display.quit() 
                    pygame.quit() 
                    quit() 
        # ... 
 
if __name__ == "__main__": 
    env = Environment("TEST") 
    for epochs in range(1,3): 
        env.run() 

<...> 然后我得到以下错误:

------------------------------------------------------------------- 
error                             Traceback (most recent call last) 
<ipython-input-1-102fa6124bbf> in <module>() 
    515     #env.run() 
    516     for epochs in range(1,3): 
--> 517         env.run() 
 
<ipython-input-1-102fa6124bbf> in run(self) 
    471                 pygame.quit() 
    472                 quit() 
--> 473             for event in pygame.event.get(): 
    474                 if (event.type == pygame.QUIT): 
    475                     carry_on = False 
 
error: video system not initialized 

请您参考如下方法:

我认为这可能与检查是否仅在 for event in pygame.event.get() 循环中超过了 max_simulation_time 有关。

这就是为什么它只在您移动鼠标时发生。在进入该 for 循环之前检查您是否超过了该时间。