用 C++ 制作简单的恶魔城打怪游戏

本示例代码演示了如何使用 C++ 编写一个简单的类恶魔城打怪游戏。玩家需要依次与三个怪物战斗,直到玩家死亡或所有怪物被击败。

代码示例

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// 怪物类
class Monster {
private:
    string name;
    int health;
    int attack;

public:
    Monster(string n, int h, int a) : name(n), health(h), attack(a) {}

    bool isAlive() {
        return health > 0;
    }

    void takeDamage(int damage) {
        health -= damage;
    }

    int getAttack() {
        return attack;
    }

    string getName() {
        return name;
    }
};

// 玩家类
class Player {
private:
    string name;
    int health;
    int attack;

public:
    Player(string n, int h, int a) : name(n), health(h), attack(a) {}

    bool isAlive() {
        return health > 0;
    }

    void takeDamage(int damage) {
        health -= damage;
    }

    int getAttack() {
        return attack;
    }

    string getName() {
        return name;
    }
};

// 游戏类
class Game {
private:
    Player player;
    vector<Monster> monsters;

public:
    Game(string playerName, int playerHealth, int playerAttack) : player(playerName, playerHealth, playerAttack) {
        Monster monster1('怪物1', 50, 10);
        Monster monster2('怪物2', 60, 15);
        Monster monster3('怪物3', 70, 20);
        monsters.push_back(monster1);
        monsters.push_back(monster2);
        monsters.push_back(monster3);
    }

    void start() {
        cout << '欢迎来到恶魔城打怪游戏!' << endl;
        cout << '玩家:' << player.getName() << ',生命值:' << player.isAlive() << ',攻击力:' << player.getAttack() << endl;

        int round = 1;
        while (player.isAlive() && !monsters.empty()) {
            cout << '第' << round << '回合:' << endl;
            Monster currentMonster = monsters.front();
            cout << '怪物:' << currentMonster.getName() << ',生命值:' << currentMonster.isAlive() << ',攻击力:' << currentMonster.getAttack() << endl;

            // 玩家攻击怪物
            currentMonster.takeDamage(player.getAttack());
            cout << '玩家对怪物造成了 ' << player.getAttack() << ' 点伤害' << endl;

            // 怪物攻击玩家
            player.takeDamage(currentMonster.getAttack());
            cout << '怪物对玩家造成了 ' << currentMonster.getAttack() << ' 点伤害' << endl;

            // 检查怪物是否死亡
            if (!currentMonster.isAlive()) {
                cout << '怪物 ' << currentMonster.getName() << ' 被击败了!' << endl;
                monsters.erase(monsters.begin());
            }

            round++;
        }

        if (player.isAlive()) {
            cout << '恭喜玩家 ' << player.getName() << ' 成功通关!' << endl;
        } else {
            cout << '玩家 ' << player.getName() << ' 被怪物击败了!' << endl;
        }
    }
};

int main() {
    Game game('玩家1', 100, 20);
    game.start();

    return 0;
}

游戏流程

  1. 初始化: 创建玩家和怪物对象,设置初始生命值和攻击力。
  2. 回合制战斗: 玩家和怪物轮流攻击对方,造成伤害。
  3. 判断是否死亡: 每个回合结束时判断玩家和怪物是否生命值为零,如果死亡则移除该角色。
  4. 游戏结束: 当所有怪物被击败或玩家死亡时,游戏结束。

代码解析

  1. 类定义: 使用 class 关键字定义玩家和怪物类,包含生命值、攻击力等属性,以及攻击、判断是否存活等方法。
  2. 游戏类: 游戏类管理玩家和怪物,控制游戏流程,包含 start() 方法开始游戏。
  3. 战斗逻辑: start() 方法使用循环控制战斗回合,玩家和怪物轮流攻击对方,并判断是否死亡。

扩展方向

  1. 增加地图: 可以使用二维数组或其他数据结构来模拟游戏地图,玩家可以在地图上移动并与怪物战斗。
  2. 增加道具: 可以添加道具系统,例如生命药水、攻击药水等,玩家可以拾取道具提升属性。
  3. 增加技能: 可以为玩家和怪物添加技能系统,例如火焰攻击、冰冻攻击等,增加游戏乐趣。

总结

本示例代码提供了一个简单的恶魔城打怪游戏的框架,你可以根据自己的需求进行修改和扩展,逐步学习游戏开发的基本知识。

注意: 本代码只提供了一个基本框架,需要根据具体需求进行完善和改进。

C++ 恶魔城打怪游戏代码示例:入门级教程

原文地址: https://www.cveoy.top/t/topic/ffLW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录