21xrx.com
2024-11-10 00:38:45 Sunday
登录
文章检索 我的文章 写文章
C++贪吃蛇源代码
2023-07-03 21:00:26 深夜i     --     --
C++ 贪吃蛇 源代码

C++是一种流行的编程语言,它在计算机科学领域广泛应用。贪吃蛇游戏是一种古老而有趣的游戏,是初学者入门编程的好项目。下面是一份C++贪吃蛇源代码,希望对学习者有所帮助。

首先,需要引入标准的C++库和Windows API库,如下面的代码所示:


#include <iostream>

#include <conio.h>

#include <windows.h>

using namespace std;

接下来定义一些全局变量:


const int width = 20;

const int height = 20;

int score = 0;

int highscore = 0;

int snakeX[100], snakeY[100];

int snakeLength = 3;

int fruitX, fruitY;

bool gameover = false;

enum direction LEFT;

direction dir;

上述代码中,width和height分别表示游戏界面的宽度和高度,score和highscore表示得分和最高得分,snakeX和snakeY分别表示蛇身的x坐标和y坐标,snakeLength表示蛇身长度,fruitX和fruitY表示水果的坐标,gameover表示游戏是否结束,dir表示蛇的移动方向。

接下来是主函数,其中包括游戏初始化、游戏循环和游戏结束清理等:


int main()

{

  Setup();

  while (!gameover)

  {

    Draw();

    Input();

    Logic();

    Sleep(100);

  }

  system("pause");

  return 0;

}

游戏初始化函数Setup()包括设置游戏边界、初始化蛇身和水果位置、清空得分和方向等:


void Setup()

{

  dir = STOP;

  snakeX[0] = width / 2;

  snakeY[0] = height / 2;

  fruitX = rand() % width;

  fruitY = rand() % height;

  score = 0;

  system("mode con cols=22 lines=22");

  system("cls");

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

  {

    snakeX[i] = snakeX[i - 1] - 1;

    snakeY[i] = snakeY[i - 1];

  }

}

游戏绘制函数Draw()包括绘制游戏界面、蛇身和水果:


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 == snakeY[0] && j == snakeX[0])

        cout << "O";

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

        cout << "F";

      else

      {

        bool printSnake = false;

        for (int k = 1; k < snakeLength; k++)

        {

          if (i == snakeY[k] && j == snakeX[k])

          

            cout << "o";

            printSnake = true;

          

        }

        if (!printSnake)

          cout << " ";

      }

      if (j == width - 1)

        cout << "#";

    }

    cout << endl;

  }

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

    cout << "#";

  cout << endl;

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

  cout << "Highscore: " << highscore << endl;

}

游戏输入函数Input()包括读取键盘输入并改变蛇的移动方向:


void Input()

{

  if (_kbhit())

  {

    switch (_getch())

    {

    case 'a':

      if (dir != RIGHT)

        dir = LEFT;

      break;

    case 'd':

      if (dir != LEFT)

        dir = RIGHT;

      break;

    case 'w':

      if (dir != DOWN)

        dir = UP;

      break;

    case 's':

      if (dir != UP)

        dir = DOWN;

      break;

    case 'x':

      gameover = true;

      break;

    }

  }

}

游戏逻辑函数Logic()包括蛇的移动、检测碰撞、得分等:


void Logic()

{

  int prevX = snakeX[0];

  int prevY = snakeY[0];

  int prev2X, prev2Y;

  snakeX[0] += (dir == RIGHT) ? 1 : (dir == LEFT) ? -1 : 0;

  snakeY[0] += (dir == DOWN) ? 1 : (dir == UP) ? -1 : 0;

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

  {

    prev2X = snakeX[i];

    prev2Y = snakeY[i];

    snakeX[i] = prevX;

    snakeY[i] = prevY;

    prevX = prev2X;

    prevY = prev2Y;

  }

  if (snakeX[0] == fruitX && snakeY[0] == fruitY)

  {

    score += 10;

    fruitX = rand() % width;

    fruitY = rand() % height;

    if (score > highscore)

      highscore = score;

    snakeLength++;

  }

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

  {

    if (snakeX[i] == snakeX[0] && snakeY[i] == snakeY[0])

      gameover = true;

  }

  if (snakeX[0] < 0 || snakeX[0] >= width || snakeY[0] < 0 || snakeY[0] >= height)

    gameover = true;

}

最后,运行程序即可体验贪吃蛇的乐趣。以上是一份C++贪吃蛇源代码,学习者可以根据自己的水平和兴趣进行定制和改进。

  
  

评论区

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