21xrx.com
2024-12-27 04:46:09 Friday
登录
文章检索 我的文章 写文章
C++小游戏程序设计源代码
2023-06-30 00:12:26 深夜i     --     --
C++编程 游戏设计 源代码 小游戏程序 计算机科学

C++是一种流行的编程语言,广泛用于游戏开发。在本文中,我们提供了一些基于C++编写的小游戏程序源代码,包括扫雷游戏和贪吃蛇游戏。

扫雷游戏

扫雷是一种经典的单人游戏,目标是在一个矩形网格中避免触发地雷。以下是一个基于C++的扫雷游戏程序源代码。


#include <iostream>

#include <cstdlib>   

#include <ctime>    

using namespace std;

int main()

{

  int Mines[6][6];     

  char Board[6][6];     

  int x, y;         

  bool GameOver = false;

  srand(time(NULL));    

  for (int i = 0; i<6; i++)  

    for (int j = 0; j<6; j++)

    {

      Mines[i][j] = rand() % 2;

      Board[i][j] = 'X';    

    }

  while (GameOver == false)   

  {

    for (int i = 0; i<6; i++)

    {

      for (int j = 0; j<6; j++)

      {

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

      }

      cout << endl;

    }

    cout << "Enter row and column separated by a space: ";

    cin >> x >> y;

    if (Mines[x][y] == 1)     

    

      cout << "Game Over!" << endl;

      GameOver = true;      

    

    else              

    {

      Board[x][y] = 'O';     

    }

  }

  return 0;

}

贪吃蛇游戏

贪吃蛇是一种经典的多人游戏,目标是吃到食物并避免碰壁或碰到蛇身。以下是一个基于C++的贪吃蛇游戏程序源代码。


#include <iostream>

#include <conio.h>

#include <windows.h>

using namespace std;

bool gameOver;

const int width = 20;

const int height = 20;

int x, y, fruitX, fruitY, score;

int tailX[100], tailY[100];

int nTail;

enum eDirection UP;

eDirection dir;

void Setup()

{

  gameOver = false;

  dir = STOP;

  x = width / 2;

  y = height / 2;

  fruitX = rand() % width;

  fruitY = rand() % height;

  score = 0;

}

void Draw()

{

  system("cls");

  for (int i = 0; i < width + 2; i++)

    cout << "#";

  cout << endl;

  for (int i = 0; i < height; i++)

  {

    for (int j = 0; j < width; j++)

    {

      if (j == 0)

        cout << "#";

      if (i == y && j == x)

        cout << "O";

      else if (i == fruitY && j == fruitX)

        cout << "F";

      else

      {

        bool print = false;

        for (int k = 0; k < nTail; k++)

        {

          if (tailX[k] == j && tailY[k] == i)

          

            cout << "o";

            print = true;

          

        }

        if (!print)

          cout << " ";

      }

        

      if (j == width - 1)

        cout << "#";

    }

    cout << endl;

  }

  for (int i = 0; i < width + 2; i++)

    cout << "#";

  cout << endl;

  cout << "Score:" << score << endl;

}

void Input()

{

  if (_kbhit())

  {

    switch (_getch())

    

    case 'a':

      dir = LEFT;

      break;

    case 'd':

      dir = RIGHT;

      break;

    case 'w':

      dir = UP;

      break;

    case 's':

      dir = DOWN;

      break;

    case 'x':

      gameOver = true;

      break;

    

  }

}

void Logic()

{

  int prevX = tailX[0];

  int prevY = tailY[0];

  int prev2X, prev2Y;

  tailX[0] = x;

  tailY[0] = y;

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

  {

    prev2X = tailX[i];

    prev2Y = tailY[i];

    tailX[i] = prevX;

    tailY[i] = prevY;

    prevX = prev2X;

    prevY = prev2Y;

  }

  switch (dir)

  {

  case LEFT:

    x--;

    break;

  case RIGHT:

    x++;

    break;

  case UP:

    y--;

    break;

  case DOWN:

    y++;

    break;

  default:

    break;

  }

  if (x > width || x < 0 || y > height || y < 0)

    gameOver = true;

  for (int i = 0; i < nTail; i++)

    if (tailX[i] == x && tailY[i] == y)

      gameOver = true;

  if (x == fruitX && y == fruitY)

  {

    score += 10;

    fruitX = rand() % width;

    fruitY = rand() % height;

    nTail++;

  }

}

int main()

{

  Setup();

  while (!gameOver)

  {

    Draw();

    Input();

    Logic();

    Sleep(50);

  }

  return 0;

}

结论

以上是基于C++的扫雷游戏和贪吃蛇游戏程序源代码。欢迎读者下载并尝试运行这些程序。对于初学者,通过学习和修改这些代码,您可以提高您的编程技能并加深对C++编程语言的理解。

  
  

评论区

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