C++ 一个简单的格斗游戏的代码,但当玩家的生命值变为0后,游戏继续循环

C++ 一个简单的格斗游戏的代码,但当玩家的生命值变为0后,游戏继续循环,c++,C++,这就是完整的代码,它有一个显示函数,用于显示和处理输入,还有一个主函数,当GameOver函数设置为false时,通过显示 #include <iostream> #include <conio.h> #include <stdlib.h> #include <cstdlib> #include <ctime> #define key_x 88 using namespace std; bool GameOver = false;

这就是完整的代码,它有一个显示函数,用于显示和处理输入,还有一个主函数,当GameOver函数设置为false时,通过显示

#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#define key_x 88
using namespace std;
bool GameOver = false;


void Display() {
    int choose;
    char ch;
    system("color 04");
    cout << "WELCOME" << endl;
    for (int i = 1; i < 5; i++) {
        cout << "Character " << i << endl;
    }
    cout << "choose character number: ";
    cin >> choose;


    if (choose > 4) {
        cout << "Enter valid character number: ";
        cin >> choose;

    }
    cout << "Selected Character is " << choose << endl;
    system("pause");
    system("cls");

    srand((unsigned)time(0));
    int random = (rand() % 4) + 1;
    system("color 02");
    cout << "ENEMY IS CHARACTER " << random << endl; 

     int hp1 = 100;
     int hp2 = 100;

    cout << "Character " << choose << "(" << hp1 << ")" << " VS. Character " << random << "(" << hp2 << ")" << endl;


    while(!GameOver)                      
    {
        srand(time(0));
        int rand1 = (rand() % 100) + 1;
        int rand2 = (rand() % 100) + 1;

        int NewHp1 = hp1 - rand1;
        int NewHp2 = hp2 - rand2;
        

            cout << "Press any key to attack" << endl;

            ch = (_getch());
            int value = ch;
            while (value == key_x)
                break;

            cout << "Enemy has " << NewHp2 << " health left." << endl;

            system("pause");

            cout << "You have " << NewHp1 << " health left." << endl;

            if (NewHp2 <= 0) {
                cout << "YOU WIN!!" << endl;
                break;
                GameOver = true;
            }

            else if (NewHp1 <= 0) {
                system("color 04");
                cout << "YOU LOSE" << endl;
                break;
                GameOver = true;
            }

            else
            {
            
                continue;

            }
    }

}


int main() {
    while(!GameOver)
          Display();
    
}
#包括
#包括
#包括
#包括
#包括
#定义键x 88
使用名称空间std;
bool GameOver=false;
无效显示(){
int选择;
char ch;
系统(“颜色04”);
库特
遇到
中断时,它会中断,因此
GameOver=true;
不会运行。请删除
中断

        else
        {  
            continue;
        }
是多余的。此循环中已没有可运行的内容,因此它仍将继续。请删除此
else

最后,您忘记使用
NewHp1
NewHp2
更新
hp1
NewHp2

hp1 = NewHp1;
hp2 = NewHp2;

就在循环结束之前。

通常只应调用
srand(时间(0))
在程序开始时设置一次。在
中断后设置
GameOver=true;
以使代码永远不会执行。不需要使用带continue的else。这是正确的,但我希望每次循环时生成一个介于1-100之间的新数字,因此它在循环内。不保证每次都是一个新的数字,如果你用相同的值对它进行两次种子设定,你就可以保证得到相同的数字。最好是一次种子设定,然后让序列播放出来。如果你需要唯一的数字,那么你应该为此添加逻辑,而不是依靠希望。
hp1 = NewHp1;
hp2 = NewHp2;