我正在进行文字冒险我正在使用
def do_health
print health,"/ 200"
显示健康状况,但我想将其转换为百分比并打印类似
的内容|---------- |
50%
取决于玩家离开的健康百分比,但我无法在其他任何地方找到任何关于使健康栏闲置的事情。
提前致谢。
答案 0 :(得分:0)
所有需要做的只是进行一些简单的转换,将您当前的健康状况转换为多个破折号,并定义破折号的最大数量(在这种情况下 20 :healthDashes
)相当于你的最大生命值(200:maxHealth
)。
考虑您还剩80
点生命值。因此,例如,如果我们将healthDashes(20)/maxHealth(200)
取为10
,则这是我们将健康除以将其转换为我们想要的破折号的值。然后,您可以将当前的health
设为80
,短划线的数量为:80/10 => 8 dashes
。百分比是直截了当的:(health(80)/maxHealth(200))*100 = > 40 percent
。
现在在python中你只需应用上面的那个lodic就可以得到:
health = 80 # Current Health
maxHealth = 200 # Max Health
healthDashes = 20 # Max Displayed dashes
def do_health():
dashConvert = int(maxHealth/healthDashes) # Get the number to divide by to convert health to dashes (being 10)
currentDashes = int(health/dashConvert) # Convert health to dash count: 80/10 => 8 dashes
remainingHealth = healthDashes - currentDashes # Get the health remaining to fill as space => 12 spaces
healthDisplay = ''.join(['-' for i in range(currentDashes)]) # Convert 8 to 8 dashes as a string: "--------"
remainingDisplay = ''.join([' ' for i in range(remainingHealth)]) # Convert 12 to 12 spaces as a string: " "
percent = str(int((health/maxHealth)*100)) + "%" # Get the percent as a whole number: 40%
print("|" + healthDisplay + remainingDisplay + "|") # Print out textbased healthbar
print(" " + percent) # Print the percent
如果你调用方法,你会得到结果:
do_health()
>
|-------- |
40%
以下是更改health
的价值的一些示例:
|---------- | # health = 100
50%
|--------------------| # health = 200
100%
| | # health = 0
0%
|------ | # health = 68
34%