fdesxzccsdahcvyvvggv
2026-11-05 16:35:22
发布于:浙江
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <string>
#include <algorithm>
// --- 常量定义 ---
const int ROWS = 5;
const int COLS = 9;
const int CELL_WIDTH = 4; // 每个格子的显示宽度
// --- 枚举与结构体 ---
enum EntityType {
EMPTY = 0,
SUNFLOWER = 1,
PEASHOOTER = 2,
ZOMBIE = 3,
BULLET = 4,
SUN = 5
};
struct Entity {
int type;
int row;
int col;
int hp;
int timer; // 用于攻击冷却或生成阳光
int moveTimer; // 用于僵尸移动
};
// --- 全局变量 ---
std::vector<Entity> entities;
int sunPoints = 50;
bool gameRunning = true;
int wave = 1;
int zombiesKilled = 0;
// --- 辅助函数:设置控制台光标位置 ---
void setCursor(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// --- 辅助函数:隐藏光标 ---
void hideCursor() {
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.dwSize = 100;
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
// --- 游戏逻辑:生成实体 ---
void spawnEntity(int type, int row, int col) {
Entity e;
e.type = type;
e.row = row;
e.col = col;
e.timer = 0;
e.moveTimer = 0;
if (type == SUNFLOWER) {
e.hp = 3;
e.timer = 0; // 阳光生成计时器
} else if (type == PEASHOOTER) {
e.hp = 3;
e.timer = 0; // 射击冷却
} else if (type == ZOMBIE) {
e.hp = 5;
e.col = COLS - 1; // 僵尸从最右侧生成
e.moveTimer = 0;
} else if (type == BULLET) {
e.hp = 1;
} else if (type == SUN) {
e.hp = 1;
e.timer = 100; // 阳光存在时间
}
entities.push_back(e);
}
// --- 游戏逻辑:更新状态 ---
void updateGame() {
// 1. 植物行为
for (auto& e : entities) {
if (e.type == SUNFLOWER) {
e.timer++;
if (e.timer >= 100) { // 每100帧生成一个阳光
spawnEntity(SUN, e.row, e.col);
e.timer = 0;
}
} else if (e.type == PEASHOOTER) {
e.timer++;
// 检查同行是否有僵尸
bool hasZombie = false;
for (const auto& z : entities) {
if (z.type == ZOMBIE && z.row == e.row && z.col > e.col) {
hasZombie = true;
break;
}
}
if (hasZombie && e.timer >= 60) { // 每60帧发射一颗子弹
spawnEntity(BULLET, e.row, e.col + 1);
e.timer = 0;
}
}
}
// 2. 子弹移动与碰撞
for (auto it = entities.begin(); it != entities.end(); ) {
if (it->type == BULLET) {
it->col++; // 向右移动
if (it->col >= COLS) {
it = entities.erase(it); // 超出边界移除
continue;
}
// 检测碰撞僵尸
bool hit = false;
for (auto& z : entities) {
if (z.type == ZOMBIE && z.row == it->row && z.col == it->col) {
z.hp--;
hit = true;
if (z.hp <= 0) {
zombiesKilled++;
}
break;
}
}
if (hit) {
it = entities.erase(it);
continue;
}
}
++it;
}
// 3. 僵尸移动与攻击
for (auto it = entities.begin(); it != entities.end(); ) {
if (it->type == ZOMBIE) {
it->moveTimer++;
// 检测前方是否有植物
bool blocked = false;
for (const auto& p : entities) {
if ((p.type == SUNFLOWER || p.type == PEASHOOTER) &&
p.row == it->row && p.col == it->col - 1) {
blocked = true;
// 攻击植物
const_cast<Entity&>(p).hp--; // 注意:这里需要小心const正确性,实际项目中建议用指针或引用管理
if (const_cast<Entity&>(p).hp <= 0) {
// 植物死亡逻辑需在外部处理或标记删除,此处简化处理
}
break;
}
}
if (!blocked && it->moveTimer >= 30) { // 每30帧移动一格
it->col--;
it->moveTimer = 0;
}
if (it->col < 0) {
gameRunning = false; // 僵尸进屋,游戏结束
return;
}
if (it->hp <= 0) {
it = entities.erase(it);
continue;
}
}
++it;
}
// 清理死亡植物
entities.erase(std::remove_if(entities.begin(), entities.end(), [](const Entity& e) {
return (e.type == SUNFLOWER || e.type == PEASHOOTER) && e.hp <= 0;
}), entities.end());
// 4. 阳光收集逻辑 (自动收集简化版,或者按空格收集)
for (auto it = entities.begin(); it != entities.end(); ) {
if (it->type == SUN) {
it->timer--;
if (it->timer <= 0) {
sunPoints += 25;
it = entities.erase(it);
continue;
}
}
++it;
}
// 5. 生成新僵尸 (简单波次逻辑)
if (rand() % 200 == 0) { // 随机生成
int row = rand() % ROWS;
spawnEntity(ZOMBIE, row, COLS - 1);
}
}
// --- 渲染引擎 ---
void render() {
system("cls");
// 顶部状态栏
std::cout << "=== Console PVZ ===" << std::endl;
std::cout << "Sun: " << sunPoints << " | Wave: " << wave << " | Killed: " << zombiesKilled << std::endl;
std::cout << "Controls: 1-Sunflower(50), 2-PeaShooter(100), Space-Collect Sun, Esc-Quit" << std::endl;
std::cout << "--------------------------------------------------" << std::endl;
// 绘制网格
for (int r = 0; r < ROWS; ++r) {
for (int c = 0; c < COLS; ++c) {
std::cout << "[";
bool drawn = false;
for (const auto& e : entities) {
if (e.row == r && e.col == c) {
switch (e.type) {
case SUNFLOWER: std::cout << "\033[33mS\033[0m"; break; // 黄色
case PEASHOOTER: std::cout << "\033[32mP\033[0m"; break; // 绿色
case ZOMBIE: std::cout << "\033[31mZ\033[0m"; break; // 红色
case BULLET: std::cout << "*"; break;
case SUN: std::cout << "\033[33mO\033[0m"; break; // 阳光
}
drawn = true;
break; // 一个格子只显示一个主要实体
}
}
if (!drawn) std::cout << " ";
std::cout << "] ";
}
std::cout << std::endl;
}
// 底部提示
std::cout << "--------------------------------------------------" << std::endl;
if (sunPoints < 50) std::cout << "Not enough sun for Sunflower!" << std::endl;
}
// --- 输入处理 ---
void handleInput() {
if (_kbhit()) {
char key = _getch();
if (key == 27) { // ESC
gameRunning = false;
} else if (key == '1') { // 种植向日葵
// 简化:默认种在第一行空位,实际应支持鼠标或方向键选择
for (int c = 0; c < COLS; ++c) {
bool occupied = false;
for (const auto& e : entities) {
if (e.row == 0 && e.col == c && (e.type == SUNFLOWER || e.type == PEASHOOTER)) {
occupied = true; break;
}
}
if (!occupied && sunPoints >= 50) {
spawnEntity(SUNFLOWER, 0, c);
sunPoints -= 50;
break;
}
}
} else if (key == '2') { // 种植豌豆射手
for (int c = 0; c < COLS; ++c) {
bool occupied = false;
for (const auto& e : entities) {
if (e.row == 0 && e.col == c && (e.type == SUNFLOWER || e.type == PEASHOOTER)) {
occupied = true; break;
}
}
if (!occupied && sunPoints >= 100) {
spawnEntity(PEASHOOTER, 0, c);
sunPoints -= 100;
break;
}
}
} else if (key == ' ') { // 收集阳光
for (auto it = entities.begin(); it != entities.end(); ) {
if (it->type == SUN) {
sunPoints += 25;
it = entities.erase(it);
} else {
++it;
}
}
}
}
}
int main() {
srand(time(0));
hideCursor();
// 初始赠送一些阳光
sunPoints = 150;
while (gameRunning) {
render();
handleInput();
updateGame();
Sleep(50); // 控制游戏速度,约20FPS
}
setCursor(0, ROWS + 5);
if (zombiesKilled > 0) {
std::cout << "Game Over! Zombies ate your brains!" << std::endl;
} else {
std::cout << "Exited." << std::endl;
}
return 0;
}
这里空空如也


















有帮助,赞一个