PvZ但是C++简易版
2026-07-01 20:27:23
发布于:重庆
本人写了写了个PvZ简易版,共花4个小时,制作不易,点个赞吧,欢迎反馈
修复了僵尸无限出现、向日葵发射豌豆的bug
操作方法: 方向键选格,数字键1、2、3、4分别代表选择植物,豌豆射手、向日葵、樱桃炸弹、坚果,按下空格放置
总共360+行代码
#include<bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;
int plts[5][9]; //1.豌 2.葵 3.樱 4.坚果
int zoms[5][9]; //1.普 2.障 3.桶
int buls[5][9];
int zmhp[5][9];
int plhp[5][9];
int suns = 150;
int zonum = 30;
int wave = 1;
bool over = false;
string zomtype[3] = {"普通僵尸", "路障僵尸", "铁通僵尸"};
string plttype[4] = {"豌豆射手", "向日葵", "樱桃炸弹", "坚果"};
clock_t zAtk[5][9];
clock_t zMov[5][9];
clock_t bTim[5][9];
double zSpd[5][9];
clock_t sunt[5][9];
double suniv[5][9];
clock_t peat[5][9];
double peaiv[5][9];
int boomt[5][9];
int cuow, cuol;
int sel = 1;
clock_t zomLast;
int zomwait;
bool firstzom = true;
int sun[5][9];
clock_t sunlast;
int sunwait;
bool zomnull(){
for (int i = 0;i < 5;i ++){
for (int j = 0;j < 9;j ++){
if (zoms[i][j] != 0) return false;
}
}
return true;
}
int randint(int min, int max){
return rand() % (max - min + 1) + min;
}
void bulforward(){
clock_t now = clock();
for (int i = 0;i < 5;i ++){
for (int j = 8;j >= 0;j --){
if (buls[i][j] == 0) continue;
double dt = 1.0 * (now - bTim[i][j]) / CLOCKS_PER_SEC;
if (dt < 0.5) continue;
if (j == 8){
buls[i][j] = 0;
continue;
}
buls[i][j] = 0;
buls[i][j + 1] = 1;
bTim[i][j + 1] = clock();
if (zoms[i][j + 1] != 0){
zmhp[i][j + 1] -= 20;
if (zmhp[i][j + 1] <= 0){
zmhp[i][j + 1] = 0;
zoms[i][j + 1] = 0;
}
buls[i][j + 1] = 0;
}
}
}
}
void zomforward(){
clock_t now = clock();
for (int i = 0;i < 5;i ++){
for (int j = 8;j >= 0;j --){
if (zoms[i][j] == 0) continue;
if (j == 0){
over = true;
return;
}
if (plts[i][j - 1] != 0){
double dt = 1.0 * (now - zAtk[i][j]) / CLOCKS_PER_SEC;
if (dt >= 0.5){
plhp[i][j - 1] -= 50;
zAtk[i][j] = clock();
if (plhp[i][j - 1] <= 0){
plts[i][j - 1] = 0;
plhp[i][j - 1] = 0;
}
}
continue;
}
double dt = 1.0 * (now - zMov[i][j]) / CLOCKS_PER_SEC;
if (dt >= zSpd[i][j]){
zoms[i][j - 1] = zoms[i][j];
zoms[i][j] = 0;
zmhp[i][j - 1] = zmhp[i][j];
zMov[i][j - 1] = clock();
zSpd[i][j - 1] = zSpd[i][j];
}
}
}
}
void zomspawn() {
if (zonum <= 0) return;
clock_t now = clock();
double dt = 1.0 * (now - zomLast) / CLOCKS_PER_SEC;
if (dt >= zomwait) {
int r = randint(0, 4);
int type;
if (firstzom) {
type = 1;
firstzom = false;
} else {
type = randint(1, 3);
}
if (zoms[r][8] == 0) {
zonum -= 1;
zoms[r][8] = type;
if (type == 1) zmhp[r][8] = 100;
else if (type == 2) zmhp[r][8] = 260;
else if (type == 3) zmhp[r][8] = 1100;
zAtk[r][8] = clock();
zMov[r][8] = clock();
zSpd[r][8] = randint(2, 3);
}
zomLast = clock();
zomwait = randint(7, 10);
}
}
void bomb(int x, int y){
for (int i = x - 1; i <= x + 1; i++){
for (int j = y - 1; j <= y + 1; j++){
if (i >= 0 && i < 5 && j >= 0 && j < 9) {
zoms[i][j] = 0;
zmhp[i][j] = 0;
boomt[i][j] = 10;
}
}
}
plts[x][y] = 0;
plhp[x][y] = 0;
}
void keycheck() {
if (!_kbhit()) return;
int key = _getch();
if (key == 224) {
int dir = _getch();
if (dir == 72 && cuow > 0) cuow--;
else if (dir == 80 && cuow < 4) cuow++;
else if (dir == 75 && cuol > 0) cuol--;
else if (dir == 77 && cuol < 8) cuol++;
if (sun[cuow][cuol] > 0) {
suns += 25;
sun[cuow][cuol] = 0;
}
return;
}
if (key == '1'){
sel = 1;
return;
}else if (key == '2'){
sel = 2;
return;
}else if (key == '3'){
sel = 3;
return;
}else if (key == '4'){
sel = 4;
return;
}
if (key == ' ') {
int r = cuow, c = cuol;
if (plts[r][c] != 0) return;
int cost;
if (sel == 1) cost = 100;
else if (sel == 2) cost = 50;
else if (sel == 3) cost = 225;
else if (sel == 4) cost = 75;
else return;
if (suns < cost) return;
suns -= cost;
if (sel == 3) {
bomb(r, c);
return;
}
plts[r][c] = sel;
if (sel == 1) {
plhp[r][c] = 300;
peat[r][c] = clock();
peaiv[r][c] = randint(8, 12) * 0.1;
}
else if (sel == 2) {
plhp[r][c] = 300;
sunt[r][c] = clock();
suniv[r][c] = randint(5, 9);
}
else if (sel == 4) {
plhp[r][c] = 4000;
}
return;
}
}
void pltmln(){
clock_t now = clock();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if (plts[i][j] == 1) {
bool havezom = false;
for (int k = j + 1; k < 9; k++) {
if (zoms[i][k] != 0) {
havezom = true;
break;
}
}
if (!havezom) continue;
double dt = 1.0 * (now - peat[i][j]) / CLOCKS_PER_SEC;
if (dt >= peaiv[i][j]) {
int bulc = j + 1;
if (bulc < 9 && buls[i][bulc] == 0) {
buls[i][bulc] = 1;
bTim[i][bulc] = clock();
if (zoms[i][bulc] == 1) {
buls[i][bulc] = 0;
zmhp[i][bulc] -= 20;
if (zmhp[i][bulc] <= 0) {
zmhp[i][bulc] = 0;
zoms[i][bulc] = 0;
}
}
peat[i][j] = clock();
peaiv[i][j] = randint(8, 12) * 0.1;
}
}
}
else if (plts[i][j] == 2) {
double dt = 1.0 * (now - sunt[i][j]) / CLOCKS_PER_SEC;
if (dt >= suniv[i][j]) {
suns += 25;
sunt[i][j] = clock();
suniv[i][j] = randint(5, 9);
}
}
}
}
}
void sunspawn() {
clock_t now = clock();
if (1.0 * (now - sunlast) / CLOCKS_PER_SEC >= sunwait) {
int r = randint(0, 4);
int c = randint(0, 8);
if (plts[r][c] == 0 && zoms[r][c] == 0 && buls[r][c] == 0 && sun[r][c] == 0) {
sun[r][c] = 150;
}
sunlast = clock();
sunwait = randint(5, 10);
}
}
void updatesun(){
for (int i = 0;i < 5;i ++){
for (int j = 0;j < 9;j ++){
if (sun[i][j] > 0) sun[i][j] --;
}
}
}
void drawGame() {
system("cls");
wave = 3 - ceil(zonum / 15);
cout << "阳光: " << suns << " 波次: " << wave << endl;
cout << "当前选择: ";
if (sel == 1) cout << "豌豆射手(P) 100阳光";
else if (sel == 2) cout << "向日葵(S) 50阳光";
else if (sel == 3) cout << "樱桃炸弹(C) 225阳光";
else if (sel == 4) cout << "坚果墙(N) 75阳光";
cout << " 光标: (" << cuow << "," << cuol << ")" << endl;
cout << " +---+---+---+---+---+---+---+---+---+" << endl;
for (int i = 0; i < 5; i++) {
cout << i << " |";
for (int j = 0; j < 9; j++) {
string cell = " ";
if (boomt[i][j] > 0) {
cell = "X";
boomt[i][j]--;
}
else if (zoms[i][j] != 0) {
if (zoms[i][j] == 1) cell = "Z";
else if (zoms[i][j] == 2) cell = "D";
else if (zoms[i][j] == 3) cell = "T";
}
else if (plts[i][j] != 0) {
if (plts[i][j] == 1) cell = "P";
else if (plts[i][j] == 2) cell = "S";
else if (plts[i][j] == 3) cell = "C";
else if (plts[i][j] == 4) cell = "N";
}
else if (buls[i][j] != 0) {
cell = "*";
}
else if (sun[i][j] > 0) {
cell = "$";
}
if (i == cuow && j == cuol) {
cout << '[' << cell << ']';
} else {
cout << ' ' << cell << ' ';
}
cout << '|';
}
cout << endl;
cout << " +---+---+---+---+---+---+---+---+---+" << endl;
}
if (zoms[cuow][cuol] != 0) cout << zomtype[zoms[cuow][cuol] - 1] << " 血量: " << zmhp[cuow][cuol] << endl;
else if (plts[cuow][cuol] != 0) cout << plttype[plts[cuow][cuol] - 1] << " 血量: " << plhp[cuow][cuol] << endl;
else cout << "空白草地" << endl;
}
int main(){
srand((unsigned)time(NULL));
memset(bTim, 0, sizeof(bTim));
memset(zAtk, 0, sizeof(zAtk));
memset(zMov, 0, sizeof(zMov));
memset(sunt, 0, sizeof(sunt));
memset(peat, 0, sizeof(peat));
memset(boomt, 0, sizeof(boomt));
memset(sun, 0, sizeof(sun));
sunlast = clock();
sunwait = randint(5, 10);
zomLast = clock();
zomwait = randint(8, 13);
firstzom = true;
cuow = 2;
cuol = 4;
while (!over) {
bulforward();
zomforward();
keycheck();
zomspawn();
pltmln();
drawGame();
sunspawn();
updatesun();
Sleep(30);
}
cout << "游戏结束!" << endl;
return 0;
}
都看到这了,点点赞吧
全部评论 2
咋玩

1周前 来自 广东
1方向键选格,数字键1、2、3、4分别代表选择植物,豌豆射手、向日葵、樱桃炸弹、坚果,按下空格放置
1周前 来自 重庆
0阳光太少了 僵尸太多了
1周前 来自 广东
1我去调整一下出怪速度
1周前 来自 重庆
0
1
1周前 来自 浙江
0
























有帮助,赞一个