Python增加了一个时间限制,并检查他们是否采取了行动

2024-06-23 19:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近在学校开始学习python作为我的第一语言,并收到了一个家庭作业任务,要求我们创建一个简单的石头布剪刀游戏,但有一个扭曲(我的是一个RPG),我认为这将是一个很好的,如果我使它,所以你必须回答一个时间限制。我检查了一些其他线程,但是我不确定如何在我的程序中实现这些代码,所以我决定在这里询问。我对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)")

Tags: orandyoutimesleepprintelifstr
1条回答
网友
1楼 · 发布于 2024-06-23 19:28:47

下面是一个函数,用于显示提示用户输入的给定提示。如果用户在指定的超时时间内没有提供任何输入,那么函数返回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

相关问题 更多 >

    热门问题