神秘塔防游戏,超级高级
2025-12-29 19:54:58
发布于:浙江
额,一不小心让deepseek做了个塔防类游戏

#include <iostream>
#include <windows.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <string>
#include <iomanip>
// 设置光标位置
void setCursorPosition(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 = 1;
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
// 设置文本颜色
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
// 重置文本颜色为默认
void resetColor() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 7 = 默认灰白色
}
struct Zombie {
int x, y;
bool active;
int health; // 僵尸血量
};
struct Bullet {
int x, y;
bool active;
int damage; // 子弹伤害
};
class Game {
private:
int playerX, playerY;
int playerHealth;
int maxHealth;
std::vector<Zombie> zombies;
std::vector<Bullet> bullets;
int baseSpeed;
int spawnCounter;
int speedCounter;
int gameTime;
int score;
int damageCounter;
int bulletMoveCounter;
bool isPlayerHurt;
int hurtEffectCounter;
int weaponLevel; // 武器等级 1=手枪 2=冲锋枪
int zombiesKilled; // 已击杀僵尸数量
int upgradeMessageCounter; // 升级提示计时器
public:
int getWeaponLevel() const { return weaponLevel; };
Game() : playerX(10), playerY(25), playerHealth(200), maxHealth(200),
baseSpeed(1000), spawnCounter(0), speedCounter(0),
gameTime(0), score(0), damageCounter(0), bulletMoveCounter(0),
isPlayerHurt(false), hurtEffectCounter(0), weaponLevel(1),
zombiesKilled(0), upgradeMessageCounter(0) {
std::srand(std::time(0));
hideCursor();
}
void draw() {
// 清屏
system("cls");
// 显示游戏信息(左上角)
double currentTime = gameTime / 1000.0;
std::cout << "时间: " << std::fixed << std::setprecision(2) << currentTime << "秒 | 生命值: " << playerHealth << "/" << maxHealth << " | 僵尸数量: " << getActiveZombieCount() << std::endl;
std::cout << "速度等级: " << (1000 - baseSpeed) / 100 + 1 << " | 得分: " << score << " | 击杀: " << zombiesKilled << "/20" << std::endl;
std::cout << "武器: " << (weaponLevel == 1 ? "手枪" : "冲锋枪") << std::endl;
// 显示升级提示
if (upgradeMessageCounter > 0) {
setColor(10); // 绿色
std::cout << "武器升级!现在是冲锋枪!" << std::endl;
resetColor();
upgradeMessageCounter -= 50;
} else {
std::cout << std::endl;
}
std::cout << std::endl;
// 绘制游戏区域
std::vector<std::string> screen(51, std::string(101, ' '));
// 绘制玩家和武器
if (playerY >= 0 && playerY <= 50 && playerX >= 0 && playerX <= 100) {
screen[playerY][playerX] = 'O';
if (weaponLevel == 1) {
// 手枪 "p="
if (playerX + 1 <= 100) screen[playerY][playerX + 1] = ' ';
if (playerX + 2 <= 100) screen[playerY][playerX + 2] = 'p';
if (playerX + 3 <= 100) screen[playerY][playerX + 3] = '=';
} else {
// 冲锋枪 "pij="
if (playerX + 1 <= 100) screen[playerY][playerX + 1] = ' ';
if (playerX + 2 <= 100) screen[playerY][playerX + 2] = 'p';
if (playerX + 3 <= 100) screen[playerY][playerX + 3] = 'i';
if (playerX + 4 <= 100) screen[playerY][playerX + 4] = 'j';
if (playerX + 5 <= 100) screen[playerY][playerX + 5] = '=';
}
}
// 绘制子弹
for (const auto& bullet : bullets) {
if (bullet.active && bullet.y >= 0 && bullet.y <= 50 && bullet.x >= 0 && bullet.x <= 100) {
screen[bullet.y][bullet.x] = '-';
}
}
// 绘制僵尸
for (const auto& zombie : zombies) {
if (zombie.active && zombie.y >= 0 && zombie.y <= 50 && zombie.x >= 0 && zombie.x <= 100) {
screen[zombie.y][zombie.x] = 'Z';
}
}
// 输出整个屏幕
for (int i = 0; i <= 50; i++) {
// 如果是玩家所在的行且玩家受伤,特殊处理颜色
if (i == playerY && isPlayerHurt) {
for (int j = 0; j <= 100; j++) {
if (j == playerX) {
setColor(12); // 红色
std::cout << 'O';
resetColor();
} else if (weaponLevel == 1) {
// 手枪绘制
if (j == playerX + 1) std::cout << ' ';
else if (j == playerX + 2) std::cout << 'p';
else if (j == playerX + 3) std::cout << '=';
else std::cout << screen[i][j];
} else {
// 冲锋枪绘制
if (j == playerX + 1) std::cout << ' ';
else if (j == playerX + 2) std::cout << 'p';
else if (j == playerX + 3) std::cout << 'i';
else if (j == playerX + 4) std::cout << 'j';
else if (j == playerX + 5) std::cout << '=';
else std::cout << screen[i][j];
}
}
std::cout << std::endl;
} else {
std::cout << screen[i] << std::endl;
}
}
// 控制说明
std::cout << std::endl;
std::cout << "WASD=移动 | 空格=射击 | R=重新开始 | ESC=退出游戏" << std::endl;
}
void update() {
gameTime += 50;
score = gameTime / 100;
// 处理受伤效果
if (isPlayerHurt) {
hurtEffectCounter += 50;
if (hurtEffectCounter >= 10) { // 0.01秒后恢复颜色
isPlayerHurt = false;
hurtEffectCounter = 0;
}
}
// 生成僵尸(每1秒一个)
spawnCounter += 50;
if (spawnCounter >= 1000) {
spawnZombie();
spawnCounter = 0;
}
// 加速机制(每10秒加速一次)
speedCounter += 50;
if (speedCounter >= 10000) {
if (baseSpeed > 200) {
baseSpeed -= 100;
}
speedCounter = 0;
}
// 移动僵尸
static int zombieMoveCounter = 0;
zombieMoveCounter += 50;
if (zombieMoveCounter >= baseSpeed) {
moveZombies();
zombieMoveCounter = 0;
}
// 移动子弹(每0.001秒移动一格)
bulletMoveCounter += 50;
if (bulletMoveCounter >= 1) {
moveBullets();
bulletMoveCounter = 0;
}
// 检测碰撞
checkCollisions();
// 伤害检测(每0.5秒一次)
damageCounter += 50;
if (damageCounter >= 500) {
checkZombieDamage();
damageCounter = 0;
}
// 检查武器升级
checkWeaponUpgrade();
// 检查游戏结束条件
checkGameOver();
}
void spawnZombie() {
// 从右侧随机位置生成僵尸
Zombie zombie;
zombie.x = 100;
zombie.y = std::rand() % 51;
zombie.active = true;
zombie.health = (weaponLevel == 1) ? 1 : 2; // 手枪1血,冲锋枪需要2血
zombies.push_back(zombie);
}
void moveZombies() {
for (auto& zombie : zombies) {
if (zombie.active) {
zombie.x--; // 向左移动
// 检查是否到达左边界
if (zombie.x <= 0) {
gameOver("有僵尸到达了左边界!");
return;
}
}
}
cleanupZombies();
}
void moveBullets() {
for (auto& bullet : bullets) {
if (bullet.active) {
bullet.x++; // 向右移动
// 检查子弹是否超出边界
if (bullet.x > 100) {
bullet.active = false;
}
}
}
cleanupBullets();
}
void shoot() {
// 发射子弹
Bullet bullet;
bullet.x = playerX + (weaponLevel == 1 ? 4 : 6); // 从武器位置发射
bullet.y = playerY;
bullet.active = true;
bullet.damage = 1; // 每发子弹伤害为1
bullets.push_back(bullet);
}
void checkCollisions() {
// 子弹与僵尸碰撞检测
for (auto& bullet : bullets) {
if (!bullet.active) continue;
for (auto& zombie : zombies) {
if (zombie.active && zombie.x == bullet.x && zombie.y == bullet.y) {
// 造成伤害
zombie.health -= bullet.damage;
bullet.active = false;
// 如果僵尸死亡
if (zombie.health <= 0) {
zombie.active = false;
zombiesKilled++;
// 如果不是满血,回复5点生命值
if (playerHealth < maxHealth) {
playerHealth = std::min(maxHealth, playerHealth + 5);
}
score += 10; // 击杀得分
}
break;
}
}
}
}
void checkZombieDamage() {
// 检查玩家是否与僵尸接触
bool isTouchingZombie = false;
for (const auto& zombie : zombies) {
if (zombie.active &&
std::abs(zombie.x - playerX) <= 1 &&
std::abs(zombie.y - playerY) <= 1) {
isTouchingZombie = true;
break;
}
}
// 如果接触到僵尸,扣血并显示受伤效果
if (isTouchingZombie) {
playerHealth -= 10; // 每0.5秒扣10血
isPlayerHurt = true; // 触发受伤效果
hurtEffectCounter = 0; // 重置受伤效果计时器
}
}
void checkWeaponUpgrade() {
// 检查是否达到升级条件
if (weaponLevel == 1 && zombiesKilled >= 20) {
weaponLevel = 2;
upgradeMessageCounter = 3000; // 显示3秒升级提示
}
}
void checkGameOver() {
if (playerHealth <= 0) {
gameOver("你的生命值已归零!");
}
}
void gameOver(const std::string& reason) {
system("cls");
double finalTime = gameTime / 1000.0;
std::cout << "==================================" << std::endl;
std::cout << " 游戏结束!" << std::endl;
std::cout << "原因: " << reason << std::endl;
std::cout << "==================================" << std::endl;
std::cout << "存活时间: " << std::fixed << std::setprecision(2) << finalTime << " 秒" << std::endl;
std::cout << "最终得分: " << score << std::endl;
std::cout << "最终速度等级: " << (1000 - baseSpeed) / 100 + 1 << std::endl;
std::cout << "击杀僵尸数: " << zombiesKilled << std::endl;
std::cout << "最终武器: " << (weaponLevel == 1 ? "手枪" : "冲锋枪") << std::endl;
std::cout << "==================================" << std::endl;
std::cout << "按 R 重新开始,ESC 退出游戏" << std::endl;
while (true) {
if (GetAsyncKeyState('R') & 0x8000) {
reset();
return;
}
if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
exit(0);
}
Sleep(50);
}
}
bool isZombieAt(int x, int y) {
for (const auto& zombie : zombies) {
if (zombie.active && zombie.x == x && zombie.y == y) return true;
}
return false;
}
bool isBulletAt(int x, int y) {
for (const auto& bullet : bullets) {
if (bullet.active && bullet.x == x && bullet.y == y) return true;
}
return false;
}
void cleanupZombies() {
zombies.erase(std::remove_if(zombies.begin(), zombies.end(),
[](const Zombie& z) { return !z.active; }), zombies.end());
}
void cleanupBullets() {
bullets.erase(std::remove_if(bullets.begin(), bullets.end(),
[](const Bullet& b) { return !b.active; }), bullets.end());
}
int getActiveZombieCount() {
return std::count_if(zombies.begin(), zombies.end(),
[](const Zombie& z) { return z.active; });
}
void movePlayer(char direction) {
int newX = playerX;
int newY = playerY;
switch (direction) {
case 'W': newY = std::max(0, playerY - 1); break;
case 'A': newX = std::max(0, playerX - 1); break;
case 'S': newY = std::min(50, playerY + 1); break;
case 'D': newX = std::min(95, playerX + 1); break; // 留出武器空间
}
playerX = newX;
playerY = newY;
}
void reset() {
playerX = 10;
playerY = 25;
playerHealth = 200;
zombies.clear();
bullets.clear();
baseSpeed = 1000;
spawnCounter = 0;
speedCounter = 0;
gameTime = 0;
score = 0;
damageCounter = 0;
bulletMoveCounter = 0;
isPlayerHurt = false;
hurtEffectCounter = 0;
weaponLevel = 1;
zombiesKilled = 0;
upgradeMessageCounter = 0;
}
};
int main() {
Game game;
system("mode con cols=110 lines=60");
std::cout << "=== 僵尸生存游戏 ===" << std::endl;
std::cout << "尽可能长时间生存!按空格键射击僵尸。" << std::endl;
std::cout << "受伤时击杀僵尸可以回复5点生命值。" << std::endl;
std::cout << "击杀20个僵尸后武器会升级为冲锋枪!" << std::endl;
std::cout << "生命值归零或有僵尸到达左边界时游戏结束。" << std::endl;
std::cout << "按任意键开始游戏..." << std::endl;
system("pause > nul");
// 子弹发射冷却
int shootCooldown = 0;
while (true) {
// 退出游戏
if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) break;
// 重新开始游戏
if (GetAsyncKeyState('R') & 0x8000) {
game.reset();
Sleep(200);
}
// 玩家移动
if (GetAsyncKeyState('W') & 0x8000) {
game.movePlayer('W');
}
if (GetAsyncKeyState('A') & 0x8000) {
game.movePlayer('A');
}
if (GetAsyncKeyState('S') & 0x8000) {
game.movePlayer('S');
}
if (GetAsyncKeyState('D') & 0x8000) {
game.movePlayer('D');
}
// 发射子弹(根据武器等级调整冷却时间)
shootCooldown += 50;
int requiredCooldown = (game.getWeaponLevel() == 1) ? 300 : 50; // 手枪0.3秒,冲锋枪0.05秒
if (shootCooldown >= requiredCooldown && (GetAsyncKeyState(VK_SPACE) & 0x8000)) {
game.shoot();
shootCooldown = 0;
}
// 更新游戏状态
game.update();
// 每次循环都重绘
game.draw();
Sleep(50); // 主循环速度
}
std::cout << "感谢游玩!" << std::endl;
return 0;
}
对,就是这样,纯属娱乐,可随便拿去修改,二创,作者不介意
注:此代码无收费,若有粉丝看到有人拿此代码售卖,请告知我:Can I play 瓦洛兰特?
欢迎留言或提意见
全部评论 6
d
6天前 来自 浙江
0d
6天前 来自 浙江
0d
6天前 来自 浙江
0d
6天前 来自 浙江
0d
d
6天前 来自 浙江
0不是,Deepseek做的不属于你的版权吧!



2025-12-30 来自 浙江
0谁发表的谁的版权
2025-12-30 来自 上海
0666
2025-12-30 来自 浙江
0























有帮助,赞一个