【问题标题】:Math isn't adding up from what is being output数学不是从输出的内容中加起来的
【发布时间】:2020-02-12 16:57:44
【问题描述】:

我目前正在尝试制作一个非常基本的随机数游戏。一切似乎都在以应有的方式工作,减去数学没有加起来的事实。 你有 3 个选项 1) 攻击,2) 治疗,3) 退出游戏。每个函数都按预期运行并显示预期结果,减去数学不加起来的事实。

--------------------- Beau hit the monster for 10 damage! The Monster hit Beau for 18 damge! You now have 81 health points! The monster now has 80 health points! ---------------------

老实说,我不确定自己错过了什么,感觉自己像个白痴。

这里是源代码。

from random import randint

game_running = True


def calculate_monster_attack(attack_min, attack_max):
    return randint(attack_min,attack_max)


def calculate_player_attack(attack_min, attack_max):
    return randint(attack_min, attack_max)


def calculate_player_healing(healing_min, healing_max):
    return randint(healing_min, healing_max)


while  game_running == True:
    new_round = True
    player = {'attack_min': 10, 'attack_max': 20, 'healing_min': 10, 'healing_max': 18, 'health': 100}
    monster = {'name': 'Monster', 'attack_min': 10, 'attack_max': 20, 'health': 100}
print('---' * 7)
print('Enter Player name!')
player['name'] = input()

while new_round == True:

    player_won = False
    monster_won = False

    print('---' *7)
    print('Please select an action')
    print('---' *7)
    print('1) Attack')
    print('---' *7)
    print('2) Heal')
    print('---' *7)
    print('3) Exit Game')

    player_choice = input()
    print('---' *7)

    if player_choice == '1':

        monster['health'] = monster['health'] - calculate_player_attack(player['attack_min'], player['attack_max'])
        print(player['name'] + ' hit the monster for {} damage!'.format(calculate_player_attack(player['attack_min'], player['attack_max'])))

        if monster['health'] <= 0:
            player_won = True

        else:
            player['health'] = player['health'] - calculate_monster_attack(monster['attack_min'], monster['attack_max'])
            print('The ' + (monster['name'] + ' hit ' + player['name'] + ' for {} damage!'.format(calculate_monster_attack(monster['attack_min'], monster['attack_max']))))

            if player['health'] <=0:
                monster_won = True

    elif player_choice == '2':

        player['health'] = player['health'] + calculate_player_healing()
        print('You healed yourself and now have {} health points!'.format(player['health']))

        print('The ' + (monster['name'] + ' hit ' + player['name'] + ' for {} damge!'.format(calculate_monster_attack(monster['attack_min'], monster['attack_max']))))
        player['health'] = player['health'] - calculate_monster_attack(monster['attack_min'], monster['attack_max'])

        if player['health'] <=0:
            monster_won = True

    elif player_choice == '3':
        new_round = False
        game_running = False
    else:
        print('Invalid Input')

    if player_won == False and monster_won == False:
        print('You now have {} health points!'.format(player['health']))
        print('The monster now has {} health points!'.format(monster['health']))

    elif monster_won:
        print('Oh no! Better luck next time ' + player['name'] + ', the monster won this round!')
        new_round = False

    elif player_won:
        print('Congratulations ' + player['name'] + ' you beat the monster!')
        new_round = False

任何帮助将不胜感激。

【问题讨论】:

    标签: python python-3.x random int


    【解决方案1】:

    您的游戏似乎会为每次攻击计算两次伤害,一次是在打印之前,一次是在打印中。尝试保存第一次攻击伤害计算的值并在打印中使用该值,而不是计算另一个随机值。

    【讨论】:

    • 感谢您的回复。总的来说,我只是开始着手编程。我不是 100% 确定如何保存第一次攻击伤害计算的值,然后使用该存储值将其打印出来。我一直在尝试创建另一个函数,然后在尝试存储前一个函数时调用该函数。我确信这是 100% 错误的,所以请随时告诉我。我也用谷歌搜索了一下,并没有找到太多帮助我解决问题的方法。
    • 类似 ”damage = calculate_player_attack(player['attack_min'], player['attack_max']);怪物['health'] = monster['health'] - 伤害; ”应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2021-04-29
    • 2017-06-28
    • 1970-01-01
    • 2022-01-22
    • 2019-12-19
    • 2019-11-16
    相关资源
    最近更新 更多