开发者

Python event handler method not returning value in conditional

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.con开发者_如何学PythontrolUpdate == 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.


Aren't you missing some parentheses there?

if self.controlUpdate() == True:


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜