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

Re: [pygame] newbie



On Thu, 25 Mar 2010 19:10:22 -0000, snet-1@xxxxxxx wrote:
> Hi guys.
> 
> I'm new to python and programming in general so expect noob questions :.)
> 
> I've got up to classes & functions & having problems getting round a
little
> input problem.
> 
> I've put my keyboard controls in a separate module from the pygames main
> loop and my 'character' in another module.
> When I was putting it all in one file, if I held left my 'character'
> carried
> on moving left, but now it will only move the specified amount of pixels
> then stop.

I think part of the problem is that you're looking at KEYDOWN events. Those
only happen when a key is pressed, meaning there's only one no matter how
long you hold the key. So the code would make your character move for one
frame, then stop.

You could make the character move every frame (eg. setting a speed and
moving by that speed per frame) until a KEYUP event happens. A different
way to handle things is to use pygame.key.get_pressed() (see
http://www.pygame.org/docs/ref/key.html ), which tells you what keys are
down at the moment you ask. You then say, "keys_down =
pygame.key.get_pressed" then "if keys_down[K_RIGHT] ## move right", and so
on.

One example is in my messy code at
http://kschnee.xepher.net/code/squirrelgame/squirrel4.py.txt , under
"Controls".