2013-11-03 55 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所有事件都无关。