Python time.sleep()没有睡觉

时间:2016-01-19 03:41:36

标签: python time

我正在编写一个游戏,而战斗功能似乎正在绊倒我。这是我的代码的战斗片段:

def combat(player, enemy, dun):
  print("\n"*100 + "A " + enemy.name + " has attacked you!")
  while player.health > 0 and enemy.health > 0:
    print(enemy.name[:1].upper() + enemy.name[1:], "health:", enemy.health)
    print("Your health:", player.health)
    cmd = input(">")
    if cmd == "attack":
      enemy.health -= player.atk(enemy)
    if cmd == "run":
      coin = random.choice(["heads", "tails"])
      if coin == "heads":
        break
      else:
        print("You couldn't escape.")
    if cmd == "equip":
      target = input("Which item?\n>")
      print(player.equip(target))
    player.health -= enemy.atk(player)

  if enemy.health <= 0:
    print("You defeated the", enemy.name + "!")
    if enemy.drop != None:
      return "Enemy defeated.\nThe " + enemy.name + " dropped a " + enemy.drop + "!"
      dun.data[dun.pos][2].append(enemy.drop)
    else:
      return "Enemy defeated."
    time.sleep(1.5)
    out = 1
  elif player.health <= 0:
    print("You died fighting %s..." % enemy.name)
    dun.pos == (0, 0)
    player.inventory == []
    return "You reawaken in the same room you started in, pack empty..."
    time.sleep(3)

我已经导入了随机模块和时间模块,它只是不在代码段中。

当序列结束时,它不会睡觉并立即进入主游戏循环(如果需要,我可以给出)。

除了凌乱,我做错了什么?

1 个答案:

答案 0 :(得分:3)

return语句会立即退出您的函数。 之后的任何代码都不会运行。您可以在sleep

之前return

示例:

time.sleep(1.5)
return "Enemy defeated."

但是在你打电话给你的功能之后睡觉可能会更有意义:

combat(...)
time.sleep(5)