2013-11-03 53 views
0

pygame中可能有「for pygame.event.get()中的事件:」多次在同一個循環中,還是隻能使用一次?我正在學習pygame,並且我決定定義兩個函數來檢查事件,但是當一個被調用時,另一個不起作用。請記住,我是一個noob。這顯然不是實際的代碼,但它是我正在做的事情。多次檢查pygame事件

import pygame 

class someclass: 
    def __init__(self): 
     some declaration 
    def somefunction(self): 
     for event in pygame.event.get(): 
      if event.type == someevent: 
       some code 
    def mainloop(self): 
     someinstance = somesubclass() 
     while True: 
      someinstance.somefunction2() 
      self.somefunction() 

class somesubclass: 
    def __init__(self): 
     some declaration 
    def somefunction2(self): 
     for event in pygame.event.get(): 
      if event.type == someevent 
       somecode 

maininstance = someclass() 
maininstance.mainloop() 

somefunction不執行

+1

最好只有一個。你應該發佈一些代碼,所以我們可以幫助你。 – jgritty

+0

在你的例子中使用真實的代碼。也許問題不在於「建設」,而在於一些真正的指導。 – furas

+0

我在你的例子中加了一些'print',我看到'somefunction()'被執行。我沒有測試事件。 – furas

回答

0

如果您希望在應用程序的許多部分檢查事件,儘管不可取 - 因爲當應用程序變大時,您將不知道某個按鈕的功能。

這將是這樣:

import pygame 

class MainClass: 
    def __init__(self): 
     self.someinstance = SomeSubclass() 
    def check_events(self): 
     for event in pygame.event.get(): 
      self.someinstance.somefunction2(event) 
      if event.type == someevent: 
       some code 
    def mainloop(self): 
     someinstance = somesubclass() 
     while True: 
      self.somefunction() 

class SomeSubclass: 
    def __init__(self): 
     some declaration 
    def somefunction2(self,event): 
     if event.type == someevent 
      somecode 

的目標是將事件傳播到每一個對象,這將是會決定,做什麼的對象。

0

您的問題是在事件系統。

如果你得到了事件,那麼事件就會被破壞,所以你不能再次得到它 - 這是正常的。

你在somefunction2somefunction所有事件都無關。