glennrv
glennrv

Reputation: 3

How can I wait for an input while something else happens at the same time?

I've been having a really hard time putting this problem into words. What I'm trying to do is to have a while-loop constantly running, unless I give a 'stop' command.

I'm making a fishing mini-game in a bigger game, and I want to be able to start fishing on command (which I have been able to do), and for the fishing to continue happening, until I eventually type 'stop'.

The way I've been doing things is that I ask for a command input, then do something based on the command. However, in this situation, I dont want to do that, because asking for the input pauses the while-loop.

I'm open to other suggestions on how to exit the while-loop with a certain command, but the game is text-based, so ideally I want a text command to exit.

if command == 'fish':
    print('You start fishing')
    while True:
        # fishing happens, you may or may not catch something
        # if you enter a command ('exit'), you stop fishing
        # I dont, however, want the code to pause to ask for a command.
        # I want it to be uninterrupted fishing, until I choose to exit

Upvotes: 0

Views: 679

Answers (1)

Hosseinreza
Hosseinreza

Reputation: 551

you need 2 loops here and flags , when user pauses the pauseFlag become True and your code will come out from the inner loop and when he unpause the pauseFlag switches to False and Code will enter the inner loop again and the game will resuming.

Somthing like below:

while(quitFlag):
    while(!pauseFlag):
        #your code
        if(somthing):
            pauseFlag = True

   if(somthing):
      pauseFlag = False # and it make the code goes to the second loop again and resume the game

Maybe you wonder : what is quitFlag ? as you see it's the condition of the first loop. Whenever quitFlag switches to false the game will shut down and The user can't resume the game and he must start the game all over.

Upvotes: 1

Related Questions