python - 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)")
解决方案
这是一个显示给定提示以提示用户输入的函数。如果用户在指定的超时时间内没有给出任何输入,则函数返回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
推荐阅读
- android - 在 Flutter 中使用按钮访问路由时出错
- java - 为多个实体配置 ReplyingKafkaTemplate
- python - 如果我的 8 核 CPU 支持 16 个线程,对于池中的进程数,16 会比 8 更好吗?
- amazon-web-services - 在 api gateway 中创建一个 api 来调用一个 lambda 函数
- node.js - 在 mongodb 数据库中保存新帖子时发生错误
- html - 我在“摘要”标签中有一个“标签”标签。单击标签时不会打开详细信息。怎么解决?
- java - 每次用户登录网站时如何生成会话ID?
- python - 在python上卡住映射位置热图
- amazon-web-services - 我如何知道无服务器使用哪个命令来构建?
- python - 如何解决 Python 中的列表索引超出范围?