I'm trying to code an attack for a text based adventure game, where the player can do damage with a sword. A random number is shown in the console and the player must type in that number as fast as possible. The faster they do it, the more damage they deal. So far I have this:
import random
import time
from threading import Timer
def damage(timeAmount, baseDamage):
damageTotal = baseDamage - timeAmount
def sword():
t = 0
timerAmount = 0
randomNumber = random.randint(0, 1000000)
print('Type the following as fast as you can: \n' + str(randomNumber))
swordAttack = input('> ')
while t == 0:
timerAmount = timerAmount + 1
time.sleep(1)
if swordAttack == randomNumber:
t = 1
damage(timerAmount, 10)
print(timerAmount)
sword()
First issue is that I can't figure out how to get a timer to count up and then get that final time for use in the damage formulas. Timer function from threading only counts down, and I couldn't figure out how exactly to work time it, although if you could explain it to me in simple terms that might work.
Second issue is that I also need to make it so the timer activates as soon as the input is shown on screen, but when the input statement runs, it stops the code until an input is given.