用C++个植物大战僵尸游戏
时间: 2023-05-26 07:05:11 浏览: 354
由于您没有提供 C 语言的具体要求,因此以下是一个使用 C++ 编写的植物大战僵尸游戏示例代码:
```
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int ROW = 5; // 行数
const int COLUMN = 10; // 列数
class Plant {
public:
Plant() { // 构造函数
health = 3;
attack = 1;
}
void attackZombie() { // 攻击僵尸
cout << "植物攻击僵尸,使其扣除 " << attack << " 点血量" << endl;
}
int getHealth() { // 获取生命值
return health;
}
void loseHealth() { // 扣除生命值
health--;
}
private:
int health; // 生命值
int attack; // 攻击力
};
class Zombie {
public:
Zombie() { // 构造函数
health = 5;
attack = 2;
}
void attackPlant() { // 攻击植物
cout << "僵尸攻击植物,使其扣除 " << attack << " 点血量" << endl;
}
int getHealth() { // 获取生命值
return health;
}
void loseHealth() { // 扣除生命值
health--;
}
private:
int health; // 生命值
int attack; // 攻击力
};
class Board {
public:
Board() { // 构造函数
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COLUMN; j++) {
board[i][j] = NULL; // 初始化所有格子为空
}
}
}
void printBoard() { // 打印游戏界面
cout << endl;
cout << "------------------------" << endl;
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COLUMN; j++) {
if(board[i][j] != NULL) {
if(dynamic_cast<Plant*>(board[i][j])) { // 如果格子上是植物
cout << "| P ";
} else { // 否则格子上是僵尸
cout << "| Z ";
}
} else { // 如果格子为空
cout << "| ";
}
}
cout << "|" << endl;
cout << "------------------------" << endl;
}
cout << endl;
}
bool isPlantOnRow(int row, int column) { // 判断该行是否有植物
for(int j = 0; j < COLUMN; j++) {
if(board[row][j] != NULL && dynamic_cast<Plant*>(board[row][j])) {
return true;
}
}
return false;
}
void addPlant(int row, int column) { // 在指定位置添加植物
if(board[row][column] == NULL) {
board[row][column] = new Plant();
} else {
cout << "该位置已经有植物了" << endl;
}
}
void addZombie(int row, int column) { // 在指定位置添加僵尸
if(board[row][column] == NULL) {
board[row][column] = new Zombie();
} else {
cout << "该位置已经有僵尸了" << endl;
}
}
bool isPlantDead(int row, int column) { // 判断植物是否死亡
if(board[row][column] != NULL && dynamic_cast<Plant*>(board[row][column])) {
if(board[row][column]->getHealth() <= 0) {
delete board[row][column];
board[row][column] = NULL;
return true;
}
}
return false;
}
bool isZombieDead(int row, int column) { // 判断僵尸是否死亡
if(board[row][column] != NULL && dynamic_cast<Zombie*>(board[row][column])) {
if(board[row][column]->getHealth() <= 0) {
delete board[row][column];
board[row][column] = NULL;
return true;
}
}
return false;
}
void plantAttack(int row, int column) { // 植物攻击
for(int j = 0; j < COLUMN; j++) {
if(board[row][j] != NULL && dynamic_cast<Zombie*>(board[row][j])) {
board[row][j]->loseHealth();
if(isZombieDead(row, j)) {
cout << "僵尸死亡" << endl;
}
}
}
}
void zombieAttack(int row, int column) { // 僵尸攻击
for(int j = 0; j < COLUMN; j++) {
if(board[row][j] != NULL && dynamic_cast<Plant*>(board[row][j])) {
board[row][j]->loseHealth();
if(isPlantDead(row, j)) {
cout << "植物死亡" << endl;
}
}
}
}
private:
Plant* plant;
Zombie* zombie;
int gameTime;
void* board[ROW][COLUMN];
};
int main() {
srand((unsigned int)time(NULL)); // 随机数种子
Board board;
bool running = true;
int gameTime = 0;
while(running) {
cout << "------------------------------" << endl;
cout << "游戏时间:" << gameTime << endl;
cout << "------------------------------" << endl;
board.printBoard();
// 每隔 5 个游戏时间随机添加一个植物
if(gameTime % 5 == 0) {
int row = rand() % ROW;
int column = rand() % COLUMN;
if(!board.isPlantOnRow(row, column)) {
cout << "在第 " << row + 1 << " 行第 " << column + 1 << " 列添加了一个植物" << endl;
board.addPlant(row, column);
}
}
// 每隔 3 个游戏时间随机添加一个僵尸
if(gameTime % 3 == 0) {
int row = rand() % ROW;
int column = rand() % COLUMN;
if(board.isPlantOnRow(row, column)) {
cout << "在第 " << row + 1 << " 行第 " << column + 1 << " 列添加了一个僵尸" << endl;
board.addZombie(row, column);
}
}
// 攻击阶段
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COLUMN; j++) {
if(board.isPlantDead(i, j)) {
cout << "在第 " << i + 1 << " 行第 " << j + 1 << " 列的植物死亡" << endl;
}
if(board.isZombieDead(i, j)) {
cout << "在第 " << i + 1 << " 行第 " << j + 1 << " 列的僵尸死亡" << endl;
}
if(board.isPlantOnRow(i, j) && board.board[i][j] != NULL && dynamic_cast<Plant*>(board.board[i][j])) {
board.plantAttack(i, j);
}
if(board.board[i][j] != NULL && dynamic_cast<Zombie*>(board.board[i][j])) {
board.zombieAttack(i, j);
}
}
}
cout << "------------------------------" << endl;
gameTime++;
system("pause");
system("cls");
}
return 0;
}
```
阅读全文