21xrx.com
2024-11-25 03:08:36 Monday
登录
文章检索 我的文章 写文章
C++小游戏编程代码大全
2023-07-06 17:27:09 深夜i     --     --
C++ 小游戏 编程 代码大全 游戏开发

如果你对C++编程有一些基础,那么你可以尝试使用C++来制作一些小游戏。这不仅可以提高你的编程技能,还可以让你享受创造游戏的乐趣。下面是一些C++小游戏的编程代码,可以帮助你快速入门。

1. 猜数游戏

这是一个简单的猜数字游戏,玩家需要在规定的次数内猜出电脑生成的随机数。代码如下:


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

  srand(time(0)); // 生成随机数种子

  int randomNum = rand() % 100 + 1; // 生成1-100的随机数

  int guess = 0;

  int times = 0;

  while (guess != randomNum && times < 10) { // 猜错或超过10次就结束游戏

    cout << "请输入你的猜测(1~100):" << endl;

    cin >> guess;

    times++;

    if (guess < randomNum)

      cout << "猜小了 else if (guess > randomNum)

      cout << "猜大了 else

      cout << "太棒了

  }

  if (times >= 10) 你没有在规定次数内猜对。" << endl;

  

  return 0;

}

2. 井字棋游戏

这是一个两人玩的井字棋游戏,玩家可以使用键盘输入行列坐标来下棋。代码如下:


#include <iostream>

using namespace std;

char board[3][3] = { // 棋盘

  ' ',

  ' ',

  ' '

};

void printBoard() { // 打印棋局

  for (int i = 0; i < 3; i++) {

    for (int j = 0; j < 3; j++) {

      cout << board[i][j];

      if (j != 2)

        cout << '|';

      |';

      }

    }

    cout << endl;

    if (i != 2) {

      cout << "-+-+-" << endl;

    }

  }

}

bool checkWin(char c) { // 检查是否胜利

  bool flag = false;

  for (int i = 0; i < 3; i++) {

    if (board[i][0] == c && board[i][1] == c && board[i][2] == c)

      flag = true;

      break;

    

  }

  for (int j = 0; j < 3; j++) {

    if (board[0][j] == c && board[1][j] == c && board[2][j] == c)

      flag = true;

      break;

    

  }

  if (board[0][0] == c && board[1][1] == c && board[2][2] == c)

    flag = true;

  

  if (board[0][2] == c && board[1][1] == c && board[2][0] == c)

    flag = true;

  

  return flag;

}

int main() {

  int row, col;

  char player;

  bool finished = false;

  while (!finished) {

    cout << "请输入你要下棋的行列坐标和玩家编号(1或2,先手为1):" << endl;

    cin >> row >> col >> player;

    if (row < 1 || row > 3 || col < 1 || col > 3 || (player != '1' && player != '2') || board[row-1][col-1] != ' ') 请重新输入。" << endl;

      continue;

    

    board[row-1][col-1] = player == '1' ? 'X' : 'O'; // 下棋

    printBoard(); // 打印棋局

    if (checkWin('X'))

      cout << "玩家1胜利!" << endl;

      finished = true;

     else if (checkWin('O'))

      cout << "玩家2胜利!" << endl;

      finished = true;

     else {

      bool full = true;

      for (int i = 0; i < 3; i++) {

        for (int j = 0; j < 3; j++) {

          if (board[i][j] == ' ')

            full = false;

            break;

          

        }

        if (!full)

          break;

        

      }

      if (full) // 平局

        cout << "平局!" << endl;

        finished = true;

      

    }

  }

  return 0;

}

3. 扫雷游戏

这是一个简单的扫雷游戏,玩家需要在规定的时间内找出所有的地雷。代码如下:


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

const int ROWS = 10;

const int COLS = 10;

const int MINES = 15;

int field[ROWS][COLS] = { // 扫雷区域

  0,

  0,

  0,

  0,

  0,

  0,

  0,

  0,

  0,

  0

};

void printField() { // 打印扫雷区域

  for (int i = 0; i < ROWS; i++) {

    for (int j = 0; j < COLS; j++) {

      if (field[i][j] == -1) { // 地雷

        cout << "* ";

      } else if (field[i][j] == 0) // 未翻开

        cout << ". ";

       else { // 数字

        cout << field[i][j] << " ";

      }

    }

    cout << endl;

  }

}

int main() {

  srand(time(0)); // 生成随机数种子

  int minesCount = 0;

  while (minesCount < MINES) { // 随机生成地雷

    int row = rand() % ROWS;

    int col = rand() % COLS;

    if (field[row][col] != -1) { // 没有地雷才放置

      field[row][col] = -1;

      minesCount++;

    }

  }

  bool gameOver = false;

  int remainMines = MINES;

  time_t start = time(NULL);

  while (!gameOver) {

    printField(); // 打印扫雷区域

    cout << "当前剩余地雷数:" << remainMines << ",输入行列坐标和操作(1:翻开,2:标记/取消标记):" << endl;

    int row, col, op;

    cin >> row >> col >> op;

    if (row < 1 || row > ROWS || col < 1 || col > COLS || (op != 1 && op != 2)) // 判断输入是否合法

      cout << "输入有误

    if (op == 1) { // 翻开

      if (field[row-1][col-1] == -1) // 踩到地雷游戏结束

        cout << "很遗憾 else {

        int minesAround = 0;

        if (row > 1) {

          if (col > 1 && field[row-2][col-2] == -1) {

            minesAround++;

          }

          if (field[row-2][col-1] == -1) {

            minesAround++;

          }

          if (col < COLS && field[row-2][col] == -1) {

            minesAround++;

          }

        }

        if (col > 1 && field[row-1][col-2] == -1) {

          minesAround++;

        }

        if (col < COLS && field[row-1][col] == -1) {

          minesAround++;

        }

        if (row < ROWS) {

          if (col > 1 && field[row][col-2] == -1) {

            minesAround++;

          }

          if (field[row][col-1] == -1) {

            minesAround++;

          }

          if (col < COLS && field[row][col] == -1) {

            minesAround++;

          }

        }

        field[row-1][col-1] = minesAround; // 标记数字

        if (field[row-1][col-1] == 0) { // 如果周围没有地雷,递归翻开周围区域

          if (row > 1 && field[row-2][col-1] == 0) {

            field[row-2][col-1] = 1;

          }

          if (col > 1 && field[row-1][col-2] == 0) {

            field[row-1][col-2] = 1;

          }

          if (col < COLS && field[row-1][col] == 0) {

            field[row-1][col] = 1;

          }

          if (row < ROWS && field[row][col-1] == 0) {

            field[row][col-1] = 1;

          }

        }

      }

    } else { // 标记/取消标记

      if (field[row-1][col-1] == -1) {

        field[row-1][col-1] = 2;

        remainMines--;

      } else if (field[row-1][col-1] == 2) {

        field[row-1][col-1] = -1;

        remainMines++;

      }

    }

    if (remainMines == 0) { // 所有地雷已标记

      cout << "恭喜你,你找到了所有地雷!" << endl;

      gameOver = true;

    }

    if (time(NULL) - start >= 60) { // 时间到游戏结束

      cout << "时间到,游戏结束!" << endl;

      gameOver = true;

    }

  }

  printField(); // 打印扫雷区域(最终结果)

  return 0;

}

总之,使用C++编程制作小游戏可以帮助你提高编程技能和发挥创造力,希望上述代码对你有所帮助。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复