【问题标题】:Pygame screen freezes when I close it当我关闭它时,Pygame 屏幕冻结
【发布时间】:2011-04-11 01:03:51
【问题描述】:

代码加载了一个 pygame 屏幕窗口,但是当我单击 X 关闭它时,它变得无响应。我在 64 位系统上运行,使用 32 位 python 和 32 位 pygame。

from livewires import games, color

games.init(screen_width = 640, screen_height = 480, fps = 50)

games.screen.mainloop()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    Mach1723 的answer 是正确的,但我想建议主循环的另一种变体:

    while 1:
        for event in pygame.event.get():
            if event.type == QUIT: ## defined in pygame.locals
                pygame.quit()
                sys.exit()
    
            if event.type == ## Handle other event types here...
    
        ## Do other important game loop stuff here.
    

    【讨论】:

      【解决方案2】:

      我推荐以下代码。首先,它包含时钟,因此您的程序不会占用 CPU,而只会轮询事件。其次,它调用 pygame.quit() 来防止程序在 Windows 上的 IDLE 下运行时冻结。

      # Sample Python/Pygame Programs
      # Simpson College Computer Science
      # http://cs.simpson.edu/?q=python_pygame_examples
      
      import pygame
      
      # Define some colors
      black    = (   0,   0,   0)
      white    = ( 255, 255, 255)
      green    = (   0, 255,   0)
      red      = ( 255,   0,   0)
      
      pygame.init()
      
      # Set the height and width of the screen
      size=[700,500]
      screen=pygame.display.set_mode(size)
      
      pygame.display.set_caption("My Game")
      
      #Loop until the user clicks the close button.
      done=False
      
      # Used to manage how fast the screen updates
      clock=pygame.time.Clock()
      
      # -------- Main Program Loop -----------
      while done==False:
          for event in pygame.event.get(): # User did something
              if event.type == pygame.QUIT: # If user clicked close
                  done=True # Flag that we are done so we exit this loop
      
          # Set the screen background
          screen.fill(black)
      
          # Limit to 20 frames per second
          clock.tick(20)
      
          # Go ahead and update the screen with what we've drawn.
          pygame.display.flip()
      
      # Be IDLE friendly. If you forget this line, the program will 'hang'
      # on exit.
      pygame.quit ()
      

      【讨论】:

      • 使用这个效果最好。一旦完成,您可以轻松保存任何状态是错误的。
      【解决方案3】:

      这是一个非常简单的问题,您需要处理“QUIT”事件,请参阅事件文档:http://www.pygame.org/docs/ref/event.html

      编辑: 现在我突然想到您可能正在处理“QUIT”事件并且它不起作用 但我不知道你的代码没有更多细节。

      处理“QUIT”事件的简单方法的简单示例:

      import sys
      import pygame
      
      # Initialize pygame
      pygame.init()
      pygame.display.set_mode(resolution=(640, 480))
      
      # Simple(ugly) main loop
      curEvent = pygame.event.poll()
      
      while curEvent.type != pygame.QUIT:
            # do something
            curEvent = pygame.event.poll()
      

      【讨论】:

        【解决方案4】:

        在使用 pygame 时,您必须处理包括 QUIT 在内的所有事件,因此如果您不处理退出事件,您的程序将不会退出。这是一个代码。

        import sys
        import pygame
        from pygame.locals import *
        
        def main():
            running = True
            while running:
                for event in pygame.event.get():
                    if event.type==QUIT: #QUIT is defined at pygame.locals 
                        runnning = False
            #other game stuff to be done
        
        if __name__=='__main__':
            pygame.init()
            pygame.display.set_mode((640,480))
            main()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-23
          • 2011-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多