I am trying to make a simple ticker game. A ticker ticks at the rate of 1 clix per second as shown in the code below. When there are enough clix (in this case, 40
for an Apple
) I want to be able to purchase it, and the clix should reset by an amount of -40
and keep counting 1 per second thereon.
Upon purchase of that 1 Apple, I also would want to increment the clix rate to 2/sec from the previous 1/sec and so on.
How is this possible in Python when the code follows a top-down sequence? In this case, the function clicker()
will never reach the lines below it.
I will be using Classes to make all this simpler, but before that can someone help me with this obstacle?(Can I make the clicks run in the background somehow while the other parts of the code can run or is there another way around?)
import time
def clicker():
global clix
clix = 0
endgame = False
while not endgame:
clix += 1
time.sleep(1)
return clix
c = clicker()
print('1. Apple : 40 clix | 2. Orange : 75 clix')
ch = int(input('\nPurchase ID: '))
if c == 1 and c >= 40:
print('Got an Apple!')
clix += 2 # Just to get the point across, I know this line will throw an error.
elif ch == 2 and c >= 75:
print('Got an Orange!')
clix += 4
else:
print('Need more Clix')
You have some other errors in your code, there. But to answer your specific question, yes you can run some stuff in the background while other parts run.
What you are looking for is Threads.
Check out this answer for more details: Creating Threads in python