I am writing a game (frogger clone) where the main character class (inspired
by "astraea.py") inherits from key.KeyStateHandler, and deals with key
events itself.  However, in doing so, I find that the key event is not
"consumed" i.e. if I press on a key to move a character, it keeps moving.
This was not the case when the key handling was all done from a window
object.   I looked at the astraea.py code and can not see what I am doing
different.  I did find a work around ... but I get the feeling I am missing
something obvious.    Here's a brief summary of the relevant parts of the
code.

# main script; lots of stuff left out


frog = Frog(world)

@win.event
def on_key_press(symbol, modifiers):
##    if symbol == key.LEFT:   # this used to work as is
##        frog.step(dx = -STEP)
##    elif symbol == key.RIGHT:
##        frog.step(dx = STEP)
##    elif symbol == key.UP:
##        frog.jump(dy = JUMP)
##    elif symbol == key.DOWN:
##        frog.jump(dy = -JUMP)
    if symbol == key.ESCAPE:
        win.has_exit = True

# new code to shift the relevant key handling to the main character
win.push_handlers(frog)

while not win.has_exit:
    win.dispatch_events()
    win.clear()

    for f in frogs:
        f.update()

    win.flip()
#########
# code from the script where Frog is defined and updated

class Frog(key.KeyStateHandler):
    '''Our hero'''
    def __init__(self, world):
        super(Frog, self).__init__()
        self.world = world
        # much stuff deleted

    def update(self):
        '''simplest case is just drawing == blitting; animation will
follow'''
        if self[key.LEFT]:
            self.step(dx = -STEP)
            self[key.LEFT] = False  # this is a workaround
        elif self[key.RIGHT]:
            self.step(dx = STEP)
        elif self[key.UP]:
            self.jump(dy = JUMP)
        elif self[key.DOWN]:
            self.jump(dy = -JUMP)
        self.at_rest.blit(self.x, self.y)

#######
Any suggestion would be appreciated.

André

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to