views:

62

answers:

2

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.

+1  A: 

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.

Justin Ardini
Yep. pygame.event.get doesn't trigger, though if I stick a print statement before the for loop that _does_ get triggered (just thought of that now).Oh, and it doesn't trigger without the parentheses though, so that was step one of my mistake...The for loop doesn't seem to trigger at all.
ReadyWater
Are you sure `pygame.event.get()` isn't triggered? It's possible that it's triggered but there aren't any events.
Justin Ardini
Ahh, just figured it out. It's 'cause I'm hitting it twice. exitCheck seems to empty the event queue, so there's nothing to check when controlUpdate gets its turn.Thanks for your help. I'm just going to combine the two methods into one, I didn't realize it worked that way.
ReadyWater
Yeah, I just looked at the docs for `pygame.event.get()` and saw that it emptied the event queue, so the queue would always be empty for `controlUpdate()`.
Justin Ardini
+3  A: 

Aren't you missing some parentheses there?

if self.controlUpdate() == True:

chryss