帮我用c++写一个探索类RPG游戏基础代码地图生成
时间: 2024-05-07 14:22:05 浏览: 24
以下是一个简单的探索类RPG游戏基础代码,包括地图生成和基本操作:
```c++
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
// 地图大小
const int MAP_SIZE = 10;
// 地图元素类型
enum class MapElement {
WALL,
FLOOR,
PLAYER,
TREASURE
};
// 玩家结构体
struct Player {
int x, y;
int hp;
int gold;
};
// 游戏地图类
class Map {
public:
Map() {
// 初始化地图
for (int i = 0; i < MAP_SIZE; i++) {
vector<MapElement> row(MAP_SIZE, MapElement::WALL);
map_.push_back(row);
}
// 生成地图
generateMap();
}
// 输出地图
void print() {
for (int i = 0; i < MAP_SIZE; i++) {
for (int j = 0; j < MAP_SIZE; j++) {
switch (map_[i][j]) {
case MapElement::WALL:
cout << "# ";
break;
case MapElement::FLOOR:
cout << ". ";
break;
case MapElement::PLAYER:
cout << "@ ";
break;
case MapElement::TREASURE:
cout << "$ ";
break;
}
}
cout << endl;
}
}
// 获取指定位置元素类型
MapElement getElement(int x, int y) {
return map_[x][y];
}
// 设置指定位置元素类型
void setElement(int x, int y, MapElement element) {
map_[x][y] = element;
}
private:
// 生成地图
void generateMap() {
// 随机放置玩家和宝藏
srand(time(NULL));
int player_x = rand() % MAP_SIZE;
int player_y = rand() % MAP_SIZE;
setElement(player_x, player_y, MapElement::PLAYER);
int treasure_x = rand() % MAP_SIZE;
int treasure_y = rand() % MAP_SIZE;
setElement(treasure_x, treasure_y, MapElement::TREASURE);
// 随机生成地图
for (int i = 0; i < MAP_SIZE; i++) {
for (int j = 0; j < MAP_SIZE; j++) {
if (getElement(i, j) == MapElement::WALL) {
if (rand() % 100 < 50) {
setElement(i, j, MapElement::FLOOR);
}
}
}
}
}
vector<vector<MapElement>> map_;
};
// 游戏类
class Game {
public:
Game() {
// 初始化玩家
player_.hp = 10;
player_.gold = 0;
// 初始化地图
map_ = new Map();
}
~Game() {
delete map_;
}
// 运行游戏
void run() {
while (player_.hp > 0 && player_.gold < 10) {
// 输出地图
map_->print();
// 获取玩家输入
char input;
cout << "Enter a command (w/a/s/d): ";
cin >> input;
// 处理玩家操作
switch (input) {
case 'w':
movePlayer(0, -1);
break;
case 'a':
movePlayer(-1, 0);
break;
case 's':
movePlayer(0, 1);
break;
case 'd':
movePlayer(1, 0);
break;
default:
cout << "Invalid command!" << endl;
break;
}
}
// 游戏结束
if (player_.hp <= 0) {
cout << "You died!" << endl;
} else {
cout << "You won!" << endl;
}
}
private:
// 移动玩家
void movePlayer(int dx, int dy) {
int new_x = player_.x + dx;
int new_y = player_.y + dy;
if (new_x >= 0 && new_x < MAP_SIZE && new_y >= 0 && new_y < MAP_SIZE) {
MapElement element = map_->getElement(new_x, new_y);
switch (element) {
case MapElement::WALL:
cout << "You bump into a wall!" << endl;
break;
case MapElement::FLOOR:
player_.x = new_x;
player_.y = new_y;
break;
case MapElement::TREASURE:
player_.x = new_x;
player_.y = new_y;
player_.gold++;
cout << "You found a treasure! Gold: " << player_.gold << endl;
break;
}
} else {
cout << "You bump into a wall!" << endl;
}
}
Player player_;
Map* map_;
};
int main() {
Game game;
game.run();
return 0;
}
```
代码中使用了一个 `Map` 类来生成和管理游戏地图,另外还有一个 `Player` 结构体来表示玩家状态。游戏主循环中,首先输出地图,然后等待玩家输入操作,最后根据玩家的操作移动玩家并更新玩家状态。游戏结束条件是玩家血量为零或获得十个金币。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)