从另一个类添加到python中的for循环

从另一个类添加到python中的for循环,python,function,loops,methods,super,Python,Function,Loops,Methods,Super,原谅我,我是初学者 简而言之,我试图在python中为循环添加一个单独的类。以下是详细信息 我正在用python和pygame制作一个游戏。我的所有“状态”(例如开始状态、游戏状态、游戏结束状态等)都有一个基类,其中包括一个函数,该函数通过pygames“事件”(例如退出程序、按键等)进行for循环检查 下面是父方法。。。它起作用了 def events(self): for event in pygame.event.get(): if event.type == py

原谅我,我是初学者

简而言之,我试图在python中为循环添加一个单独的类。以下是详细信息

我正在用python和pygame制作一个游戏。我的所有“状态”(例如开始状态、游戏状态、游戏结束状态等)都有一个基类,其中包括一个函数,该函数通过pygames“事件”(例如退出程序、按键等)进行for循环检查

下面是父方法。。。它起作用了

def events(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            self.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.quit()
下面是childs的方法。它想添加一个“密钥启动事件”到已经存在的“退出”和“退出”事件中

def events(self):
    super().events()
    for event in pygame.event.get():
        if event.type == pygame.KEYUP:
            self.start_state = False
发生的情况是,代码卡在父类for循环中,并忽略它下面的所有内容。我还试着调用childs for循环下面的super,这只会导致它陷入childs for循环,而忽略了父母

我想一个解决方案是为childs类事件创建一个单独的方法,但是我仍然想知道我尝试的方法是否可行,这样我就可以避免混乱的单独函数

谢谢。

与其在pygame.event.get():方法中调用循环(
for event in pygame.event():
),不如在
events
方法中使用单独的方法来处理事件,而将它们放到其他地方

class Base():

    def events(self):
       for event in pygame.event.get():
          self._proceed_event(event)

    def _process_event(self, event):        
        if event.type == pygame.QUIT:
            self.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.quit()

class Child(Base):

    def _process_event(self, event):
       if event.type == pygame.KEYUP:
          self.start_state = False
       super()._proceed_event(event)