Python添加了时间限制,并检查它们是否已执行

时间:2018-11-23 19:41:52

标签: python python-3.x

我最近开始在学校里学习python作为我的第一语言,并收到一项作业任务,要求我们创建一个简单的剪刀石头布游戏,但要有所作为(我的角色扮演游戏),我认为这样做会很好您必须在有时间限制的情况下回答。我检查了其他几个线程,但是不确定如何在程序中实现该代码,所以我决定在这里询问。我是python的新手,因此,如果可能的话,最好选择任何简单的答案。 谢谢你提前! 编辑:tomh1012提供了一些建议,我接受了,但我的计时器仍然不起作用。没有任何错误的东西,它根本行不通!任何帮助是极大的赞赏。另外,由于某种原因,我的老师还没有教我们功能,所以我对它们并不了解。

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)")

1 个答案:

答案 0 :(得分:0)

此处是显示给定提示以提示用户输入的功能。如果用户未在指定的超时时间内提供任何输入,则该函数返回~

None