Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
1

Um... I need some help with pausing for a set amount of time without stopping the game

noway15
noway15

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?

2 years ago

Voters

Comments

TopNew
1
SixBeeps
SixBeeps

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.

2 years ago
1
noway15
noway15

@SixBeeps
how does that work?

2 years ago
1
SixBeeps
SixBeeps

@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 EVENT_LOOP: if currentTime - lastTime >= 1sec: # do something lastTime = currentTime ...
2 years ago
1
noway15
noway15

@SixBeeps
I guess I can see

2 years ago
1
import time time.sleep(however many milliseconds you want it to rest)
2 years ago
1
noway15
noway15

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

2 years ago
1

@noway15

i dont think it does tho

2 years ago
1
noway15
noway15

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

2 years ago
1
SixBeeps
SixBeeps

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

2 years ago
Load more