2011-06-22 61 views
1

我正在通過livewires wrapper運行它,它只是訓練pygame和其他python模塊的輪子;但每次我運行它,它都會執行,當我嘗試退出時,它不會響應,然後崩潰。pygame在我從IDLE運行後試圖退出時凍結!

任何關於如何解決這個問題的建議都會很棒。在我的教科書中沒有任何輸入,並且所有谷歌似乎都會使用pygame本身來解決這個問題。

顯然pygame和Tkinter似乎衝突?

在此先感謝!

附錄 - 這是我試圖運行代碼:

from livewires import games 

screen_w = 640 
screen_h = 480 

my_screen = games.Screen (wid, heit) 
my_screen.mainloop() 
+1

你說的崩潰是什麼意思?它是否會拋出異常或錯誤?此外,pygame使用SDL而不是Tkinter,SDL已知需要一段時間才能關閉。 –

+1

通過崩潰我的意思是程序變得沒有響應,我不得不去任務管理器退出它。 另外,事實證明我不應該從IDLE運行pygame,我應該直接從Python運行它。我不知道爲什麼,但我仍然想知道。從IDLE運行它確保pygame程序無響應。 – Louis93

+0

嗯,這很奇怪。當你在IDLE之外運行時會發生什麼? –

回答

4

類似的問題:Pygame screen freezes when I close it

沒有任何輸入在我的課本 和所有谷歌似乎收率 使用pygame 本身導致此問題。

這些結果可能解決了您遇到的同樣問題。這是從livewires的games.py文件的相關部分,無處它調用pygame.quit()

def handle_events (self): 
    """ 
    If you override this method in a subclass of the Screen 
    class, you can specify how to handle different kinds of 
    events. However you must handle the quit condition! 
    """ 
    events = pygame.event.get() 
    for event in events: 
     if event.type == QUIT: 
      self.quit() 
     elif event.type == KEYDOWN: 
      self.keypress (event.key) 
     elif event.type == MOUSEBUTTONUP: 
      self.mouse_up (event.pos, event.button-1) 
     elif event.type == MOUSEBUTTONDOWN: 
      self.mouse_down (event.pos, event.button-1) 

def quit (self): 
    """ 
    Calling this method will stop the main loop from running and 
    make the graphics window disappear. 
    """ 

    self._exit = 1 

def mainloop (self, fps = 50): 
    """ 
    Run the pygame main loop. This will animate the objects on the 
    screen and call their tick methods every tick. 

    fps -- target frame rate 
    """ 

    self._exit = 0 

    while not self._exit: 
     self._wait_frame (fps) 

     for object in self._objects: 
      if not object._static: 
       object._erase() 
       object._dirty = 1 

     # Take a copy of the _objects list as it may get changed in place. 
     for object in self._objects [:]: 
      if object._tickable: object._tick() 

     self.tick() 

     if Screen.got_statics: 
      for object in self._objects: 
       if not object._static: 
        for o in object.overlapping_objects(): 
         if o._static and not o._dirty: 
          o._erase() 
          o._dirty = 1 

     for object in self._objects: 
      if object._dirty: 
       object._draw() 
       object._dirty = 0 

     self._update_display() 

     self.handle_events() 

    # Throw away any pending events. 
    pygame.event.get() 

的退出事件只是設置它會把你出的mainloop功能while循環的一個標誌。我猜如果你在你的Python目錄中找到這個文件,並在mainloop的最後一行後面加上一個pygame.quit(),它會解決你的問題。

0

我同意。您需要將整個程序(要執行的部分,而不是定義等)放到while循環中。問題是pygame在IDLE中退出時沒有被告知關閉,但在IDLE之外,程序的關閉覆蓋需要關閉pygame。

這裏是循環:

done = False 
while done==False: 
    # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT 
    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 

    #Main program here 

pygame.quit()