Here's an example:

UP    = 0
DOWN  = 1
LEFT  = 2
RIGHT = 3

keys = [0, 0, 1, 0] #up, down, left, and right keys, respectively. 0 stands
for 'not pressed', 1 stands for 'is pressed'.

if LEFT in keys:
  print True

Now, it won't work, because "2" is not in the keys list.

if keys[LEFT]:
   print True

This works, because you're checking to see if the 3rd numeral in the list is
positive/true.

HTH

On Wed, Feb 11, 2009 at 5:46 PM, Yanom Mobis <ya...@rocketmail.com> wrote:

> Hmm. now i remember how it's done. the part about pygame.key.get_pressed
> returning 1's and 0's makes sense, but I just don't get why
>
> if key[K_UP]:
>
> works if
>
> if K_UP in key:
>
> doesn't.
>
> --- On *Tue, 2/10/09, mani...@gmx.de <mani...@gmx.de>* wrote:
>
>
> From: mani...@gmx.de <mani...@gmx.de>
> Subject: Re: [pygame] move problems
> To: pygame-users@seul.org
> Date: Tuesday, February 10, 2009, 7:22 PM
>
>
> -----Inline Attachment Follows-----
>
> hi,
>
> K_UP has the value 273 and pygame.key.get_pressed() returns a tuple where
> the
> n'th value is 1 if the n'th key was pressed.
> This should work:
>
> if key[K_UP]:
>     .....
>
> > this code is in my main game loop
> >
> > key = pygame.key.get_pressed() #create a key index
> >
> >     if K_UP in key: #check if the up arrow is pressed
> >         redcar.speed = (0, -2)
> >     else:
> >         redcar.speed = (0, 0)
> >
> >     redcar.rect = redcar.rect.move(redcar.speed) #move redcar by speed
> >
> > but pressing the up arrow doesn't move the sprite.
>
>
>


-- 
- pymike
"Python eggs me on."

Reply via email to