Press Enter to continue otherwise exit code
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?
Please specify your language next time.
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!
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!")
10
so:
from getch import getch i = getch() if ord(i) == 10: print("Let's play!") else: print("You should of pressed enter!")
and spend all that time using ord()? That's like typing five more characters!
@IcingHackzprobs becuase I was capitalized when it wasn't supposed to, try it now.
@IcingHackzhuh, let me check
@IcingHackzOh I see, it was just me being stupid, fixed now
@IcingHackzhow do i make it when arrow keys are pressed e.g when left arrow key pressed then it will change the [email protected]
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
use the
getkey
module