C++ implements a simplified version of Yuanshen game

directly on the code

#include <iostream>
#include <string>

struct Character {
    
    
    std::string name;
    int health;
    int attack;
};

int main() {
    
    
    Character player;
    player.name = "旅行者";
    player.health = 100;
    player.attack = 10;

    Character enemy;
    enemy.name = "原神";
    enemy.health = 100;
    enemy.attack = 10;

    std::cout << "欢迎来到原神游戏!\n";
    std::cout << "你是" << player.name << ",拥有" << player.health << "点生命值和" << player.attack << "点攻击力。\n";
    std::cout << "你的对手是" << enemy.name << ",拥有" << enemy.health << "点生命值和" << enemy.attack << "点攻击力。\n\n";

    // 游戏循环
    while (player.health > 0 && enemy.health > 0) {
    
    
        // 玩家攻击敌人
        std::cout << player.name << "攻击" << enemy.name << ",造成" << player.attack << "点伤害。\n";
        enemy.health -= player.attack;

        if (enemy.health <= 0) {
    
    
            std::cout << enemy.name << "被击败了!恭喜你获得胜利!\n";
            break;
        }

        // 敌人攻击玩家
        std::cout << enemy.name << "反击" << player.name << ",造成" << enemy.attack << "点伤害。\n";
        player.health -= enemy.attack;

        if (player.health <= 0) {
    
    
            std::cout << player.name << "被击败了!很遗憾你失败了。\n";
        }

        std::cout << "当前状态:\n";
        std::cout << "你的生命值:" << player.health << "\n";
        std::cout << enemy.name << "的生命值:" << enemy.health << "\n\n";
    }

    std::cout << "游戏结束。\n";

    return 0;
}

In this game, you will play the role of a traveler and fight against the original god. Both parties have health and attack power, and attack each other until one party's health reaches zero. The game loop continues until one side wins or both lose. The game will output the current health status of both players, and display a victory or defeat message at the end of the game. This is just a tiny simulation, hope you like it!

Guess you like

Origin blog.csdn.net/yaosichengalpha/article/details/131897567