1
    from time import sleep
    alive = True
    while alive == True:
        print("You have awoken in a unknown land.")
        sleep(2)
        print("\nDo you go north, south, east or west?")
        print("Press the up arrow to go north.")
        print("Press the down arrow to go south.")
        print("Press the left arrow to go west.")
        print("Press the right arrow to go east.")
        input(" ")

How do I get the user to press one of the arrow keys and have the program carry on without the need of pressing the enter key?

Thanks in advance!

~Lorenzo

  • Which operating system? [`msvcrt.getch`](https://docs.python.org/2/library/msvcrt.html#msvcrt.getch) is available but only on Windows, for instance. – Kevin May 22 '17 at 18:18
  • This may be more complicated than you think. You could see [here](https://stackoverflow.com/questions/22397289/finding-the-values-of-the-arrow-keys-in-python-why-are-they-triples) for example. By the looks of your example code, you will find it easier to progress with your project by accepting "N, E, S, W" as inputs. – roganjosh May 22 '17 at 18:20
  • Thanks Kevin but I would like to make it a bit more friendly for people who are new to the pc world, it is alot easier to find the arrows than the N, E, S and W keys. Just a personal preference though. :) – Lorenzo Rossi May 22 '17 at 18:25
  • 1
    Kevin didn't suggest that, I did. I was suggesting that you use the letters for simplicity for now. It's my assumption that you are learning Python (`while alive == True:` would just be `while alive:` and your `input` possibly won't do anything as it's not assigned to anything). I think it will be easier for you to move forward (for now) without struggling with this point. – roganjosh May 22 '17 at 18:29

2 Answers2

2

You may look into a package like pynput for multiplatform support. Pynput implements mouse and keyboard listeners. This will also allow you to do WSAD movement in your RPG-like game.

For keyboard listeners, you can have a onpress/onrelease key. The help files will have some better examples.

from pynput import keyboard

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

If you want to use up/down/left/right (arrow keys) for movement, this may be the easiest least painful solution too.

zglin
  • 2,891
  • 2
  • 15
  • 26
  • That looks like an idea, thankyou for that, will check it out! :D Honestly to me it seems extremely silly that an advance language like Python would not have this function built in. – Lorenzo Rossi May 22 '17 at 18:44
1

You Could Make Options Like:

option = input('1/2/3/4:')

or:
Python has a keyboard module with many features. You Can Use It In Both Shell and Console. Install it, perhaps with this command:

pip3 install keyboard

Then use it in code like:

import keyboard #Using module keyboard
while True:  #making a loop
    try:  #used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('up'): #if key 'up' is pressed.You can use right,left,up,down and others
            print('You Pressed A Key!')
            #your code to move up here.
            break #finishing the loop
        else:
            pass
    except:
        break  #if user pressed other than the given key the loop will break

You can set it to multiple Key Detection:

if keyboard.is_pressed('up') or keyboard.is_pressed('down') or keyboard.is_pressed('left') or keyboard.is_pressed('right'):
    #then do this

You Can Also Do Something like:

if keyboard.is_pressed('up') and keyboard.is_pressed('down'):
    #then do this

It Also Detect Key For The Whole Windows.
Thanks.