1

I recently started learning python in school as my first language and received a homework task asking us to create a simple rock paper scissors game but with a twist (mine's an RPG) and I thought it would be good if I made it so you would have to answer withing a time limit. I checked a few other threads, but I wasn't sure how to implement that code into my program, so I decided to ask here. I'm very new to python so any simple answers would be preferred if possible. Thank you advance! EDIT: tomh1012 gave some advice and I took it but my timer still does not work. There isnt any error on anything, it simply does not work! Any help is greatly appreciated. Also, for whatever reason, my teacher hasn't taught us functions yet so I don't really understand them much.

while keepPlaying == "y":
while playerHealth > 0 and cpuHealth > 0:
    time.sleep(0.75)
    print ("You have " + str(playerHealth) + " health.")
    print ("The enemy has " + str(cpuHealth) + " health.")
    print ("Rock")
    time.sleep(0.75)
    print ("Paper")
    time.sleep(0.75)
    print("Scissors")
    time.sleep(0.75)
    startTime=time.process_time() 
    playerChoice = input ("Shoot!")
    endTime=time.process_time() 
    elapsedTime = startTime - endTime
    cpuChoice = (random.choice(options))
    time.sleep(0.75)
    print ("Your opponent chose " + cpuChoice)
    if elapsedTime > 300:
        print("You're too slow!")
    elif playerChoice == "r" and cpuChoice == "s" or playerChoice == "p" and cpuChoice == "r" or playerChoice == "s" and cpuChoice == "p":
        damageDealt = 10 * combo
        combo = combo + 1
        time.sleep(0.75)
        print("You deal " + str(damageDealt) + " damage!")
        cpuHealth = cpuHealth - damageDealt
        enemyCombo = 1
    elif cpuChoice == "r" and playerChoice == "s" or cpuChoice == "p" and playerChoice == "r" or cpuChoice == "s" and playerChoice == "p":
        enemyDamageDealt = fans * enemyCombo
        playerHealth = playerHealth - enemyDamageDealt
        enemyCombo = enemyCombo + 1
        time.sleep(0.75)
        print("Your enemy deals " + str(enemyDamageDealt) + " damage!")
        combo = 1
    elif cpuChoice == playerChoice:
         time.sleep(0.75)
         print ("You both chose the same!")
    else:
        time.sleep(0.75)
        print ("...")
        time.sleep(1)
        print("Thats not a choice...")
        enemyDamageDealt = fans * enemyCombo
        playerHealth = playerHealth - enemyDamageDealt
        enemyCombo = enemyCombo + 1
        time.sleep(0.75)
        print("Your enemy deals " + str(enemyDamageDealt) + " damage!")
    if cpuHealth <= 0:
        print ("You win and gained 5 fans!")
        fans = fans + 5
        keepPlaying = input("Play again (y or n)")
        enemyHeal
    elif playerHealth <= 0:
        print("You lose, sorry.")
        keepPlaying = input("Play again (y or n)")
ElSponge
  • 11
  • 4
  • Have a look here and see if this helps: https://stackoverflow.com/a/15530613/9742036 – Andrew McDowell Nov 23 '18 at 19:45
  • You can get time elapsed using the time module. Add import time to your code, then use t=time.clock() to take a record of the current cpu time. You can then subtract your first time from your second to get the amount of time elapsed between them. An if t1-t2==chosenTime: would then let you decide if they acted quick enough. Hope this helps – tomh1012 Nov 23 '18 at 19:47
  • @tomh1012 Just tried this, right before the user inputs I get this error `Warning (from warnings module): File "D:\Downloads\Rock paper scissors simple.py", line 37 startTime=time.clock() DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead` and the timer does not work. What kind of result would I get if i minused the two times? Would it be seconds, minutes or a time stamp? – ElSponge Nov 23 '18 at 19:54
  • Check it: https://stackoverflow.com/questions/53167495/taking-in-multiple-inputs-for-a-fixed-time-in-python/53168235#53168235 – kantal Nov 23 '18 at 20:11
  • @ElSponge time.clock() will give you a number of milliseconds. That error is saying that time.clock() is outdated so you should use one of those other functions they've suggested. Really sorry but I can't help much with those new functions as I haven't learned much new python so time.clock() was the best I knew! If you look up those functions you should be able to work out what's new though – tomh1012 Nov 23 '18 at 20:30

1 Answers1

0

Here is a function that displays the given prompt to prompt a user for input. If the user does not give any input by the specified timeout, then the function returns None

from select import select
import sys

def timed_input(prompt, timeout):
    """Wait for user input, or timeout.

    Arguments:
    prompt  -- String to present to user.
    timeout -- Seconds to wait for input before returning None.

    Return:
    User input string.  Empty string is user only gave Enter key input.
    None for timeout.
    """
    sys.stdout.write('(waiting %d seconds) ' % (int(timeout),))
    sys.stdout.write(prompt)
    sys.stdout.flush()
    rlist, wlist, xlist = select([sys.stdin], [], [], timeout)
    if rlist:
        return sys.stdin.readline().strip()
    print()
    return None
gammazero
  • 773
  • 6
  • 13
  • This seems great! But I really wouldn't know how any of this works as Ive only had a few lessons. My teacher generally doesn't like us copying large blocks of code, but really, I wouldn't be able to adapt this. For now, I'll work on other parts of my program unless I get any new answers. Thanks a lot though! If we learn some of this soon maybe I'll come back to this and see if I can make any sense of it – ElSponge Nov 23 '18 at 23:09