用C++做挂机小游戏

介绍

有三个选项

1. 战斗
2. 保存游戏进度
3. 退出游戏

当用户选择1时就会对"魔王"造成damage点伤害。

当用户选择2时会保存游戏进度,生成一个叫"savegame.txt"的txt的文件,存储了"勇士"和"魔王"的血量等

当用户选择3时就会直接退出

实现

可以分别分成:头文件,定义角色类,构造函数,战斗函数,升级函数,保存游戏进度,            加载游戏进度,保存后重新进入时的加载游戏进度,操作。

头文件
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
定义角色类
class Character {
public:
    string name;
    int level;
    int exp;
    int hp;
    int maxHp;
    int attack;
    int defense;
构造函数
Character(string n, int l, int e, int h, int a, int d) {
        name = n;
        level = l;
        exp = e;
        hp = h;
        maxHp = h;
        attack = a;
        defense = d;
    }
战斗函数
void fight(Character& enemy) {
        int damage = attack - enemy.defense;
        if (damage < 0) {
            damage = 0;
        }
        enemy.hp -= damage;
        cout << name << "攻击了" << enemy.name << ",造成了" << damage << "点伤害。" << endl;
        if (enemy.hp <= 0) {
            cout << enemy.name << "被击败了!" << endl;
            int gainExp = rand() % 10 + 1;
            cout << name << "获得了" << gainExp << "点经验值。" << endl;
            exp += gainExp;
            if (exp >= level * 10) {
                levelUp();
            }
            enemy.hp = enemy.maxHp;
        }
    }
升级函数
    void levelUp() {
        level++;
        exp = 0;
        maxHp += rand() % 5 + 1;
        attack += rand() % 3 + 1;
        defense += rand() % 3 + 1;
        cout << name << "升级了!" << endl;
        cout << "等级:" << level << endl;
        cout << "生命值:" << maxHp << endl;
        cout << "攻击力:" << attack << endl;
        cout << "防御力:" << defense << endl;
    }
};
保存游戏进度
void saveGame(Character& player) {
    ofstream outfile("savegame.txt");
    if (outfile.is_open()) {
        outfile << player.name << endl;
        outfile << player.level << endl;
        outfile << player.exp << endl;
        outfile << player.hp << endl;
        outfile << player.maxHp << endl;
        outfile << player.attack << endl;
        outfile << player.defense << endl;
        outfile.close();
        cout << "游戏进度已保存。" << endl;
    }
    else {
        cout << "无法保存游戏进度。" << endl;
    }
}
加载游戏进度
bool loadGame(Character& player) {
    ifstream infile("savegame.txt");
    if (infile.is_open()) {
        string name;
        int level, exp, hp, maxHp, attack, defense;
        infile >> name >> level >> exp >> hp >> maxHp >> attack >> defense;
        player = Character(name, level, exp, hp, attack, defense);
        infile.close();
        cout << "游戏进度已加载。" << endl;
        return true;
    }
    else {
        cout << "无法加载游戏进度。" << endl;
        return false;
    }
}

int main() {
    srand(static_cast<unsigned int>(time(0))); // 初始化随机数种子

    Character player("勇士", 1, 0, 10, 3, 2); // 创建角色
    Character enemy("魔王", 1, 0, 10, 2, 1); // 创建敌人

    cout << "欢迎来到挂机游戏!" << endl;
保存后重新进入时的加载游戏进度
if (loadGame(player)) {
    cout << "继续游戏?(y/n)" << endl;
    char choice;
    cin >> choice;
    if (choice == 'n') {
        player = Character("勇士", 1, 0, 10, 3, 2);
        cout << "游戏进度已重置。" << endl;
    }
}

操作
    while (true) {
        cout << "当前状态:" << endl;
        cout << "等级:" << player.level << endl;
        cout << "经验值:" << player.exp << endl;
        cout << "生命值:" << player.hp << "/" << player.maxHp << endl;
        cout << "攻击力:" << player.attack << endl;
        cout << "防御力:" << player.defense << endl;

        cout << "请选择操作:" << endl;
        cout << "1. 战斗" << endl;
        cout << "2. 保存游戏进度" << endl;
        cout << "3. 退出游戏" << endl;
        int choice;
        cin >> choice;

        switch (choice) {
        case 1:
            player.fight(enemy);
            break;
        case 2:
            saveGame(player);
            break;
        case 3:
            return 0;
        default:
            cout << "无效的选择。" << endl;
            break;
        }
    }

    return 0;
}

完整代码

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

// 定义角色类
class Character {
public:
    string name;
    int level;
    int exp;
    int hp;
    int maxHp;
    int attack;
    int defense;

    // 构造函数
    Character(string n, int l, int e, int h, int a, int d) {
        name = n;
        level = l;
        exp = e;
        hp = h;
        maxHp = h;
        attack = a;
        defense = d;
    }

    // 战斗函数
    void fight(Character& enemy) {
        int damage = attack - enemy.defense;
        if (damage < 0) {
            damage = 0;
        }
        enemy.hp -= damage;
        cout << name << "攻击了" << enemy.name << ",造成了" << damage << "点伤害。" << endl;
        if (enemy.hp <= 0) {
            cout << enemy.name << "被击败了!" << endl;
            int gainExp = rand() % 10 + 1;
            cout << name << "获得了" << gainExp << "点经验值。" << endl;
            exp += gainExp;
            if (exp >= level * 10) {
                levelUp();
            }
            enemy.hp = enemy.maxHp;
        }
    }

    // 升级函数
    void levelUp() {
        level++;
        exp = 0;
        maxHp += rand() % 5 + 1;
        attack += rand() % 3 + 1;
        defense += rand() % 3 + 1;
        cout << name << "升级了!" << endl;
        cout << "等级:" << level << endl;
        cout << "生命值:" << maxHp << endl;
        cout << "攻击力:" << attack << endl;
        cout << "防御力:" << defense << endl;
    }
};

// 保存游戏进度
void saveGame(Character& player) {
    ofstream outfile("savegame.txt");
    if (outfile.is_open()) {
        outfile << player.name << endl;
        outfile << player.level << endl;
        outfile << player.exp << endl;
        outfile << player.hp << endl;
        outfile << player.maxHp << endl;
        outfile << player.attack << endl;
        outfile << player.defense << endl;
        outfile.close();
        cout << "游戏进度已保存。" << endl;
    }
    else {
        cout << "无法保存游戏进度。" << endl;
    }
}

// 加载游戏进度
bool loadGame(Character& player) {
    ifstream infile("savegame.txt");
    if (infile.is_open()) {
        string name;
        int level, exp, hp, maxHp, attack, defense;
        infile >> name >> level >> exp >> hp >> maxHp >> attack >> defense;
        player = Character(name, level, exp, hp, attack, defense);
        infile.close();
        cout << "游戏进度已加载。" << endl;
        return true;
    }
    else {
        cout << "无法加载游戏进度。" << endl;
        return false;
    }
}

int main() {
    srand(static_cast<unsigned int>(time(0))); // 初始化随机数种子

    Character player("勇士", 1, 0, 10, 3, 2); // 创建角色
    Character enemy("魔王", 1, 0, 10, 2, 1); // 创建敌人

    cout << "欢迎来到挂机游戏!" << endl;

    // 加载游戏进度
    if (loadGame(player)) {
        cout << "继续游戏?(y/n)" << endl;
        char choice;
        cin >> choice;
        if (choice == 'n') {
            player = Character("勇士", 1, 0, 10, 3, 2);
            cout << "游戏进度已重置。" << endl;
        }
    }

    while (true) {
        cout << "当前状态:" << endl;
        cout << "等级:" << player.level << endl;
        cout << "经验值:" << player.exp << endl;
        cout << "生命值:" << player.hp << "/" << player.maxHp << endl;
        cout << "攻击力:" << player.attack << endl;
        cout << "防御力:" << player.defense << endl;

        cout << "请选择操作:" << endl;
        cout << "1. 战斗" << endl;
        cout << "2. 保存游戏进度" << endl;
        cout << "3. 退出游戏" << endl;
        int choice;
        cin >> choice;

        switch (choice) {
        case 1:
            player.fight(enemy);
            break;
        case 2:
            saveGame(player);
            break;
        case 3:
            return 0;
        default:
            cout << "无效的选择。" << endl;
            break;
        }
    }

    return 0;
}

文件下载

百度网盘:https://pan.baidu.com/s/1rUbr6NuXd79Mdv6_MGJKbQ
提取码:nzx9

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值