c语言植物大战僵尸代码
时间: 2023-06-26 17:06:33 浏览: 215
以下是一个简单的植物大战僵尸的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 5
#define COL 9
#define SUNFLOWER 1
#define PEASHOOTER 2
#define ZOMBIE 3
void printGrid(int grid[ROW][COL], int sunCount);
int checkWin(int grid[ROW][COL]);
int checkLose(int grid[ROW][COL]);
void generateSun(int grid[ROW][COL], int *sunCount);
void plant(int grid[ROW][COL], int type, int row, int col, int *sunCount);
void shoot(int grid[ROW][COL]);
void advanceZombies(int grid[ROW][COL]);
void spawnZombie(int grid[ROW][COL]);
int main() {
int grid[ROW][COL] = {0};
int sunCount = 50;
srand(time(NULL));
while(1) {
system("clear");
printGrid(grid, sunCount);
// check for win
if(checkWin(grid)) {
printf("\nCongratulations! You have won!\n");
return 0;
}
// check for lose
if(checkLose(grid)) {
printf("\nGame over! The zombies have eaten your brains!\n");
return 0;
}
// generate sun every 5 turns
if(rand() % 5 == 0) {
generateSun(grid, &sunCount);
}
// get user input
int type, row, col;
printf("\nWhat do you want to do?\n");
printf("1. Plant a sunflower (cost: 25 sun)\n");
printf("2. Plant a peashooter (cost: 50 sun)\n");
printf("3. Advance\n");
scanf("%d", &type);
if(type == 1 || type == 2) {
printf("\nWhere do you want to plant it? (row col)\n");
scanf("%d %d", &row, &col);
// check if spot is empty and player has enough sun
if(grid[row-1][col-1] == 0 && sunCount >= (type == 1 ? 25 : 50)) {
plant(grid, type, row-1, col-1, &sunCount);
} else {
printf("\nInvalid move. Try again.\n");
getchar();
}
} else {
// advance turn
shoot(grid);
advanceZombies(grid);
spawnZombie(grid);
}
}
return 0;
}
void printGrid(int grid[ROW][COL], int sunCount) {
printf(" ");
for(int i=1; i<=COL; i++) {
printf("%d ", i);
}
printf("\tSun count: %d\n", sunCount);
for(int i=0; i<ROW; i++) {
printf("%d ", i+1);
for(int j=0; j<COL; j++) {
switch(grid[i][j]) {
case 0:
printf(". ");
break;
case SUNFLOWER:
printf("S ");
break;
case PEASHOOTER:
printf("P ");
break;
case ZOMBIE:
printf("Z ");
break;
}
}
printf("\n");
}
}
int checkWin(int grid[ROW][COL]) {
for(int i=0; i<ROW; i++) {
for(int j=0; j<COL; j++) {
if(grid[i][j] == ZOMBIE) {
return 0;
}
}
}
return 1;
}
int checkLose(int grid[ROW][COL]) {
for(int i=0; i<ROW; i++) {
for(int j=0; j<COL; j++) {
if(grid[i][j] == ZOMBIE) {
if(j == 0) {
return 1;
} else {
break;
}
}
}
}
return 0;
}
void generateSun(int grid[ROW][COL], int *sunCount) {
int row = rand() % ROW;
int col = rand() % COL;
// check if spot is empty
if(grid[row][col] == 0) {
grid[row][col] = SUNFLOWER;
*sunCount += 25;
}
}
void plant(int grid[ROW][COL], int type, int row, int col, int *sunCount) {
grid[row][col] = type;
*sunCount -= (type == 1 ? 25 : 50);
}
void shoot(int grid[ROW][COL]) {
for(int i=0; i<ROW; i++) {
for(int j=0; j<COL; j++) {
if(grid[i][j] == PEASHOOTER) {
for(int k=j+1; k<COL; k++) {
if(grid[i][k] == ZOMBIE) {
grid[i][k] = 0;
break;
}
}
}
}
}
}
void advanceZombies(int grid[ROW][COL]) {
for(int i=0; i<ROW; i++) {
for(int j=COL-1; j>=0; j--) {
if(grid[i][j] == ZOMBIE) {
if(j == 0) {
// zombie reached end, game over
return;
} else {
// move zombie forward
grid[i][j] = 0;
grid[i][j-1] = ZOMBIE;
}
}
}
}
}
void spawnZombie(int grid[ROW][COL]) {
int row = rand() % ROW;
// check if spot is empty
if(grid[row][COL-1] == 0) {
grid[row][COL-1] = ZOMBIE;
}
}
```
这个代码实现了以下功能:
1. 使用二维数组表示游戏场地,其中包含太阳花、豌豆射手和僵尸等元素。
2. 玩家可以在空位上种植太阳花或豌豆射手,也可以选择结束回合。
3. 太阳花每隔一定时间会产生太阳能量,玩家可以用它来种植植物。
4. 豌豆射手会射出豌豆攻击僵尸。
5. 僵尸会从右侧进入游戏场地,向左前进,如果到达左侧则游戏失败。
6. 玩家需要在种植植物的同时防止僵尸的进攻,直到击败所有僵尸为止。
该代码为简单的示例代码,仅供参考。如果你想创建一个完整的植物大战僵尸游戏,你需要添加更多的功能和细节。
阅读全文