用c++制作简单小游戏代码
时间: 2024-05-26 13:08:06 浏览: 15
当然可以!用 C++ 制作小游戏代码是一项有趣的任务。这里我介绍一个简单的命令行扫雷游戏的代码实现思路:
1. 定义一个类 `MineSweeper`,包含以下属性和方法:
属性:
- `board`:一个二维数组,表示扫雷棋盘上每个格子的状态。
- `rows` 和 `cols`:分别表示扫雷棋盘的行数和列数。
- `num_mines`:表示扫雷棋盘上地雷的数量。
方法:
- `init_board`:初始化扫雷棋盘,将所有格子都设置为未开启状态(默认为 0),并随机放置地雷。
- `print_board`:打印扫雷棋盘的当前状态。
- `open_cell`:开启一个格子,如果该格子上是地雷,则游戏结束;否则,如果该格子周围没有地雷,则递归开启周围的格子。
- `play_game`:开始游戏,不断接收用户输入,并调用相应的方法进行处理,直到游戏结束。
2. 在 `main` 函数中创建一个 `MineSweeper` 对象,并调用其 `play_game` 方法开始游戏。
下面是一个简单的示例代码(仅供参考):
```
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
class MineSweeper {
public:
MineSweeper(int r, int c, int m) : rows(r), cols(c), num_mines(m), board(rows, vector<int>(cols, 0)) {
init_board();
}
void init_board() {
// 随机放置地雷
srand(time(NULL));
int count = 0;
while (count < num_mines) {
int r = rand() % rows;
int c = rand() % cols;
if (board[r][c] != -1) {
board[r][c] = -1;
count++;
}
}
}
void print_board() {
cout << " ";
for (int i = 0; i < cols; i++) {
cout << " " << i;
}
cout << endl;
for (int i = 0; i < rows; i++) {
cout << i << " ";
for (int j = 0; j < cols; j++) {
if (board[i][j] == -1) {
cout << "* ";
} else if (board[i][j] == 0) {
cout << "- ";
} else {
cout << board[i][j] << " ";
}
}
cout << endl;
}
}
void open_cell(int r, int c) {
if (r < 0 || r >= rows || c < 0 || c >= cols || board[r][c] != 0) {
return;
}
int count = 0;
for (int i = max(0, r-1); i <= min(rows-1, r+1); i++) {
for (int j = max(0, c-1); j <= min(cols-1, c+1); j++) {
if (board[i][j] == -1) {
count++;
}
}
}
if (count > 0) {
board[r][c] = count;
} else {
board[r][c] = -2;
for (int i = max(0, r-1); i <= min(rows-1, r+1); i++) {
for (int j = max(0, c-1); j <= min(cols-1, c+1); j++) {
open_cell(i, j);
}
}
}
}
bool play_game() {
print_board();
while (true) {
int r, c;
cout << "请输入要开启的格子的行号和列号,用空格分隔(例如:0 1):";
cin >> r >> c;
if (board[r][c] == -1) {
cout << "你踩到地雷了,游戏结束!" << endl;
return false;
} else {
open_cell(r, c);
print_board();
bool win = true;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == 0) {
win = false;
break;
}
}
}
if (win) {
cout << "恭喜你扫完了所有地雷,游戏胜利!" << endl;
return true;
}
}
}
}
private:
int rows, cols, num_mines;
vector<vector<int>> board;
};
int main() {
int rows = 10, cols = 10, num_mines = 10;
MineSweeper game(rows, cols, num_mines);
game.play_game();
return 0;
}
```
相关推荐
![](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)