由于植物大战僵尸游戏涉及到的代码较多,这里给出一个简化版的示例代码,仅供参考。
```cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
class Plant {
public:
virtual void attack() = 0;
};
class Peashooter : public Plant {
public:
void attack() {
cout << "Peashooter attacks zombies!" << endl;
}
};
class Zombie {
public:
virtual void attack() = 0;
};
class BasicZombie : public Zombie {
public:
void attack() {
cout << "BasicZombie attacks plants!" << endl;
}
};
int main() {
srand(time(0));
Plant* plant = new Peashooter();
Zombie* zombie = new BasicZombie();
while (true) {
plant->attack();
zombie->attack();
int randomNum = rand() % 10;
if (randomNum == 0) {
cout << "A zombie has been defeated!" << endl;
break;
}
}
delete plant;
delete zombie;
return 0;
}
```
这个简化版的游戏中,有一个植物(Peashooter)和一个僵尸(BasicZombie)。游戏会一直进行下去,直到有僵尸被击败。这只是一个非常简化的版本,实际的植物大战僵尸游戏会涉及到更多的角色、关卡、道具等元素。