[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Keyboard handling



> Hey all, I'm writing a breakout clone in PyGame, and I'm trying
> to implement acceleration for my paddle. I have a Paddle.Move()
> function that continuously adds accel_rate to self.cur_accel, and then
> moves the paddle self.cur_accel to the left or right. The problem is,
> I want the cur_accel to be set back to 0 when the player lets go
> of the key.. I couldn't find anything on 'key release' events, and
> having a 'timeout' period for the key event seems nasty. Any suggestions?

hello chris, it looks like you've already got a couple responses, but
i'll just throw in one more here. here is a section of code from one
of the projects i'm working with. in this game a spaceship will fly
at turbo speed while the spacebar is down. here is what that chunk of
code looks like.


    def handleevent(self, e):
            if e.type == QUIT:
                self.finish()
                return 1
            elif e.type == KEYDOWN:
                elif e.key == K_SPACE:
                    self.player.cmd_turbo(1)
                    return 1
                elif e.key == K_ESCAPE:
                    self.finish()
                    return 1
            elif e.type == KEYUP:
                key = e.key
                if key == K_SPACE:
                    self.player.cmd_turbo(0)
                    return 0


if you want to base your movement off of the event queue, this can
be an ideal way to do it. notice that this event handler code belongs
in a special 'state' class i have, the code that calls this function
looks like this.

        e = pygame.event.poll()
        if e.type:
            mystate.handleevent(e)

note here i check if e.type is nonzero. the event.poll() function
will return an event type "NOEVENT", which means there are no
pending events.

if you want to see an example of how to handle keyboard by just
checking the current state each frame, see the aliens example.

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org