[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] moving sprites diagonal with keys



I don't know that you can check for two keys at once. How are you moving your
sprite? Do you have to tap your key to move the sprite? If so, maybe this would
work:

if LEFT_ARROW_PRESSED: sprite.x -= 1
if UP_ARROW_PRESSED: sprite.y -= 1

If you handle all your keyboard events before you tick(), this would produce
diagonal movement.

If you keep moving the sprite while the a key is down, then you could do
something like:

class Sprite:
def update( self ):
  self.x += self.vx * PIXELS_PER_TICK
  self.y += self.vy * PIXELS_PER_TICK

def eventlistener( self, event ):
  if event == LEFT_ARROW_PRESSED: self.vx = LEFT
  elif event == RIGHT_ARROW_PRESSED: self.vx = RIGHT
  elif event == UP_ARROW_PRESSED: self.vy = UP
  elif event == DOWN_ARROW_PRESSED: self.vy = DOWN
  elif event == LEFT_ARROW_RELEASED: self.vx = 0
  elif event == RIGHT_ARROW_RELEASED: self.vx = 0
  elif event == UP_ARROW_RELEASED: self.vy = 0
  elif event == DOWN_ARROW_RELEASED: self.vy = 0



--- mbopp <markus.bopp@smail.inf.fh-rhein-sieg.de> wrote:
> hi,
> 
> do you know a simple method to check for pressing two keys at 'once'?
> I need it to move sprites in diagonal directions.
> 
> thanks
> 
> markus
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com