Skip to content
Press Enter to continue otherwise exit code
Profile icon
egrakev

Hi, in my textbased game I want the player to press Enter to play and if they dont press Enter I want the code to exit and for the player to restart the game.

I watched a video about the key function and im trying to make it work from that.

def on_press(key):

if key == Key.enter: print("Wise choice, lets play!") return False if key != Key.enter: print("You pressed {0} you moron, get lost!".format(key)) quit()

with Listener(on_press = on_press) as listener:
listener.join()

time.sleep(1)
print("Game starts in 3..")
time.sleep(1)
print("2..)"
time.sleep(1)
print("1..)"

And this is where I am trying to code the continuation.
However, even if I press anything but Enter, the code continues, do I have to continue coding within the definition of on_press?

You are viewing a single comment. View All
Profile icon
InvisibleOne

this is pretty outdated… and getch hates working on replit.
Just use Getkey, it’s basically the same and works great for arrow keys.
Example:

‘’’py
from getkey import getkey, keys

pressed = getkey()

if pressed == keys.UP:
print(“up arrow”)

‘’’
use keys.RIGHT, keys.LEFT, and keys.DOWN for the other arrow keys


@lumaali