0

Here is a relevant code snippet that I'd like to implement this on (the f-string in start()). I've assigned to variables the values from the parameter of the function that allows the interpreter to pause when running my script whenever it's called. That's what this part here is all about. (although it's not working)

def pause_here(delay=0):
    time.sleep(delay)

quartsec_pause = pause_here(.25)
halfsec_pause = pause_here(.5)
sec_pause = pause_here(1)
sec2_pause = pause_here(2)

This is the beginning of my Text Adventure, the main screen. This is where I attempted to implement the feature using this f-string here:

def start():
    slow_print(f"""_
    
        It's Monday, 9:33pm, August 4th, 2024.{halfsec_pause}

        You lay in the back of an old truck headed down what now feels 
        like an endless dirt road.{quartsec_pause} The sound of the truck clunking 
        down the road enters your consciousness.{halfsec_pause} 
        
        Just as quickly,{quartsec_pause} the spatter of the engine reduces to a 
        complete silence.{quartsec_pause} Your eyes open for the first time in well, 
        
        that's the thing...{sec_pause}

        You can't remember the last time you were awake.{halfsec_pause}

        Enter (1) to observe your surroundings.{quartsec_pause}
        Enter (2) to scream your lungs out.{quartsec_pause}
        Enter (3) to climb out of the truck.{quartsec_pause}
        Enter (4) to hear what's going on around you.
    
    _""", 3)

Below is where I handle all of the user input stuff. Not really as pertinent to the question at hand so this can mostly be ignored. Just wanted to show in case anyone needed some help with the overall picture of what I'm trying to create.

    choice = input("--> ")
    reset_console()
    
    if "1" in choice:
        start_observe()
    elif "2" in choice:
        start_scream()
    elif "3" in choice:
        start_climb()
    elif "4" in choice:
        start_hear()
    else:
         start()

I appreciate any solution I get on this, it'd be great if I can implement this somehow to make it look a lot nice when it's running!

Update:

def start():
    for i in [.5, .25, .5, .25, .25, 1, .5, .25, .25, .25]:
        time.sleep(i)
        slow_print(f"""_
        
            It's Monday, 9:33pm, August 4th, 2024.{i}

            You lay in the back of an old truck headed down what now feels 
            like an endless dirt road.{i} The sound of the truck clunking 
            down the road enters your consciousness.{i} 
            
            Just as quickly,{i} the spatter of the engine reduces to a 
            complete silence.{i} Your eyes open for the first time in well, 
            
            that's the thing...{i}

            You can't remember the last time you were awake.{i}

            Enter (1) to observe your surroundings.{i}
            Enter (2) to scream your lungs out.{i}
            Enter (3) to climb out of the truck.{i}
            Enter (4) to hear what's going on around you.
        
        _""", 3)

While waiting to post this question here. I tried the above, it did not work either.

1 Answers1

0

One way to solve this problem is to separate the print operation from the pause operation. In the example below I create a list of tuples that includes the text to be printed along with the pause to be inserted after the print operation. The function unpacks those tuples and uses the first value (the text) to print and the second value (the pause) to sleep:

import time
pause_text_list = [
    ("It's Monday, 9:33pm, August 4th, 2024.", .5),
    ("You lay in the back of an old truck headed down what now feels like an endless dirt road.", .25),
    ("The sound of the truck clunking down the road enters your consciousness.", .5),
    ("Just as quickly,", .25),
    ("the spatter of the engine reduces to a complete silence.", .25),
    ("Your eyes open for the first time in well, that's the thing...", 1.0),
    ("You can't remember the last time you were awake.", .5),
    ("Enter (1) to observe your surroundings.", .25),
    ("Enter (2) to scream your lungs out.", .25),
    ("Enter (3) to climb out of the truck.", .25),
    ("Enter (4) to hear what's going on around you.", .25),
]
def print_pause_text(pause_text_list):
    for text, pause in pause_text_list:
        print(text)
        time.sleep(pause)

print_pause_text(pause_text_list)
It's Monday, 9:33pm, August 4th, 2024.
You lay in the back of an old truck headed down what now feels like an endless dirt road.
The sound of the truck clunking down the road enters your consciousness.
Just as quickly,
the spatter of the engine reduces to a complete silence.
Your eyes open for the first time in well, that's the thing...
You can't remember the last time you were awake.
Enter (1) to observe your surroundings.
Enter (2) to scream your lungs out.
Enter (3) to climb out of the truck.
Enter (4) to hear what's going on around you.
rhurwitz
  • 2,557
  • 2
  • 10
  • 18