logame team mailing list archive
-
logame team
-
Mailing list archive
-
Message #00024
Re: Keyboard Problems
On Mon, 05 Oct 2009 22:14:29 +0100
Paul Elms <paul@xxxxxxxxxxxxxx> wrote:
> Hi Ben,
>
> My keyboard will register 3 keys at once, ie: left, up and fire and
> all three keypresses are registered by pygame (I get the logging
> output from 3 keys). I have also read that keyboards can only handle
> so many key presses at one time and it varies from keyboard to
> keyboard, so I think we will have to live with it.
>
> I don't really know how event handling works in pygame, but I also get
> what you describe, holding down one key and then tapping another stops
> the first key. It sounds like the keyup event from the tap blocks the
> ongoing keydown event, but I am only guessing. I'll have another look
> at event handling tomorrow, when I am less tired :)
>
> Paul
>
>
> _______________________________________________
> Mailing list: https://launchpad.net/~logame
> Post to : logame@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~logame
> More help : https://help.launchpad.net/ListHelp
>
A possible solution is to set four directional movement variables in
the sprite class, possibly in an array, and then loop through the
variables and move as necessary. Something along the lines of
class sprite:
speed = 5
gravity_speed = 1
movement = (0,0,0,0) # up,right,down,left
def move:
if self.movement[0]==1:
self.y-=speed
if self.movement[1]==1:
self.x+=speed
if self.movement[2]==1:
self.y+=speed
if self.movement[3]==1:
self.x-=speed
#move for gravity
self.y+=gravity_speed
And then from the keyboard commands the RIGHT_ARROW keydown sets the
movement[1] to 1 and the RIGHT_ARROW keyup set the movement[1] to 0.
Actually, booleans would probably work a bit better.
I have recently written some Vala code to do this using SDL, and I could
port the code to Python if anyone is interested.
jezra
Follow ups
References