如何访问并随后更改循环外的变量

我正在为我目前正在开发的游戏开发遭遇系统。stats 和 medkits 只是为了在完成游戏之前构建系统的占位符,除了这个问题之外,它的功能很好。我希望在循环内访问 medkits 变量,然后,如果有第二次遇到,将该变量更改为正确的数字。例如,如果我在第一次遭遇时使用了一个医疗包,那么下次我应该少一个。不知道如何做到这一点,因为这是我第一次真正尝试制作合法游戏。任何功能解决方案都会很棒!


我还想补充一点,我知道第三个选项不起作用,敌人还没有反击,但这对我来说没有问题。我只想要这个特定问题的答案。


charisma = int(60)

strength = int(25)

endurance = int(40)

intelligence = int(70)

capacity = int(50)

from random import randint

medkits = 2


def encounter(enemy):

  print("Engaging "+enemy+"!")

  print()

  y = 10 + (strength*.10)

  enhealth = int(100)

  enhealth = int(enhealth-y)

  health = int(100)

  med = int(medkits)

  while True:

    print("Pick your move!")

    print("1. Smack with shovel")

    print("2. Use a medkit (you have "+str(med)+" medkits)")

    print("3. Attempt escape")

    enc = input("I will try to (put a #): ")

    if "1" in enc:

      x = randint(0, 100)

      if x < 80:

        enhealth = int(enhealth-y)

        print()

        print("You hit "+enemy+" for "+str(y)+" damage!")

        print()

      else:

        print()

        print("Miss!")

        print()

    elif "2" in enc:

      if med > 0:

        print()

        print("You used a medkit!")

        print()

        health = health+30

        med = med-1

        print("Health is at "+str(health))

        print()

      else:

        print("You're out of medkits!")

    if enhealth <= int(0):

      print(enemy+" has been defeated! Well done!")

      break


encounter("Cornelius")


喵喔喔
浏览 139回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript