Skip to content
← Back to Community
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?

Voters
Profile icon
CollinHammatt
Profile icon
egrakev
Comments
hotnewtop
Profile icon
codingjlu

Please specify your language next time.

Profile icon
egrakev

Thanks guys, seems like I have found the perfect place to throw out all my questions, you just wait until I want to make NPC's and character classes, ooh so many questions there will be!

Profile icon
InvisibleOne

Here's a simpler way:

from getch import getch i = getch() if i == '\n': print("Let's play!") else: print("You should of pressed enter!")
Profile icon
icodeletion

@InvisibleOne
actually each key has it's on code. And enter has the code 10 so:

from getch import getch i = getch() if ord(i) == 10: print("Let's play!") else: print("You should of pressed enter!")
Profile icon
InvisibleOne

and spend all that time using ord()? That's like typing five more characters!

@IcingHackz

Profile icon
icodeletion

@InvisibleOne
yours doesn't work

Profile icon
InvisibleOne

probs becuase I was capitalized when it wasn't supposed to, try it now.

@IcingHackz

Profile icon
icodeletion

@InvisibleOne
nope, still doesn't work. When I press enter it says You should of pressed enter! even though it should say lets play! Mine works fine though.

Profile icon
InvisibleOne

huh, let me check

@IcingHackz

Profile icon
InvisibleOne

Oh I see, it was just me being stupid, fixed now

@IcingHackz

Profile icon
Atomic-Gamming

@IcingHackz
maybe try /n cause thats what it types

Profile icon
icodeletion

@InvisibleOne
yup! it works now

Profile icon
InvisibleOne
Profile icon
lumaali

how do i make it when arrow keys are pressed e.g when left arrow key pressed then it will change the [email protected]

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

Profile icon
icodeletion

use the getkey module