#include <iostream>
#include <conio.h>
#include <windows.h>
#include <random>
#include <chrono>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <thread>
#include <fstream>
#include <string>
#include <map>
using namespace std;
const int MAP_WIDTH = 20;
const int MAP_HEIGHT = 19;
const int INITIAL_HEALTH = 5;
const int BOSS_HEALTH = 5;
const int ENEMY_RESPAWN_TIME = 5;
const int COIN_RESPAWN_TIME = 10;
const int ANIMATION_DURATION = 300;
const int SURVIVAL_WIN_TIME = 30;
const int COIN_WIN_CONDITION = 15;
const int MAX_LEVEL = 5; // 最大关卡数
// 元素类型
enum ElementType {
NONE = 0,
FIRE = 1,
WATER = 2,
EARTH = 3,
WIND = 4
};
// 商店物品定义
enum ShopItem {
HEALTH_POTION = 1,
ATTACK_BOOST = 2,
TELEPORT = 3,
ELEMENT_CHANGE = 4
};
// 商店物品价格
const int SHOP_PRICES[] = {0, 5, 8, 10, 7}; // 0是占位符,对应无效物品
enum AnimationState {
NONE_ANIM = 0,
ATTACKING = 1,
DYING = 2,
ELEMENT_ATTACK = 3
};
enum MapElement {
EMPTY = 0,
WALL = 1,
EXIT = 2,
PLAYER = 3,
ENEMY = 4,
HEALTH_PACK = 5,
BOSS = 6,
EXPLOSION = 7,
COIN = 8,
GOLD_COIN = 9,
FIRE_ELEMENT = 10,
WATER_ELEMENT = 11,
EARTH_ELEMENT = 12,
WIND_ELEMENT = 13,
FIRE_TRAP = 14,
WATER_POOL = 15,
EARTH_WALL = 16,
WIND_CURRENT = 17
};
int gameMap[MAP_HEIGHT][MAP_WIDTH];
struct Player {
int x = 1;
int y = 1;
int health = INITIAL_HEALTH;
int maxHealth = INITIAL_HEALTH;
int coins = 0;
int attackBoost = 0; // 攻击加成
int teleportCharges = 0; // 传送次数
ElementType currentElement = FIRE; // 当前元素
AnimationState animState = NONE_ANIM;
chrono::steady_clock::time_point animStartTime;
int level = 1; // 当前关卡
int score = 0; // 总得分
} player;
struct Boss {
int x;
int y;
int health = BOSS_HEALTH;
ElementType weakness = FIRE; // Boss的弱点元素
} boss;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> disCoord(1, MAP_HEIGHT-2);
const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
// 元素相克关系
const map<ElementType, ElementType> ELEMENT_WEAKNESS = {
{FIRE, WATER},
{WATER, EARTH},
{EARTH, WIND},
{WIND, FIRE}
};
// 元素攻击范围
const map<ElementType, int> ELEMENT_RANGE = {
{FIRE, 2}, // 火焰攻击范围中等
{WATER, 1}, // 水元素攻击范围小但可以穿透
{EARTH, 3}, // 土元素攻击范围大但速度慢
{WIND, 2} // 风元素攻击范围中等
};
// 元素攻击效果
const map<ElementType, string> ELEMENT_EFFECTS = {
{FIRE, "燃烧"},
{WATER, "穿透"},
{EARTH, "击晕"},
{WIND, "击退"}
};
// 存档数据结构
struct GameSaveData {
int gameMap[MAP_HEIGHT][MAP_WIDTH];
Player player;
Boss boss;
chrono::steady_clock::time_point startTime;
int elapsedSeconds;
};
// 关卡难度设置
struct LevelSettings {
int enemyCount;
int healthPackCount;
int coinCount;
int goldCoinCount;
int elementCount;
int terrainCount;
int bossHealth;
int survivalTime;
int coinWinCondition;
};
// 关卡设置数组
const LevelSettings LEVEL_SETTINGS[MAX_LEVEL] = {
{3, 2, 5, 1, 1, 1, 5, 30, 15}, // 第1关
{5, 2, 6, 2, 2, 2, 6, 35, 20}, // 第2关
{7, 3, 7, 2, 2, 3, 7, 40, 25}, // 第3关
{9, 3, 8, 3, 3, 3, 8, 45, 30}, // 第4关
{12, 4, 10, 3, 3, 4, 10, 50, 35} // 第5关
};
// 保存游戏状态到文件
bool saveGame(const string& filename) {
ofstream outFile(filename, ios::binary);
if (!outFile) {
return false;
}
GameSaveData saveData;
memcpy(saveData.gameMap, gameMap, sizeof(gameMap));
saveData.player = player;
saveData.boss = boss;
auto currentTime = chrono::steady_clock::now();
saveData.elapsedSeconds = chrono::duration_cast<chrono::seconds>(currentTime - saveData.startTime).count();
outFile.write(reinterpret_cast<const char*>(&saveData), sizeof(saveData));
outFile.close();
return true;
}
// 从文件加载游戏状态
bool loadGame(const string& filename) {
ifstream inFile(filename, ios::binary);
if (!inFile) {
return false;
}
GameSaveData saveData;
inFile.read(reinterpret_cast<char*>(&saveData), sizeof(saveData));
inFile.close();
memcpy(gameMap, saveData.gameMap, sizeof(gameMap));
player = saveData.player;
boss = saveData.boss;
auto currentTime = chrono::steady_clock::now();
saveData.startTime = currentTime - chrono::seconds(saveData.elapsedSeconds);
return true;
}
void renderMap() {
system("cls"); // 只在需要更新时才清屏
for (int i = 0; i < MAP_HEIGHT; i++) {
for (int j = 0; j < MAP_WIDTH; j++) {
if (player.animState == ATTACKING &&
abs(j - player.x) <= 2 && abs(i - player.y) <= 2 &&
gameMap[i][j] == EMPTY) {
cout << "※";
}
else if (player.animState == ELEMENT_ATTACK) {
int range = ELEMENT_RANGE.at(player.currentElement);
if (abs(j - player.x) <= range && abs(i - player.y) <= range) {
switch (player.currentElement) {
case FIRE: cout << "火"; break;
case WATER: cout << "水"; break;
case EARTH: cout << "土"; break;
case WIND: cout << "风"; break;
default: cout << " "; break;
}
} else {
goto normalRender;
}
}
else if (player.animState == DYING &&
abs(j - player.x) <= 1 && abs(i - player.y) <= 1) {
cout << "?";
}
else {
normalRender:
switch (gameMap[i][j]) {
case EMPTY: cout << " "; break;
case WALL: cout << "■"; break;
case EXIT: cout << "!!"; break;
case PLAYER:
if (player.animState == DYING) cout << "?";
else cout << "* ";
break;
case ENEMY: cout << "E "; break;
case HEALTH_PACK: cout << "H "; break;
case BOSS: cout << "B "; break;
case EXPLOSION: cout << "※"; break;
case COIN: cout << "C "; break;
case GOLD_COIN: cout << "G "; break;
case FIRE_ELEMENT: cout << "火"; break;
case WATER_ELEMENT: cout << "水"; break;
case EARTH_ELEMENT: cout << "土"; break;
case WIND_ELEMENT: cout << "风"; break;
case FIRE_TRAP: cout << "烫"; break;
case WATER_POOL: cout << "~ "; break;
case EARTH_WALL: cout << "□"; break;
case WIND_CURRENT: cout << "→"; break;
}
}
}
cout << endl;
}
}
bool isValidPosition(int x, int y) {
return x >= 0 && x < MAP_WIDTH && y >= 0 && y < MAP_HEIGHT &&
gameMap[y][x] != WALL && gameMap[y][x] != PLAYER &&
gameMap[y][x] != EARTH_WALL;
}
bool isFarFromPlayerAndBoss(int x, int y) {
return (abs(x - player.x) > 3 || abs(y - player.y) > 3) &&
(abs(x - boss.x) > 3 || abs(y - boss.y) > 3);
}
bool isPathExist(int startX, int startY, int endX, int endY) {
if (startX == endX && startY == endY) return true;
vector<vector<bool>> visited(MAP_HEIGHT, vector<bool>(MAP_WIDTH, false));
queue<pair<int, int>> q;
q.push({startX, startY});
visited[startY][startX] = true;
while (!q.empty()) {
auto current = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = current.first + dirs[i][0];
int ny = current.second + dirs[i][1];
if (nx >= 0 && nx < MAP_WIDTH && ny >= 0 && ny < MAP_HEIGHT &&
!visited[ny][nx] && gameMap[ny][nx] != WALL && gameMap[ny][nx] != EARTH_WALL) {
if (nx == endX && ny == endY) {
return true;
}
visited[ny][nx] = true;
q.push({nx, ny});
}
}
}
return false;
}
void generateRandomMap() {
// 清空地图
for (int i = 0; i < MAP_HEIGHT; i++) {
for (int j = 0; j < MAP_WIDTH; j++) {
gameMap[i][j] = WALL;
}
}
// 使用深度优先搜索生成迷宫
stack<pair<int, int>> s;
gameMap[1][1] = EMPTY;
s.push({1, 1});
while (!s.empty()) {
auto current = s.top();
vector<pair<int, int>> neighbors;
for (int i = 0; i < 4; i++) {
int nx = current.first + dirs[i][0] * 2;
int ny = current.second + dirs[i][1] * 2;
if (nx > 0 && nx < MAP_WIDTH-1 && ny > 0 && ny < MAP_HEIGHT-1 && gameMap[ny][nx] == WALL) {
neighbors.emplace_back(nx, ny);
}
}
if (!neighbors.empty()) {
shuffle(neighbors.begin(), neighbors.end(), gen);
auto next = neighbors[0];
int wallX = current.first + (next.first - current.first) / 2;
int wallY = current.second + (next.second - current.second) / 2;
gameMap[wallY][wallX] = EMPTY;
gameMap[next.second][next.first] = EMPTY;
s.push(next);
} else {
s.pop();
}
}
// 随机放置出口
do {
uniform_int_distribution<> disExit(0, 3);
int exitSide = disExit(gen);
switch (exitSide) {
case 0:
boss.x = uniform_int_distribution<>(1, MAP_WIDTH-2)(gen);
boss.y = 1;
break;
case 1:
boss.x = MAP_WIDTH-2;
boss.y = uniform_int_distribution<>(1, MAP_HEIGHT-2)(gen);
break;
case 2:
boss.x = uniform_int_distribution<>(1, MAP_WIDTH-2)(gen);
boss.y = MAP_HEIGHT-2;
break;
case 3:
boss.x = 1;
boss.y = uniform_int_distribution<>(1, MAP_HEIGHT-2)(gen);
break;
}
gameMap[boss.y][boss.x] = EXIT;
} while (!isPathExist(player.x, player.y, boss.x, boss.y));
// 随机添加一些墙壁增加复杂度
for (int i = 0; i < MAP_HEIGHT; i++) {
for (int j = 0; j < MAP_WIDTH; j++) {
if (gameMap[i][j] == EMPTY && uniform_int_distribution<>(0, 4)(gen) == 0) {
gameMap[i][j] = WALL;
if (!isPathExist(player.x, player.y, boss.x, boss.y)) {
gameMap[i][j] = EMPTY;
}
}
}
}
// 根据当前关卡设置Boss属性
boss.health = LEVEL_SETTINGS[player.level-1].bossHealth;
uniform_int_distribution<> disElement(1, 4);
boss.weakness = static_cast<ElementType>(disElement(gen));
}
void movePlayer(char direction) {
if (player.animState != NONE_ANIM) return;
int newX = player.x;
int newY = player.y;
switch (direction) {
case 'w': newY--; break;
case 's': newY++; break;
case 'a': newX--; break;
case 'd': newX++; break;
}
if (isValidPosition(newX, newY)) {
// 检查风之气流效果
if (gameMap[newY][newX] == WIND_CURRENT) {
// 根据气流方向继续移动
if (newX > player.x) newX++; // 向右的气流
else if (newX < player.x) newX--; // 向左的气流
else if (newY > player.y) newY++; // 向下的气流
else if (newY < player.y) newY--; // 向上的气流
// 确保新位置有效
if (!isValidPosition(newX, newY)) {
newX = player.x + (newX - player.x) / 2;
newY = player.y + (newY - player.y) / 2;
}
}
// 检查火陷阱
if (gameMap[newY][newX] == FIRE_TRAP) {
player.health--;
}
gameMap[player.y][player.x] = EMPTY;
player.x = newX;
player.y = newY;
gameMap[player.y][player.x] = PLAYER;
// 移动后需要更新屏幕
renderMap();
}
}
void showAttackAnimation() {
player.animState = ATTACKING;
player.animStartTime = chrono::steady_clock::now();
for (int dy = -2; dy <= 2; dy++) {
for (int dx = -2; dx <= 2; dx++) {
int x = player.x + dx;
int y = player.y + dy;
if (x >= 0 && x < MAP_WIDTH && y >= 0 && y < MAP_HEIGHT) {
if (gameMap[y][x] == ENEMY || gameMap[y][x] == BOSS) {
gameMap[y][x] = EXPLOSION;
}
}
}
}
// 显示攻击动画
renderMap();
}
void showElementAttackAnimation() {
player.animState = ELEMENT_ATTACK;
player.animStartTime = chrono::steady_clock::now();
// 显示元素攻击动画
renderMap();
}
void attackEnemies() {
if (player.animState != NONE_ANIM) return;
showAttackAnimation();
}
void elementAttack() {
if (player.animState != NONE_ANIM) return;
showElementAttackAnimation();
int range = ELEMENT_RANGE.at(player.currentElement);
for (int dy = -range; dy <= range; dy++) {
for (int dx = -range; dx <= range; dx++) {
int x = player.x + dx;
int y = player.y + dy;
if (x >= 0 && x < MAP_WIDTH && y >= 0 && y < MAP_HEIGHT) {
// 根据元素类型处理攻击效果
switch (player.currentElement) {
case FIRE:
if (gameMap[y][x] == ENEMY || gameMap[y][x] == BOSS) {
gameMap[y][x] = EXPLOSION;
}
break;
case WATER:
// 水元素可以穿透敌人
if (gameMap[y][x] == ENEMY || gameMap[y][x] == BOSS) {
gameMap[y][x] = EXPLOSION;
// 继续检查后面的格子
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < MAP_WIDTH && ny >= 0 && ny < MAP_HEIGHT) {
if (gameMap[ny][nx] == ENEMY || gameMap[ny][nx] == BOSS) {
gameMap[ny][nx] = EXPLOSION;
}
}
}
break;
case EARTH:
// 土元素可以击晕敌人
if (gameMap[y][x] == ENEMY || gameMap[y][x] == BOSS) {
gameMap[y][x] = EXPLOSION;
}
break;
case WIND:
// 风元素可以击退敌人
if (gameMap[y][x] == ENEMY || gameMap[y][x] == BOSS) {
gameMap[y][x] = EXPLOSION;
// 尝试将敌人向后移动
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < MAP_WIDTH && ny >= 0 && ny < MAP_HEIGHT &&
gameMap[ny][nx] == EMPTY) {
gameMap[ny][nx] = gameMap[y][x];
}
}
break;
}
}
}
}
}
void attackBoss() {
if (abs(player.x - boss.x) <= 2 && abs(player.y - boss.y) <= 2) {
// 检查元素相克关系
if (player.currentElement == boss.weakness) {
boss.health -= 2; // 弱点攻击造成双倍伤害
} else {
boss.health--;
}
if (boss.health <= 0) {
gameMap[boss.y][boss.x] = EXPLOSION;
}
}
}
void collectItems() {
if (gameMap[player.y][player.x] == HEALTH_PACK) {
player.health = min(player.health + 1, player.maxHealth);
gameMap[player.y][player.x] = EMPTY;
renderMap(); // 收集物品后更新屏幕
}
else if (gameMap[player.y][player.x] == COIN) {
player.coins += 1;
gameMap[player.y][player.x] = EMPTY;
renderMap(); // 收集物品后更新屏幕
}
else if (gameMap[player.y][player.x] == GOLD_COIN) {
player.coins += 3;
gameMap[player.y][player.x] = EMPTY;
renderMap(); // 收集物品后更新屏幕
}
else if (gameMap[player.y][player.x] >= FIRE_ELEMENT && gameMap[player.y][player.x] <= WIND_ELEMENT) {
// 收集元素
player.currentElement = static_cast<ElementType>(gameMap[player.y][player.x] - FIRE_ELEMENT + 1);
gameMap[player.y][player.x] = EMPTY;
renderMap(); // 收集物品后更新屏幕
cout << "获得" << (player.currentElement == FIRE ? "火" :
player.currentElement == WATER ? "水" :
player.currentElement == EARTH ? "土" : "风") << "元素之力!" << endl;
Sleep(1000);
}
}
void moveEnemies() {
vector<pair<int, int>> enemies;
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
if (gameMap[y][x] == ENEMY) {
enemies.emplace_back(x, y);
}
}
}
bool enemyMoved = false;
for (auto& enemy : enemies) {
int x = enemy.first;
int y = enemy.second;
// 检查水之池效果
if (gameMap[y][x] == WATER_POOL) {
continue; // 敌人被困在水中无法移动
}
int dx = player.x - x;
int dy = player.y - y;
int newX = x + (dx > 0 ? 1 : (dx < 0 ? -1 : 0));
int newY = y + (dy > 0 ? 1 : (dy < 0 ? -1 : 0));
if (isValidPosition(newX, newY)) {
gameMap[y][x] = EMPTY;
gameMap[newY][newX] = ENEMY;
enemyMoved = true;
}
}
if (enemyMoved) {
renderMap(); // 敌人移动后更新屏幕
}
}
void moveBoss() {
if (boss.health <= 0) return;
// 检查水之池效果
if (gameMap[boss.y][boss.x] == WATER_POOL) {
return; // Boss被困在水中无法移动
}
int dx = player.x - boss.x;
int dy = player.y - boss.y;
int newX = boss.x + (dx > 0 ? 1 : (dx < 0 ? -1 : 0));
int newY = boss.y + (dy > 0 ? 1 : (dy < 0 ? -1 : 0));
if (isValidPosition(newX, newY)) {
gameMap[boss.y][boss.x] = EMPTY;
boss.x = newX;
boss.y = newY;
gameMap[boss.y][boss.x] = BOSS;
renderMap(); // Boss移动后更新屏幕
}
}
void spawnCoins() {
const LevelSettings& settings = LEVEL_SETTINGS[player.level-1];
int coinCount = uniform_int_distribution<>(settings.coinCount, settings.coinCount + 2)(gen);
for (int i = 0; i < coinCount; i++) {
int x = disCoord(gen);
int y = disCoord(gen);
if (gameMap[y][x] == EMPTY && isFarFromPlayerAndBoss(x, y)) {
gameMap[y][x] = COIN;
}
}
int goldCoinCount = uniform_int_distribution<>(settings.goldCoinCount, settings.goldCoinCount + 1)(gen);
for (int i = 0; i < goldCoinCount; i++) {
int x = disCoord(gen);
int y = disCoord(gen);
if (gameMap[y][x] == EMPTY && isFarFromPlayerAndBoss(x, y)) {
gameMap[y][x] = GOLD_COIN;
}
}
renderMap(); // 生成新金币后更新屏幕
}
void spawnElements() {
const LevelSettings& settings = LEVEL_SETTINGS[player.level-1];
// 随机生成元素物品
uniform_int_distribution<> disElement(0, 3);
for (int i = 0; i < settings.elementCount; i++) {
int x = disCoord(gen);
int y = disCoord(gen);
if (gameMap[y][x] == EMPTY && isFarFromPlayerAndBoss(x, y)) {
gameMap[y][x] = FIRE_ELEMENT + disElement(gen);
}
}
// 随机生成元素地形效果
uniform_int_distribution<> disTerrain(0, 7);
for (int i = 0; i < settings.terrainCount; i++) {
int x = disCoord(gen);
int y = disCoord(gen);
if (gameMap[y][x] == EMPTY && isFarFromPlayerAndBoss(x, y)) {
int terrainType = disTerrain(gen);
if (terrainType == 0) gameMap[y][x] = FIRE_TRAP;
else if (terrainType == 1) gameMap[y][x] = WATER_POOL;
else if (terrainType == 2) gameMap[y][x] = EARTH_WALL;
else if (terrainType == 3) {
gameMap[y][x] = WIND_CURRENT;
// 设置气流方向
int dir = uniform_int_distribution<>(0, 3)(gen);
switch (dir) {
case 0: gameMap[y][x] = WIND_CURRENT; break; // 右
case 1: gameMap[y][x] = WIND_CURRENT + 1; break; // 左
case 2: gameMap[y][x] = WIND_CURRENT + 2; break; // 下
case 3: gameMap[y][x] = WIND_CURRENT + 3; break; // 上
}
}
}
}
renderMap(); // 生成新元素后更新屏幕
}
void spawnEntities() {
const LevelSettings& settings = LEVEL_SETTINGS[player.level-1];
int enemyCount = uniform_int_distribution<>(settings.enemyCount, settings.enemyCount + 2)(gen);
for (int i = 0; i < enemyCount; i++) {
int x = disCoord(gen);
int y = disCoord(gen);
if (gameMap[y][x] == EMPTY) {
gameMap[y][x] = ENEMY;
}
}
int healthCount = uniform_int_distribution<>(settings.healthPackCount, settings.healthPackCount + 1)(gen);
for (int i = 0; i < healthCount; i++) {
int x = disCoord(gen);
int y = disCoord(gen);
if (gameMap[y][x] == EMPTY) {
gameMap[y][x] = HEALTH_PACK;
}
}
spawnCoins();
spawnElements();
renderMap(); // 生成新实体后更新屏幕
}
void updateAnimations() {
if (player.animState == NONE_ANIM) return;
auto now = chrono::steady_clock::now();
auto elapsed = chrono::duration_cast<chrono::milliseconds>(now - player.animStartTime).count();
if (elapsed >= ANIMATION_DURATION) {
if (player.animState == ATTACKING) {
for (int dy = -2; dy <= 2; dy++) {
for (int dx = -2; dx <= 2; dx++) {
int x = player.x + dx;
int y = player.y + dy;
if (x >= 0 && x < MAP_WIDTH && y >= 0 && y < MAP_HEIGHT) {
if (gameMap[y][x] == EXPLOSION) {
gameMap[y][x] = EMPTY;
}
if (gameMap[y][x] == ENEMY) {
gameMap[y][x] = EMPTY;
}
if (gameMap[y][x] == BOSS && abs(player.x - boss.x) <= 2 && abs(player.y - boss.y) <= 2) {
boss.health--;
if (boss.health <= 0) {
gameMap[boss.y][boss.x] = EMPTY;
}
}
}
}
}
}
else if (player.animState == ELEMENT_ATTACK) {
int range = ELEMENT_RANGE.at(player.currentElement);
for (int dy = -range; dy <= range; dy++) {
for (int dx = -range; dx <= range; dx++) {
int x = player.x + dx;
int y = player.y + dy;
if (x >= 0 && x < MAP_WIDTH && y >= 0 && y < MAP_HEIGHT) {
// 处理元素攻击效果
if (gameMap[y][x] == ENEMY || gameMap[y][x] == BOSS) {
// 检查元素相克关系
if (player.currentElement == boss.weakness && gameMap[y][x] == BOSS) {
boss.health -= 2; // 弱点攻击造成双倍伤害
} else {
gameMap[y][x] = EMPTY;
}
}
}
}
}
}
player.animState = NONE_ANIM;
renderMap(); // 动画结束后更新屏幕
}
}
void showDeathAnimation() {
player.animState = DYING;
player.animStartTime = chrono::steady_clock::now();
renderMap(); // 显示死亡动画
}
int checkGameState(chrono::steady_clock::time_point startTime) {
if (gameMap[player.y][player.x] == ENEMY) {
player.health--;
if (player.health <= 0) {
showDeathAnimation();
return -1;
}
gameMap[player.y][player.x] = PLAYER;
renderMap(); // 状态变化后更新屏幕
}
if (gameMap[player.y][player.x] == FIRE_TRAP) {
player.health--;
if (player.health <= 0) {
showDeathAnimation();
return -1;
}
renderMap(); // 状态变化后更新屏幕
}
if (gameMap[player.y][player.x] == EXIT) {
return 1;
}
const LevelSettings& settings = LEVEL_SETTINGS[player.level-1];
if (player.coins >= settings.coinWinCondition) {
return 3;
}
auto currentTime = chrono::steady_clock::now();
auto elapsed = chrono::duration_cast<chrono::seconds>(currentTime - startTime).count();
if (elapsed >= settings.survivalTime) {
return 2;
}
return 0;
}
// 显示商店菜单
void showShopMenu() {
system("cls");
cout << "====== 商店 ======" << endl;
cout << "金币: " << player.coins << endl;
cout << "当前元素: " <<
(player.currentElement == FIRE ? "火" :
player.currentElement == WATER ? "水" :
player.currentElement == EARTH ? "土" : "风") << endl;
cout << "1. 生命药水 (+1生命) - 5金币" << endl;
cout << "2. 攻击加成 (下次攻击范围+1) - 8金币" << endl;
cout << "3. 传送卷轴 (随机传送) - 10金币" << endl;
cout << "4. 元素转换 (随机改变当前元素) - 7金币" << endl;
cout << "5. 返回游戏" << endl;
cout << "请选择(1-5): ";
}
// 使用传送卷轴
void useTeleport() {
if (player.teleportCharges <= 0) return;
vector<pair<int, int>> emptySpots;
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
if (gameMap[y][x] == EMPTY) {
emptySpots.emplace_back(x, y);
}
}
}
if (!emptySpots.empty()) {
shuffle(emptySpots.begin(), emptySpots.end(), gen);
auto newPos = emptySpots[0];
gameMap[player.y][player.x] = EMPTY;
player.x = newPos.first;
player.y = newPos.second;
gameMap[player.y][player.x] = PLAYER;
player.teleportCharges--;
renderMap(); // 传送后更新屏幕
}
}
// 改变当前元素
void changeElement() {
uniform_int_distribution<> disElement(1, 4);
ElementType newElement;
do {
newElement = static_cast<ElementType>(disElement(gen));
} while (newElement == player.currentElement);
player.currentElement = newElement;
renderMap(); // 元素变化后更新屏幕
cout << "元素已转换为: " <<
(player.currentElement == FIRE ? "火" :
player.currentElement == WATER ? "水" :
player.currentElement == EARTH ? "土" : "风") << endl;
Sleep(1000);
}
// 处理商店购买
void handleShopPurchase(int item) {
switch (item) {
case HEALTH_POTION:
if (player.coins >= SHOP_PRICES[HEALTH_POTION]) {
player.health = min(player.health + 1, player.maxHealth);
player.coins -= SHOP_PRICES[HEALTH_POTION];
cout << "购买了生命药水!" << endl;
} else {
cout << "金币不足!" << endl;
}
break;
case ATTACK_BOOST:
if (player.coins >= SHOP_PRICES[ATTACK_BOOST]) {
player.attackBoost++;
player.coins -= SHOP_PRICES[ATTACK_BOOST];
cout << "购买了攻击加成!" << endl;
} else {
cout << "金币不足!" << endl;
}
break;
case TELEPORT:
if (player.coins >= SHOP_PRICES[TELEPORT]) {
player.teleportCharges++;
player.coins -= SHOP_PRICES[TELEPORT];
cout << "购买了传送卷轴!" << endl;
} else {
cout << "金币不足!" << endl;
}
break;
case ELEMENT_CHANGE:
if (player.coins >= SHOP_PRICES[ELEMENT_CHANGE]) {
changeElement();
player.coins -= SHOP_PRICES[ELEMENT_CHANGE];
} else {
cout << "金币不足!" << endl;
}
break;
default:
cout << "无效选择!" << endl;
break;
}
Sleep(1000);
}
void showStartScreen() {
system("cls");
cout << "====================================" << endl;
cout << "| 迷宫冒险游戏 |" << endl;
cout << "| Maze Adventure Game |" << endl;
cout << "====================================" << endl;
cout << endl;
cout << "游戏目标:" << endl;
cout << "1. 找到出口(!!)并逃脱" << endl;
cout << "2. 收集足够金币" << endl;
cout << "3. 或者生存足够时间" << endl;
cout << endl;
cout << "元素系统:" << endl;
cout << "火 - 燃烧敌人, 范围中等" << endl;
cout << "水 - 穿透攻击, 范围小" << endl;
cout << "土 - 击晕敌人, 范围大" << endl;
cout << "风 - 击退敌人, 范围中等" << endl;
cout << "相克关系: 火→风→土→水→火" << endl;
cout << endl;
cout << "控制方式:" << endl;
cout << "WASD - 移动" << endl;
cout << "K - 普通攻击(范围2格)" << endl;
cout << "J - 元素攻击" << endl;
cout << "T - 使用传送卷轴" << endl;
cout << "L - 保存游戏" << endl;
cout << "O - 加载游戏" << endl;
cout << "P - 打开商店" << endl;
cout << endl;
cout << "符号说明:" << endl;
cout << "* - 玩家" << endl;
cout << "E - 敌人" << endl;
cout << "B - Boss" << endl;
cout << "H - 生命恢复道具" << endl;
cout << "C - 银色金币(1分)" << endl;
cout << "G - 金色金币(3分)" << endl;
cout << "!! - 出口" << endl;
cout << "■ - 墙壁" << endl;
cout << "火 - 火元素/火陷阱" << endl;
cout << "水 - 水元素/水池" << endl;
cout << "土 - 土元素/土墙" << endl;
cout << "风 - 风元素/气流" << endl;
cout << endl;
cout << "1. 新游戏" << endl;
cout << "2. 加载游戏" << endl;
cout << "3. 退出" << endl;
cout << endl;
cout << "请选择(1-3): ";
}
void showGameMenu() {
system("cls");
cout << "====== 游戏菜单 ======" << endl;
cout << "1. 返回游戏" << endl;
cout << "2. 保存游戏" << endl;
cout << "3. 打开商店" << endl;
cout << "4. 退出游戏" << endl;
cout << "请选择(1-4): ";
}
void showLevelCompleteScreen(int result, int level, int score) {
system("cls");
cout << "====================================" << endl;
cout << "| 关卡 " << level << " 完成! |" << endl;
cout << "====================================" << endl;
cout << endl;
if (result == 1) {
cout << "你成功逃出出口!" << endl;
} else if (result == 2) {
cout << "你成功生存了足够时间!" << endl;
} else if (result == 3) {
cout << "你收集了足够的金币!" << endl;
}
cout << "当前得分: " << score << endl;
cout << endl;
if (level < MAX_LEVEL) {
cout << "1. 进入下一关" << endl;
} else {
cout << "恭喜你通关了所有关卡!" << endl;
}
cout << "2. 返回主菜单" << endl;
cout << endl;
cout << "请选择(1-2): ";
}
void resetPlayerForNewLevel() {
// 保留部分属性,重置其他属性
player.health = player.maxHealth;
player.x = 1;
player.y = 1;
player.animState = NONE_ANIM;
// 保留金币、攻击加成、传送次数等
}
void gameLoop() {
char choice;
do {
showStartScreen();
choice = _getch();
if (choice == '2') {
if (loadGame("save.dat")) {
cout << "游戏加载成功!" << endl;
Sleep(1000);
break;
} else {
cout << "加载游戏失败,开始新游戏" << endl;
Sleep(1000);
player.level = 1;
player.score = 0;
generateRandomMap();
gameMap[player.y][player.x] = PLAYER;
gameMap[boss.y][boss.x] = BOSS;
spawnEntities();
break;
}
} else if (choice == '3') {
return;
} else if (choice == '1') {
player.level = 1;
player.score = 0;
generateRandomMap();
gameMap[player.y][player.x] = PLAYER;
gameMap[boss.y][boss.x] = BOSS;
spawnEntities();
break;
}
} while (true);
while (player.level <= MAX_LEVEL) {
auto lastSpawnTime = chrono::steady_clock::now();
auto lastCoinSpawnTime = chrono::steady_clock::now();
auto startTime = chrono::steady_clock::now();
bool gameEnded = false;
int gameResult = 0;
while (true) {
system("cls");
renderMap();
const LevelSettings& settings = LEVEL_SETTINGS[player.level-1];
auto currentTime = chrono::steady_clock::now();
auto elapsedTime = chrono::duration_cast<chrono::seconds>(currentTime - startTime).count();
int remainingTime = max(0, settings.survivalTime - (int)elapsedTime);
cout << "关卡: " << player.level << "/" << MAX_LEVEL << endl;
cout << "得分: " << player.score << endl;
cout << "生命: " << player.health << "/" << player.maxHealth << endl;
cout << "Boss生命: " << boss.health << " (弱点: " <<
(boss.weakness == FIRE ? "火" :
boss.weakness == WATER ? "水" :
boss.weakness == EARTH ? "土" : "风") << ")" << endl;
cout << "金币: " << player.coins << "/" << settings.coinWinCondition << " (C:1, G:3)" << endl;
cout << "当前元素: " <<
(player.currentElement == FIRE ? "火" :
player.currentElement == WATER ? "水" :
player.currentElement == EARTH ? "土" : "风") << endl;
cout << "攻击加成: " << player.attackBoost << endl;
cout << "传送卷轴: " << player.teleportCharges << endl;
cout << "剩余时间: " << remainingTime << " 秒" << endl;
cout << "控制: WASD移动, K普通攻击, J元素攻击, T传送, L保存, O加载, P商店" << endl;
updateAnimations();
if (gameEnded) {
if (gameResult == -1) {
system("cls");
cout << "游戏结束! 你死了." << endl;
cout << "最终得分: " << player.score << endl;
cout << endl << "按任意键返回主菜单..." << endl;
_getch();
return;
} else {
// 计算得分
int levelScore = 0;
if (gameResult == 1) levelScore = 100 * player.level; // 通过出口逃脱
else if (gameResult == 2) levelScore = 80 * player.level; // 生存
else if (gameResult == 3) levelScore = 70 * player.level; // 收集金币
player.score += levelScore;
char levelChoice;
do {
showLevelCompleteScreen(gameResult, player.level, player.score);
levelChoice = _getch();
if (levelChoice == '1' && player.level < MAX_LEVEL) {
player.level++;
resetPlayerForNewLevel();
generateRandomMap();
gameMap[player.y][player.x] = PLAYER;
gameMap[boss.y][boss.x] = BOSS;
spawnEntities();
break;
} else if (levelChoice == '2') {
return; // 返回主菜单
}
} while (levelChoice != '1' && levelChoice != '2');
break; // 退出当前关卡循环
}
}
if (_kbhit()) {
char input = _getch();
if (input == 'k') {
attackEnemies();
attackBoss();
if (player.attackBoost > 0) {
player.attackBoost--;
}
}
else if (input == 'j') {
elementAttack();
if (player.attackBoost > 0) {
player.attackBoost--;
}
}
else if (input == 't') {
useTeleport();
}
else if (input == 'l') {
if (saveGame("save.dat")) {
cout << "游戏已保存!" << endl;
Sleep(1000);
} else {
cout << "保存游戏失败!" << endl;
Sleep(1000);
}
}
else if (input == 'o') {
if (loadGame("save.dat")) {
cout << "游戏已加载!" << endl;
Sleep(1000);
startTime = chrono::steady_clock::now() - chrono::seconds(0);
lastSpawnTime = chrono::steady_clock::now();
lastCoinSpawnTime = chrono::steady_clock::now();
} else {
cout << "加载游戏失败!" << endl;
Sleep(1000);
}
}
else if (input == 'p') {
// 打开商店
char shopChoice;
do {
showShopMenu();
shopChoice = _getch();
if (shopChoice >= '1' && shopChoice <= '4') {
handleShopPurchase(shopChoice - '0');
}
} while (shopChoice != '5');
}
else if (input == 'm') {
// 显示游戏菜单
char menuChoice;
do {
showGameMenu();
menuChoice = _getch();
if (menuChoice == '2') {
if (saveGame("save.dat")) {
cout << "游戏已保存!" << endl;
Sleep(1000);
} else {
cout << "保存游戏失败!" << endl;
Sleep(1000);
}
}
else if (menuChoice == '3') {
// 打开商店
char shopChoice;
do {
showShopMenu();
shopChoice = _getch();
if (shopChoice >= '1' && shopChoice <= '4') {
handleShopPurchase(shopChoice - '0');
}
} while (shopChoice != '5');
}
else if (menuChoice == '4') {
return;
}
} while (menuChoice != '1');
}
else if (input == 'w' || input == 'a' || input == 's' || input == 'd') {
movePlayer(input);
collectItems();
}
}
moveEnemies();
moveBoss();
//auto currentTime = chrono::steady_clock::now();
if (chrono::duration_cast<chrono::seconds>(currentTime - lastSpawnTime).count() >= ENEMY_RESPAWN_TIME) {
spawnEntities();
lastSpawnTime = currentTime;
}
if (chrono::duration_cast<chrono::seconds>(currentTime - lastCoinSpawnTime).count() >= COIN_RESPAWN_TIME) {
spawnCoins();
lastCoinSpawnTime = currentTime;
}
gameResult = checkGameState(startTime);
if (gameResult != 0) {
gameEnded = true;
}
Sleep(100);
}
}
// 通关所有关卡
system("cls");
cout << "====================================" << endl;
cout << "| 恭喜通关所有关卡! |" << endl;
cout << "====================================" << endl;
cout << endl;
cout << "最终得分: " << player.score << endl;
cout << endl << "按任意键返回主菜单..." << endl;
_getch();
}
int main() {
// 设置控制台输出为UTF-8编码,以支持中文和特殊符号
//SetConsoleOutputCP(65001);
while (true) {
gameLoop();
// 询问是否重新开始游戏
system("cls");
cout << "====================================" << endl;
cout << "| 游戏结束 |" << endl;
cout << "====================================" << endl;
cout << endl;
cout << "1. 重新开始游戏" << endl;
cout << "2. 退出游戏" << endl;
cout << endl;
cout << "请选择(1-2): ";
char choice = _getch();
if (choice == '2') {
break;
}
}
return 0;
}小游戏
文章信息与版本
文章信息
- 收录
- 2025年12月12日
- 更新
- 2026年5月13日
- 篇幅
- 约 80 分钟
版本
仅有当前版本