Skip to content
Sign upLog in
← Back to Community

Is there a way of making an 'Repeat until input'?

Profile icon
GoodCoderBadBoy

I'm making a game in python and I'm aiming for a title screen similar to the original Super Mario Bros I know how to make the "press enter to start" flash but I can't figure out a way for it to do this until the player presses the enter button. Is there a way of doing this?

Answered by Alois [earned 5 cycles]
View Answer
Voters
Profile icon
LuisNava4
Profile icon
sylashur
Profile icon
GoodCoderBadBoy
Comments
hotnewtop
Profile icon
Alois

Oh, by the way...
Another solution:

import os # just imports the base os module, should work in repl.it

os.system("""bash -c 'read -s -n 1 -p "Press any key to continue..."'""") # this makes it so that it won't proceed until ANY key is pressed... you can change the string to whatever you want, but i'm still looking into non-static methods for this

Profile icon
GoodCoderBadBoy

@Alois
thank you

Profile icon
Alois

Use

import this

from console.utils import wait_key

then use this

wait_key()

^^^ This just waits until you press a key (raw) and moves on when it's done. You could combine this with threading to have the animation still go on...

Profile icon
Alois

Could you be a little more descriptive?
Are you using pygame or just inputs from the python shell?
(i'm assuming pygame)

If it's pygame you're using, try the following:

start=False
while not start:
# (everything in loop here)

for event in pygame.event.get(): if event.type == pygame.KEYDOWN: # checks if key pressed start=True # once this happens, the loop is broken

(main program after this loop)

feel free to copy this, but this is really dumb and simple...
The way to see if a keyboard key was pressed is [event.type == pygame.KEYDOWN]. It says False or True as it's output, so you can easily implement it in your program.

Keep in mind 'pygame.event.get()' is a list of events, and it contains all inputs/events that are given, that's why you need a for loop to check each event, and if one of them is a key pressed event (using event.type... there are other values like event.key which specifies which key was pressed, but it's not needed for this)

Profile icon
GoodCoderBadBoy

@Alois
It's text based and normal python 3.0 I'm new-ish and am only using python

Profile icon
Alois

@JoshuaIp
Interesting... This is probably somewhere in the 'sys' module (import sys)...

Try doing some research I'll see how I can help

Profile icon
Alois

@JoshuaIp

I found something that COULD help...
Try looking into this:
https://docs.python.org/2/library/curses.html

I'm not 100% sure tho...

Profile icon
sylashur

All you need is a while loop that depends on the condition of a variable, for convenience let's call it enterPressed. You don't have to actually call it that obviously. Anyway, the while loop is going to compare the value of the enter key, and when it finds it as pressed, then change the enterPressed variable to true. If your while loop looks like this: while not enterPressed then it should work.