21xrx.com
2025-04-01 15:54:16 Tuesday
文章检索 我的文章 写文章
"C++编写的精彩小游戏程序"
2023-07-04 03:32:49 深夜i     10     0
C++ 小游戏程序 编写 精彩 游戏开发

C++作为一种静态类型、编译型的高级编程语言,被广泛应用于各个领域,包括游戏开发。许多著名的游戏如《魔兽世界》和《反恐精英》都是使用C++编写的。但是,即使你不是一名专业的游戏开发者,也可以使用C++编写出令人兴奋和有趣的小游戏程序,这就是本文要介绍的主题——C++编写的精彩小游戏程序。

首先,让我们来看看一个简单的C++命令行猜数字游戏程序。这个程序会让用户输入一个1-100的整数,然后根据用户的输入反馈不同的信息,直到用户猜中为止。以下是程序的代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
  srand(time(NULL)); //使用系统时间作为随机数种子
  int target = rand() % 100 + 1; //生成1-100之间的随机数
  int guess;
  
  do {
    cout << "Guess a number between 1 and 100: ";
    cin >> guess;
    
    if (guess > target)
      cout << "Too high! Try again." << endl;
     else if (guess < target)
      cout << "Too low! Try again." << endl;
     else
      cout << "Congratulations! You guessed the number." << endl;
    
  } while (guess != target);
  
  return 0;
}

当你运行这个程序时,它会提示你猜一个数字。如果你所猜的数字比目标数字大,程序会提示你“太高了,请再试一次。”;如果你所猜的数字比目标数字小,程序会提示你“太低了,请再试一次。”;如果你猜中了,程序会向你展示一条祝贺的消息。

接下来,让我们看看另一个C++小游戏程序——扫雷游戏。在这个程序中,你需要揭开一个NxN的方格,其中一些方格下面可能隐藏着地雷。你需要根据周围的数字提示来判断哪些方格下面有地雷,哪些方格是安全的。以下是程序的代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
const int MAXN = 10; //方格最大尺寸为10x10
const int MAXM = 10; //地雷最大数量为10int n, m; //方格尺寸和地雷数量
char field[MAXN][MAXN]; //方格数组
bool explored[MAXN][MAXN]; //标记是否被翻开
int dx[8] = 1; //8个方向
int dy[8] = 1;
void generate_field()
{
  //初始化方格数组
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      field[i][j] = '.';
  
  //随机生成地雷
  int mines = 0;
  while (mines < m) {
    int x = rand() % n;
    int y = rand() % n;
    if (field[x][y] != '*') {
      field[x][y] = '*';
      mines++;
    }
  }
}
void show_field(bool show_mines = false)
{
  //显示方格数组
  cout << " ";
  for (int i = 0; i < n; i++)
    cout << i;
  cout << endl;
  
  for (int i = 0; i < n; i++) {
    cout << i << " ";
    for (int j = 0; j < n; j++) {
      if (explored[i][j]) {
        cout << field[i][j];
      } else if (show_mines && field[i][j] == '*') {
        cout << field[i][j];
      } else
        cout << ".";
      
    }
    cout << endl;
  }
}
int count_mines(int x, int y)
{
  //计算相邻的地雷数量
  int mines = 0;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && ny >= 0 && nx < n && ny < n && field[nx][ny] == '*') {
      mines++;
    }
  }
  return mines;
}
bool explore_field(int x, int y)
{
  //翻开方格
  if (field[x][y] == '*')
    return false;
   else {
    explored[x][y] = true;
    int mines = count_mines(x, y);
    if (mines == 0) {
      for (int i = 0; i < 8; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && ny >= 0 && nx < n && ny < n && !explored[nx][ny]) {
          explore_field(nx, ny);
        }
      }
    }
    return true;
  }
}
int main()
{
  srand(time(NULL)); //使用系统时间作为随机数种子
  cout << "Enter field size (1-10): ";
  cin >> n;
  cout << "Enter number of mines (1-10): ";
  cin >> m;
  generate_field();
  show_field();
  
  int x, y;
  while (true) {
    cout << "Enter cell coordinates (row col): ";
    cin >> x >> y;
    if (x < 0 || y < 0 || x >= n || y >= n) please try again." << endl;
      continue;
    
    
    if (!explore_field(x, y)) {
      cout << "You hit a mine! Game over." << endl;
      show_field(true);
      break;
    } else {
      bool win = true;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (field[i][j] != '*' && !explored[i][j])
            win = false;
          
        }
      }
      if (win) {
        cout << "You win! Congratulations!" << endl;
        show_field(true);
        break;
      } else {
        show_field();
      }
    }
  }
  
  return 0;
}

当你运行这个程序时,它会提示你输入方格尺寸和地雷数量。然后它会随机生成一个NxN的方格,其中包括M个地雷。你需要通过输入方格的行列坐标来翻开它们,并根据相邻的地雷数量来判断哪些方格是安全的。如果你翻开了一枚地雷,游戏就会结束;如果你成功翻开了所有的安全方格,你就赢了。

以上,我们介绍了两个使用C++编写的小游戏程序。当然,还有很多其他类型的小游戏,如拼图游戏、飞行射击游戏、RPG游戏等等。通过学习C++编程,你可以探索更多有趣的游戏类型,并创造属于自己的精彩小游戏程序。

  
  

评论区

请求出错了