制作一个控制台游戏。(Making a console game. I want the game to end when they get 0 HP)

编程入门 行业动态 更新时间:2024-10-28 07:19:11
制作一个控制台游戏。(Making a console game. I want the game to end when they get 0 HP)

我目前正在创建一个涉及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; }

I am currently creating a game where it involves HP (character's health) and wanted to know how to make the game end when it reaches 0 HP for the player. Can anyone help me out? Also, my current code so far is pasted below if needed.

//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; }

最满意答案

你的int main()是以最终退出的方式设置的, return 0; 声明。 就像其余的if (...)分支一样,在减去一些值后检查当前的hp数量。 当达到(或低于)零时,您可以简单地输出一条消息,然后(从您的main函数)退出(在main内部返回0有效地停止您的程序)。

一般来说,“玩家已经死了吗?” 检查将是你不止一次会做的事情,并且检查本身(以及任何类型的“你死的”消息)将是相同的,无论它实际发生在各个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; }

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

Your int main() is setup in a way that exits at the very end, with that return 0; statement. Just like the rest of your if (...) branches, you check the current hp amount after you subtract some value. When that reaches (or falls below) zero, you can simply output a message and then (from your main function) exit (returning 0 inside of main effectively stops your program).

In general, the "has the player died yet?" check is going to be something you will do more than once, and the check itself (along with any kind of "you died" message) will be the same, regardless of where along the various if () branches it actually occurred.

I'd create a new method like bool isPlayerDead(int currentHP) { ... } that you could call after each of your condition checks:

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; }

Of course, you will organize this later to do loops, have more content, etc. but it seems like this should at least get you pointed in the right direction.

更多推荐

本文发布于:2023-08-04 03:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   游戏   Making   HP   game

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!