以下是一个相对复杂的 C++ 模拟植物大战僵尸游戏的代码示例,该代码实现了游戏的基本逻辑,包括植物和僵尸的生成、移动、攻击等。 ```cpp #include <iostream> #include <vector> #include <cstdlib> #include <ctime> // 植物基类 class Plant { protected: int health; // 植物生命值 int attack; // 植物攻击力 public: Plant(int h, int a) : health(h), attack(a) {} virtual void attackZombie(class Zombie& zombie) = 0; // 攻击僵尸的纯虚函数 bool isAlive() { return health > 0; } void takeDamage(int damage) { health -= damage; } }; // 豌豆射手类,继承自植物基类 class Peashooter : public Plant { public: Peashooter() : Plant(100, 20) {} void attackZombie(Zombie& zombie) override { zombie.takeDamage(attack); } }; // 僵尸基类 class Zombie { protected: int health; // 僵尸生命值 int attack; // 僵尸攻击力 int position; // 僵尸位置 public: Zombie(int h, int a, int p) : health(h), attack(a), position(p) {} void move() { position--; } void attackPlant(Plant& plant) { plant.takeDamage(attack); } bool isAlive() { return health > 0; } void takeDamage(int damage) { health -= damage; } int getPosition() { return position; } }; // 普通僵尸类,继承自僵尸基类 class NormalZombie : public Zombie { public: NormalZombie(int p) : Zombie(200, 10, p) {} }; // 游戏类 class Game { private: std::vector<Plant*> plants; // 存储植物的向量 std::vector<Zombie*> zombies; // 存储僵尸的向量 int laneLength; // 车道长度 public: Game(int length) : laneLength(length) { srand(time(0)); } ~Game() { for (auto plant : plants) { delete plant; } for (auto zombie : zombies) { delete zombie; } } void addPlant(Plant* plant) { plants.push_back(plant); } void addZombie(Zombie* zombie) { zombies.push_back(zombie); } void update() { // 僵尸移动 for (auto zombie : zombies) { if (zombie->isAlive()) { zombie->move(); } } // 检查僵尸和植物的碰撞 for (auto zombie : zombies) { if (zombie->isAlive()) { for (auto plant : plants) { if (plant->isAlive() && zombie->getPosition() == 0) { zombie->attackPlant(*plant); } } } } // 植物攻击僵尸 for (auto plant : plants) { if (plant->isAlive()) { for (auto zombie : zombies) { if (zombie->isAlive() && zombie->getPosition() <= 2) { plant->attackZombie(*zombie); } } } } // 移除死亡的植物和僵尸 for (auto it = plants.begin(); it != plants.end();) { if (!(*it)->isAlive()) { delete *it; it = plants.erase(it); } else { ++it; } } for (auto it = zombies.begin(); it != zombies.end();) { if (!(*it)->isAlive()) { delete *it; it = zombies.erase(it); } else { ++it; } } } void printStatus() { std::cout << "Plants: "; for (auto plant : plants) { std::cout << "Health: " << (plant->isAlive()? std::to_string(plant->isAlive()) : "Dead") << " "; } std::cout << std::endl; std::cout << "Zombies: "; for (auto zombie : zombies) { std::cout << "Position: " << zombie->getPosition() << ", Health: " << (zombie->isAlive()? std::to_string(zombie->isAlive()) : "Dead") << " "; } std::cout << std::endl; } bool isGameOver() { for (auto zombie : zombies) { if (zombie->isAlive() && zombie->getPosition() < 0) { return true; } } return plants.empty(); } }; int main() { Game game(10); // 添加植物 game.addPlant(new Peashooter()); // 添加僵尸 game.addZombie(new NormalZombie(9)); while (!game.isGameOver()) { game.update(); game.printStatus(); } std::cout << "Game Over!" << std::endl; return 0; } ``` 代码说明: 1. `Plant` 类是植物的基类,包含生命值、攻击力等属性,以及攻击僵尸的纯虚函数。 2. `Peashooter` 类继承自 `Plant` 类,实现了具体的攻击逻辑。 3. `Zombie` 类是僵尸的基类,包含生命值、攻击力、位置等属性,以及移动、攻击植物等方法。 4. `NormalZombie` 类继承自 `Zombie` 类,是普通僵尸的具体实现。 5. `Game` 类负责管理游戏的整体逻辑,包括添加植物和僵尸、更新游戏状态、检查游戏是否结束等。 6. `main` 函数创建游戏对象,添加植物和僵尸,然后进入游戏循环,直到游戏结束。 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 内存管理:在使用动态内存分配(如 `new`)时,要确保在不需要时使用 `delete` 释放内存,避免内存泄漏。 2. 边界检查:在处理僵尸的位置和移动时,要进行边界检查,避免越界访问。 3. 多态使用:在使用多态时,要确保基类的虚函数被正确实现,否则可能会导致运行时错误。 [2025-08-16 19:56:53 | AI写代码神器 | 1551点数解答]