0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Herobrine
  • 47
  • 1
  • 5
  • maybe get time at start - `start = time.time()` - and at the end - `end = time.time()` - and calculate `end - start` to get how long it takes. – furas Nov 20 '19 at 15:34
  • if you have to use `input()` with other thing then you have to run `input()` or other thing in separated thread. – furas Nov 20 '19 at 15:36

1 Answers1

0

Ok So I'm going to give this a go in a way that uses the timer count down. If you have the damage of the sword be 10 anyways there is no need to go over 10 into 'negatives' it would just be 0 so just count down from whatever the swords damage is. So if they type it in 2 seconds then that would be 8 damage in this case. Here is some code I got to work using a countdown. All you have to do is set the timer to whatever the swords damage is.

import random
import time
from threading import Timer


def damage(timeAmount, baseDamage):
    damageTotal = baseDamage - timeAmount
    print(format(damageTotal, '.0f'))


def sword():
    user_sword_max_damage = 10
    randomNumber = random.randint(0, 10)
    timeout = user_sword_max_damage
    t = Timer(timeout, print, ['Damage dealt = 0'])
    start_time = time.time()
    t.start()
    while True:
        try:
            prompt = int(input('Enter ' + str(randomNumber) + ' as fast as you can: '))
            if prompt == randomNumber:
                t.cancel()
                final_time = time.time()-start_time
                print(format(final_time, '.0f'))
                damage(final_time, user_sword_max_damage)
                break
            elif prompt != randomNumber:
                continue
        except:
            continue
sword()

This could also potentially point you in the right direction if you add more damage items later on. The only part I didn't add is the logic to set the damageTotal to 0 if the time runs out which you should be able to put inside the if statement within the while loop(be kind this is my first time attempting an answer ha but I do hope this helps)

David Sorrell
  • 231
  • 2
  • 12