21xrx.com
2024-12-22 21:11:29 Sunday
登录
文章检索 我的文章 写文章
C++小游戏代码示例
2023-07-06 06:53:21 深夜i     --     --
C++ 小游戏 代码 示例

随着计算机科学的发展,游戏成为了一个广泛受欢迎的领域之一。编写游戏代码,成为了许多程序员的一项有趣和挑战性的任务。在这篇文章中,我们将介绍一些C++小游戏代码示例,以帮助您开始掌握这项技能。

1. 猜数字游戏

猜数字游戏是一个简单的小游戏,能够向您展示如何使用C++实现基本逻辑。代码如下:


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

  int randomNumber, userInput;

  

  srand(time(0)); //用当前时间初始化随机数生成器

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

  

  cout << "猜数字游戏-猜1-100中的一个数字:\n";

  

  while(true)

  {

    cout << "请输入您的数字猜测:\n";

    cin >> userInput;

    

    if(userInput == randomNumber)

    {

      cout << "恭喜您猜对了!\n";

      break;

    }

    else if(userInput < randomNumber)

    {

      cout << "您输入的数字过小,请再次尝试:\n";

    }

    else if(userInput > randomNumber)

    {

      cout << "您输入的数字过大,请再次尝试:\n";

    }

  }

  

  return 0;

}

2. 扫雷游戏

扫雷游戏是一个受欢迎的游戏,它在寻找和标记地雷的同时,还要确保玩家不会不小心踩上它们。下面是一个简单的代码示例,它用C++实现了扫雷游戏的基本逻辑:


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

const int ROWS = 10; //行数

const int COLS = 10; //列数

const int BOMBS = 10; //地雷数量

int gameBoard[ROWS][COLS];

void initializeGameBoard()

{

  int row, col, bombsPlaced = 0;

  

  for(row = 0; row < ROWS; row++)

  {

    for(col = 0; col < COLS; col++)

    {

      gameBoard[row][col] = 0; //初始化每个方块

    }

  }

  

  srand(time(0));

  

  while(bombsPlaced < BOMBS)

  {

    row = (rand() % ROWS);

    col = (rand() % COLS);

    

    if(gameBoard[row][col] != -1)

    {

      gameBoard[row][col] = -1; //将方块标记为地雷

      bombsPlaced++;

    }

  }

}

int countAdjacentBombs(int row, int col)

{

  int count = 0;

  //检查每个相邻的单元格,包括对角线

  if(row-1 >= 0 && gameBoard[row-1][col] == -1) count++;

  if(col-1 >= 0 && gameBoard[row][col-1] == -1) count++;

  if(row+1 < ROWS && gameBoard[row+1][col] == -1) count++;

  if(col+1 < COLS && gameBoard[row][col+1] == -1) count++;

  if(row-1 >= 0 && col-1 >= 0 && gameBoard[row-1][col-1] == -1) count++;

  if(row+1 < ROWS && col+1 < COLS && gameBoard[row+1][col+1] == -1) count++;

  if(row-1 >= 0 && col+1 < COLS && gameBoard[row-1][col+1] == -1) count++;

  if(row+1 < ROWS && col-1 >= 0 && gameBoard[row+1][col-1] == -1) count++;

  

  return count;

}

void printBoard(bool shouldShowBombs)

{

  int row, col;

  

  for(row = 0; row < ROWS; row++)

  {

    for(col = 0; col < COLS; col++)

    {

      if(gameBoard[row][col] == -1 && shouldShowBombs)

      {

        cout << "B ";

      }

      else if(gameBoard[row][col] == -2)

      {

        cout << "F ";

      }

      else if(gameBoard[row][col] == -3)

      {

        cout << "M ";

      }

      else if(gameBoard[row][col] >= 0)

      {

        cout << gameBoard[row][col] << " ";

      }

    }

    cout << endl;

  }

}

bool checkForWin()

{

  int row, col, countNonBomb = 0, countFlag = 0;

  

  for(row = 0; row < ROWS; row++)

  {

    for(col = 0; col < COLS; col++)

    {

      if(gameBoard[row][col] >= 0 && gameBoard[row][col] <= 8)

      {

        countNonBomb++;

      }

      else if(gameBoard[row][col] == -2)

      {

        countFlag++;

      }

    }

  }

  

  if(countFlag == BOMBS && countNonBomb == (ROWS * COLS - BOMBS))

  {

    return true;

  }

  else

  {

    return false;

  }

}

int main()

{

  int row, col, counter = 0;

  bool gameOver = false, winGame = false;

  

  cout << "扫雷游戏-看看您能否找到所有地雷!\n";

  

  initializeGameBoard(); //初始化游戏板

  

  while(!gameOver)

  {

    printBoard(false);

    cout << "请输入要翻转的行和列(中间用空格隔开):";

    cin >> row >> col;

    

    if(gameBoard[row][col] == -1) //如果玩家翻开了地雷,则游戏结束

    {

      cout << "BOOM! 你已经踩到了地雷。游戏结束。" << endl;

      gameBoard[row][col] = -3; //标记被翻开的地雷

      printBoard(true); //展示游戏板,包括所有地雷

      gameOver = true;

    }

    else if(gameBoard[row][col] >= 0 && gameBoard[row][col] <= 8) //如果玩家翻开了数字方块

    {

      gameBoard[row][col] = countAdjacentBombs(row, col);

      counter++;

      if(checkForWin()) //检查是否胜利

      {

        cout << "你赢了!你在翻开" << counter << "个方块后找到了所有的地雷!" << endl;

        printBoard(true);

        gameOver = true;

        winGame = true;

      }

    }

    else if(gameBoard[row][col] == -2) //如果玩家标记了旗帜

    {

      cout << "你已经标记了一个旗帜,现在请移除它(Y/N)?";

      char response;

      cin >> response;

      

      if(response == 'Y' || response == 'y')

      {

        gameBoard[row][col] = 0; //移除旗帜

      }

    }

    else //玩家翻开空方块

    {

      cout << "这是一个空的方块,请尝试别的地方。" << endl;

    }

  }

  

  return 0;

}

这个程序与真正的扫雷游戏相比,还有很多有待改进的地方。但是,它可以帮助您开始理解C++如何实现复杂的游戏逻辑。

3. 黑白棋游戏

黑白棋游戏,也称反棋,是两个人玩的棋类游戏。在这个游戏中,每个玩家尝试使他们的棋子比对手多。黑白棋游戏的C++代码示例如下:


#include <iostream>

#include <cstring>

using namespace std;

const int BOARD_SIZE = 8; //棋盘大小

enum class Color {BLACK, WHITE}; //黑色和白色棋子

class Reversi {

private:

  Color board[BOARD_SIZE][BOARD_SIZE]; //棋盘

  Color turn; //当前玩家的回合

  int blackCount; //黑色棋子的数量

  int whiteCount; //白色棋子的数量

  

  void initializeBoard() //初始化棋盘

  {

    int row, col;

    

    for(row = 0; row < BOARD_SIZE; row++)

    {

      for(col = 0; col < BOARD_SIZE; col++)

      {

        board[row][col] = Color::WHITE; //初始化所有方块为白色

      }

    }

    

    board[3][3] = Color::BLACK;

    board[4][4] = Color::BLACK;

    board[3][4] = Color::WHITE;

    board[4][3] = Color::WHITE;

    

    turn = Color::BLACK; //黑色棋子先行

    blackCount = 2;

    whiteCount = 2;

  }

  

  bool canPlacePiece(int row, int col, Color piece) //检查是否能够在指定的位置放置棋子

  {

    if(board[row][col] != Color::WHITE) //如果不是白色方块,则不能在此放置棋子

    {

      return false;

    }

    

    int r, c;

    bool foundPiece;

    

    for(r = -1; r <= 1; r++)

    {

      for(c = -1; c <= 1; c++)

      {

        if(r == 0 && c == 0) //如果是当前位置,则跳过

        {

          continue;

        }

        

        int i = 1;

        foundPiece = false;

        

        while(r*i + row >= 0 && r*i + row < BOARD_SIZE && c*i + col >= 0 && c*i + col < BOARD_SIZE)

        {

          if(board[row + r*i][col + c*i] == piece) //如果找到了同色的棋子,则说明可以将对手的棋子反转

          {

            foundPiece = true;

            break;

          }

          else if(board[row + r*i][col + c*i] == Color::WHITE) //如果找到了空方块,则跳出循环

          {

            break;

          }

          

          i++;

        }

        

        if(foundPiece) //如果可以进行反转,则该位置可行

        {

          return true;

        }

      }

    }

    

    return false; //否则,该位置不可行

  }

  

  void printBoard() //展示棋盘

  {

    cout << " ";

    for(int col = 0; col < BOARD_SIZE; col++)

    {

      cout << col << " ";

    }

    cout << endl;

    

    for(int row = 0; row < BOARD_SIZE; row++)

    {

      cout << row << " ";

      for(int col = 0; col < BOARD_SIZE; col++)

      {

        if(board[row][col] == Color::BLACK)

        {

          cout << "@ ";

        }

        else if(board[row][col] == Color::WHITE)

        {

          cout << "O ";

        }

      }

      cout << endl;

    }

  }

  

  void flipPieces(int row, int col, Color piece) //将对手的棋子反转为当前玩家的颜色

  {

    int r, c;

    bool foundPiece;

    

    for(r = -1; r <= 1; r++)

    {

      for(c = -1; c <= 1; c++)

      {

        if(r == 0 && c == 0)

        {

          continue;

        }

        

        int i = 1;

        foundPiece = false;

        

        while(r*i + row >= 0 && r*i + row < BOARD_SIZE && c*i + col >= 0 && c*i + col < BOARD_SIZE)

        {

          if(board[row + r*i][col + c*i] == piece)

          {

            foundPiece = true;

            break;

          }

          else if(board[row + r*i][col + c*i] == Color::WHITE)

          {

            break;

          }

          

          i++;

        }

        

        if(foundPiece)

        {

          for(int j = 1; j < i; j++)

          {

            board[row + r*j][col + c*j] = piece;

          }

        }

      }

    }

  }

  

public:

  Reversi()

  {

    initializeBoard();

  }

  

  void playGame() //开始游戏

  {

    int row, col;

    bool inputValid, canPlace;

    char response;

    

    while(true)

    {

      printBoard();

      

      if(turn == Color::BLACK)

      {

        cout << "黑色棋子的回合。" << endl;

      }

      else

      {

        cout << "白色棋子的回合。" << endl;

      }

      

      inputValid = false;

      canPlace = false;

      

      while(!inputValid || !canPlace)

      {

        cout << "请输入要放置棋子的行和列(例如“3 4”): ";

        cin >> row >> col;

        

        if(row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE)

        {

          inputValid = true;

        }

        else

        {

          cout << "无效的输入,请重试。" << endl;

        }

        

        if(inputValid) //检查是否能够放棋子

        {

          if(turn == Color::BLACK)

          {

            canPlace = canPlacePiece(row, col, Color::BLACK);

          }

          else

          {

            canPlace = canPlacePiece(row, col, Color::WHITE);

          }

          

          if(!canPlace)

          {

            cout << "您不能在此处放置棋子,请重试。" << endl;

          }

        }

      }

      

      if(turn == Color::BLACK)

      {

        board[row][col] = Color::BLACK;

        blackCount++;

        flipPieces(row, col, Color::BLACK);

      }

      else

      {

        board[row][col] = Color::WHITE;

        whiteCount++;

        flipPieces(row, col, Color::WHITE);

      }

      

      if(blackCount + whiteCount == BOARD_SIZE*BOARD_SIZE) //如果棋盘已满,则游戏结束

      {

        break;

      }

      

      if(turn == Color::BLACK)

      {

        turn = Color::WHITE;

      }

      else

      {

        turn = Color::BLACK;

      }

    }

    

    printBoard();

    

    if(blackCount > whiteCount)

    {

      cout << "黑色棋子胜利!" << endl;

    }

    else if(whiteCount > blackCount)

    {

      cout << "白色棋子胜利!" << endl;

    }

    else

    {

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

    }

    

    cout << "游戏结束。是否再玩一次(Y/N)?";

    cin >> response;

    

    if(response == 'Y' || response == 'y')

    {

      initializeBoard();

      playGame();

    }

    else

    {

      cout << "感谢您的游戏。" << endl;

    }

  }

};

int main()

{

  Reversi game;

  

  cout << "欢迎来玩黑白棋游戏。" << endl;

  

  game.playGame();

  

  return 0;

}

总结

在本文中,我们介绍了三个不同的C++小游戏代码示例,包括猜数字游戏、扫雷游戏和黑白棋游戏。这些示例向您展示了如何使用C++语言编写游戏代码,并让您开始了解游戏编程的基本原理。如果您想进一步深入学习游戏编程,可以寻找更多有关C++游戏编程的学习资源和教程。

  
  

评论区

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