地狱闯关游戏2.0
2025-06-10 19:29:01
发布于:浙江
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;
const int WIDTH = 10;
const int HEIGHT = 10;
struct Player {
int x, y;
int health;
int attack;
};
struct Monster {
int x, y;
int health;
int attack;
};
void displayMap(const vector<vector<char>>& map, const Player& player) {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == player.y && j == player.x) {
cout << 'P';
} else {
cout << map[i][j];
}
}
cout << endl;
}
cout << "生命值: " << player.health << " | 攻击: " << player.attack << endl;
}
bool handleCombat(Player& player, Monster& monster) {
while (monster.health > 0 && player.health > 0) {
cout << "战斗! 怪物生命: " << monster.health << endl;
// 玩家攻击
monster.health -= player.attack;
if (monster.health <= 0) {
cout << "你击败了怪物!" << endl;
return true;
}
// 怪物攻击
player.health -= monster.attack;
if (player.health <= 0) {
cout << "你被怪物击败了!" << endl;
return false;
}
}
return player.health > 0;
}
int main() {
srand(time(0));
Player player = {0, 0, 100, 15};
Monster monster = {rand() % WIDTH, rand() % HEIGHT, 30, 8};
vector<vector<char>> map(HEIGHT, vector<char>(WIDTH, '.'));
while (true) {
displayMap(map, player);
if (player.health <= 0) {
cout << "游戏结束! 你被怪物击败了。" << endl;
break;
}
char input;
cout << "移动(wasd)或战斗(f): ";
cin >> input;
if (input == 'f' &&
abs(player.x - monster.x) + abs(player.y - monster.y) == 1) {
// 遇到怪物战斗
if (!handleCombat(player, monster)) {
break;
}
// 怪物被击败后重新生成
monster.x = rand() % WIDTH;
monster.y = rand() % HEIGHT;
monster.health = 30;
} else {
// 移动
int newX = player.x, newY = player.y;
switch (input) {
case 'w': newY--; break;
case 's': newY++; break;
case 'a': newX--; break;
case 'd': newX++; break;
default: continue;
}
if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT) {
player.x = newX;
player.y = newY;
// 检查是否遇到怪物
if (abs(player.x - monster.x) + abs(player.y - monster.y) == 1) {
cout << "你遇到了怪物!" << endl;
if (!handleCombat(player, monster)) {
break;
}
monster.x = rand() % WIDTH;
monster.y = rand() % HEIGHT;
monster.health = 30;
}
}
}
}
return 0;
}
全部评论 1
我把它复制进devc++怎么老会报错
2025-06-01 来自 云南
0我也是
2025-06-01 来自 上海
0哪里报错?
2025-06-02 来自 浙江
1就像这样
2025-06-02 来自 云南
1
有帮助,赞一个