Tutorial :Python event handler method not returning value in conditional



Question:

I'm completely new to python (and it's been a while since I've coded much). I'm trying to call a method which acts as an event handler in a little "hello world" type game, but it's not working at all. I'm using the pygames 1.9.1 lib with python 2.6.1 on OSX 10.6.3.

So this is in a while loop:

       self.exitCheck()               if self.controlUpdate == True:             self.playerX += 1  

And the two methods in question are:

def exitCheck(self):      for event in pygame.event.get():          if event.type == KEYDOWN:              if event.key == pygame.K_ESCAPE:                  print "Escape pressed"                  pygame.quit()                  exit()      def controlUpdate(self):      for event in pygame.event.get():          if event.type == KEYDOWN:              if event.key == pygame.K_SPACE:                  print "Spacebar pressed"                  return True          elif event.type == KEYUP:              if event.key == pygame.K_SPACE:                  print "Spacebar not pressed"                  return False          else:              return False  

exitCheck is always, but controlUpdate never seems to get called while in the conditional. I feel like I've either missed something from the python book I've been going through (the oreilly Learning Python) or I've just taken too long a break from proper coding, but yeah. Would much appreciate the help.


Solution:1

Edit: The problem is that pygame.event.get() both returns and removes all events from the event queue. This means that every time you call controlUpdate(), the event queue will be empty and nothing inside the for loop will be executed.


Solution:2

Aren't you missing some parentheses there?

if self.controlUpdate() == True:


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »