[Tutor] Need help with python keyboard press/navigation commands

Alan Gauld alan.gauld at btinternet.com
Tue Mar 12 10:33:02 CET 2013


On 12/03/13 03:59, akuma ukpo wrote:

> I have tried
>
> def get_direction(self):
>          """
>          whenever a key is pressed the character moves to
>          the direction corresponding to the key
>          """
>          'North':w
>          'South': s
>          'East' : d
>          'West' : a


What did you think that would do?
There is no control structure and no return value. Nothing is assigned 
to anything. And none of the names are defined anywhere.

It makes no sense to me looking at it and it certainly doesn't make 
sense to Python trying to execute it.

If you want to respond to keys you are going to have to either pass in 
the keystroke to the function or read the keystroke inside the function.

How you do that depends on whether you are using a GUI, and if so which. 
Whether you are using PyGame. Or if a CLI program which OS you are using.


Finally, from a user experience point of view, it might be more logical 
to make the keys w,a,s,z North, West,East and South respectively since 
its a more logical correspondence to the compass points (assuming a 
standard QWERTY keyboard layout).

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list