制作一个控制台游戏。我希望游戏在获得0 HP时结束

时间:2014-02-25 04:30:44

标签: c++

我目前正在创建一个涉及HP(角色的健康状况)的游戏,并想知道如何在游戏达到0 HP时让游戏结束。谁能帮我吗?此外,如果需要,我目前的代码粘贴在下面。

//Important:
//finalHp = hp - 10;
//cout << "Name: " << name << "   |   " << "Age: " << age << "   |   " << "Sex: " << sex << "   |   " << "Race: " << race << "   |   " << "HP: " << finalHp << endl;

#include <iostream>
#include <string>
using namespace std;

int main()
{
    //sets strings and starting hp
    string name;
    string age;
    string sex;
    string race;
    string input;
    int hp = 20;

    //creating your character
    cout << "Welcome to xVert77x's first ever published game!\nWhen answering questions you must either capitalize the first letter not\ncapitalize anything at all and must spell things correctly.\n(This rule excludes your name. You can make your name\nXxXxsNipERkIll360n0Sc0peSkIllZxXxX.)\n\nHave Fun!\n" << endl;
    cout << "Enter a character name: ";
    cin >> name;
    cout << "You have a very strange name... Enter a character age: ";
    cin >> age;
    cout << "Enter a character sex (M/F)(That means no aliens. Sorry ET): ";
    cin >> sex;
    cout << "Enter a character race (Human/Dwarf/Beast)(Still no aliens): ";
    cin >> race;
    cout << "Character created! Bringing you to your HUD..." << endl;

    //hud
    cout << "Name: " << name << "   |   " << "Age: " << age << "   |   " << "Sex: " << sex << "   |   " << "Race: " << race << "   |   " << "HP: " << hp << endl;

    //first question to player
    cout << "\nYou see a man. Do you kill him?\n1. Yes\n2. No" << endl;
    cin >> input;
    if (input == "yes" || input == "Yes")
    //if they choose yes it will take 5HP
    {
        cout << "You killed him! You lost 5 HP in the battle.\nThere was no reward because you shouldn't\nkill helpless people. :(" << endl;
        hp -= 5;
        cout << "Name: " << name << "   |   " << "Age: " << age << "   |   " << "Sex: " << sex << "   |   " << "Race: " << race << "   |   " << "HP: " <<hp << endl;
    }
    //if they choose no it will do nothing
    else if (input == "no" || input == "No")
    {
        cout << "You decided otherwise. That was a very smart decision." << endl;
        cout << "Name: " << name << "   |   " << "Age: " << age << "   |   " << "Sex: " << sex << "   |   " << "Race: " << race << "   |   " << "HP: " << hp << endl;
    }

    //next question to the player
    cout << "\nYou come to a fork in the road. Do you go right or do you go left? " << endl;
    cin >> input;
    if (input == "right" || input == "Right")
    //when they choose right this will happen
    {
        cout << "\nYou find a health potion and gain 7 HP!" << endl;
        hp += 7;
        cout << "Name: " << name << "   |   " << "Age: " << age << "   |   " << "Sex: " << sex << "   |   " << "Race: " << race << "   |   " << "HP: " << hp << endl;
    }
    //if you choose the left path
    else if (input == "left" || input == "Left")
    {
        cout << "\nYou fall into quicksand and barely make it out alive and then go to the path to the right. -10 HP." << endl;
        hp -= 10;
        cout << "Name: " << name << "   |   " << "Age: " << age << "   |   " << "Sex: " << sex << "   |   " << "Race: " << race << "   |   " << "HP: " << hp << endl;
    }


    cout << "\n\nEnd of game. For now... Press <enter> to exit...";
    cin.get();
    cin.get();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的int main()的设置方式是在最后退出,并使用return 0;语句。与其他if (...)分支一样,您在减去一些值后检查当前hp金额。当达到(或低于)零时,您只需输出一条消息,然后(从main函数)退出(在0内返回main有效地停止您的程序)。< / p>

一般来说,“玩家已经死了吗?” check将是你不止一次会做的事情,并且检查本身(以及任何类型的“你死了”的消息)将是相同的,无论它实际发生在各个if ()分支的哪个位置。

我会创建一个像bool isPlayerDead(int currentHP) { ... }这样的新方法,您可以在每次条件检查后调用它:

int main()
{
    // ...your setup stuff here...
    int hp = 20;

    // ...the first of your if() checks here...

    if (isPlayerDead(hp))
    {
        showPlayerDeathMessage();
        return 0;
    }

    // ...if you got here, the player is not yet dead... do some more if() checks...

    if (isPlayerDead(hp))
    {
        showPlayerDeathMessage();
        return 0;
    }

    // ...and again, player is not dead, so so some more...
    // (and repeat this process for a while...)


    // Finally, if you get here, the player did NOT die while playing. Maybe
    // print a "Wow! You're still alive!" message before exiting.
    return 0;
}

当然,你稍后会组织这个循环,有更多内容等等,但看起来这至少应该让你指向正确的方向。