试图用python编写DnD战斗脚本,掷骰后需要帮助打印敌方HP

问题描述 投票:0回答:1

我是编码的新手,所以如果我犯了一个粗心的错误,请提前抱歉。

我正在尝试编写DnD(特别是旧共和国的星球大战骑士)战斗脚本。基本上,每当攻击时,游戏掷出20面骰子,并且如果结果大于敌人的防御力,则表示成功命中。

[这里,我创建了一个脚本,基本上可以做到这一点。但是,我在打印敌人的HP时遇到问题。脚本没有记住最后一个数字并继续减去伤害直到敌人的生命值达到0,而是每次都从30减去该数字,所以我每次掷骰子都会得到一个不同的数字。

我将如何使程序记住HP值并相应地从中减去损失?

import random

dice_min = 1
dice_max = 20

defense = 10
attack_bonus = 3

player_vitality = 30
enemy_vitality = 30

running = True

while running == True:

dice_roll = random.randint(dice_min, dice_max)

attack_roll = dice_roll + attack_bonus

enemy_damage = enemy_vitality - attack_roll

attack_prompt = input("Do you want to attack? [y/n]: ")

if attack_roll > defense:
    print("successful hit, the enemy's health is now " + str(enemy_damage))
elif attack_roll < defense:
    print("you missed, the enemy's health is " + str(enemy_damage))
python python-3.x random
1个回答
0
投票

您需要做的是将敌人的新hp值保存到其变量中。在您的代码中,您只需计算出攻击后的剩余HP并打印即可,但是您没有将其保存在任何地方。然后在下一个循环中,您只需计算本质上是30损伤的表达式并打印结果。

像这样修改您的代码:

import random

dice_min = 1
dice_max = 20

defense = 10
attack_bonus = 3

player_vitality = 30
enemy_vitality = 30

running = True

while running == True:

    dice_roll = random.randint(dice_min, dice_max)
    attack_roll = dice_roll + attack_bonus
    enemy_vitality = enemy_vitality - attack_roll
    attack_prompt = input("Do you want to attack? [y/n]: ")

    if attack_roll > defense:
        print("successful hit, the enemy's health is now " + str(enemy_vitality))
    elif attack_roll < defense:
        print("you missed, the enemy's health is " + str(enemy_vitality))
© www.soinside.com 2019 - 2024. All rights reserved.