Ask coding questions

← Back to all posts
Um... I need some help with pausing for a set amount of time without stopping the game
noway15 (97)

Basically I want to make a clicker game, but in Python's terminal. So I need a way to make it wait a set amount of time before delivering the currency (auto click thing), is there any way I can do that?

SixBeeps (5221)

The most common way to do this is through what's called an event loop. A game is typically structured entirely around this loop, and its job is to handle any sort of realtime events. I'd suggest you read up more if you want to familiarize yourself with this model.

You might be able to get away with running this loop through a new thread, but honestly it's better to implement it as the core of your game.

SixBeeps (5221)

@noway15 There's two main parts to an event loop, the event and the loop. Big surprise, I know.

There's a data structure, normally a queue, that holds all of the events that need to be processed. Whenever something happens, you put it into the event queue.

Then, an infinite loop checks if anything is in the event queue. It processes all of the events until the queue is empty. This infinite loop is the event loop.

Your your use case, I'm assuming you're handling key presses rather than mouse clicks. When a key is pressed, you'd add a key press event to the event queue. The next time the event loop iterates, it will see the new event and process it. You can tell your event queue to add to the score when it sees a key press event.

Now for what you were originally asking for: non-interupting time delay. I'll give you some Pythonic pseudocode to work with. It's hard for me to explain it, but hopefully you can see what's going on here.

lastTime = currentTime

  if currentTime - lastTime >= 1sec:
    # do something
    lastTime = currentTime
poetaetoes (327)
import time

time.sleep(however many milliseconds you want it to rest)
noway15 (97)

@poetaetoes I know about the time thing, but it doesn't really work because that pauses the entire program.

poetaetoes (327)

i dont think it does tho

noway15 (97)

@poetaetoes V(o-o)V idk but I do know that the full values in the parentheses are actually seconds?

SixBeeps (5221)

@poetaetoes Can confirm, time.sleep causes an interrupting time delay.