21xrx.com
2024-06-03 10:12:20 Monday
登录
文章检索 我的文章 写文章
C++双人贪吃蛇代码
2023-07-05 11:29:51 深夜i     --     --
C++ 双人 贪吃蛇 代码

贪吃蛇是一款经典的游戏,不仅深受小朋友的喜欢,也在程序员中广受欢迎。C++作为一种广泛应用于计算机科学领域的编程语言,自然也非常适合编写贪吃蛇游戏。下面将介绍一份C++双人贪吃蛇代码示例。

在编写该代码之前,我们需要先明确游戏的规则。贪吃蛇游戏中,玩家需要操控蛇前进,并尽可能多地吃到食物。每吃到一个食物,蛇的长度就会增加,同时游戏难度也会随之提高。如果蛇头撞到了游戏边界或自己的身体,那么游戏就会结束。

C++双人贪吃蛇代码示例:


#include <iostream>

#include <conio.h>

#include <windows.h>

using namespace std;

bool gameOver;

const int width = 20;

const int height = 20;

int x1, y1, x2, y2, fruitX, fruitY, score1, score2;

int tailX[100], tailY[100];

int nTail1=0, nTail2=0;

enum eDirection RIGHT;

eDirection dir1, dir2;

void Setup()

{

  gameOver = false;

  dir1 = STOP;

  dir2 = STOP;

  x1 = width / 4;

  y1 = height / 2;

  x2 = 3 * width / 4;

  y2 = height / 2;

  fruitX = rand() % width;

  fruitY = rand() % height;

  score1 = 0;

  score2 = 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 == y1 && j == x1)

        cout << "A";

      else if (i == y2 && j == x2)

        cout << "B";

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

        cout << "F";

      else

      {

        bool print = false;

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

        {

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

          

            print = true;

            cout << "a";

          

        }

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

        {

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

          

            print = true;

            cout << "b";

          

        }

        if (!print)

          cout << " ";

      }

      if (j == width - 1)

        cout << "#";

    }

    cout << endl;

  }

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

    cout << "#";

  cout << endl;

}

void Input1()

{

  if (_kbhit()) //如果有输入

  {

    switch (_getch()) //获取输入

    

    case 'a':

      dir1 = LEFT;

      break;

    case 'd':

      dir1 = RIGHT;

      break;

    case 'w':

      dir1 = UP;

      break;

    case 's':

      dir1 = DOWN;

      break;

    case 'x':

      gameOver = true;

      break;

    

  }

}

void Input2()

{

  if (_kbhit()) //如果有输入

  {

    switch (_getch()) //获取输入

    

    case 'j':

      dir2 = LEFT;

      break;

    case 'l':

      dir2 = RIGHT;

      break;

    case 'i':

      dir2 = UP;

      break;

    case 'k':

      dir2 = DOWN;

      break;

    case 'm':

      gameOver = true;

      break;

    

  }

}

void Logic()

{

  int prevX1 = tailX[0];

  int prevY1 = tailY[0];

  int prevX2 = tailX[0];

  int prevY2 = tailY[0];

  int prev2X, prev2Y;

  tailX[0] = x1;

  tailY[0] = y1;

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

  {

    prev2X = tailX[i];

    prev2Y = tailY[i];

    tailX[i] = prevX1;

    tailY[i] = prevY1;

    prevX1 = prev2X;

    prevY1 = prev2Y;

  }

  int prev2X2, prev2Y2;

  tailX[0] = x2;

  tailY[0] = y2;

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

  {

    prev2X2 = tailX[i];

    prev2Y2 = tailY[i];

    tailX[i] = prevX2;

    tailY[i] = prevY2;

    prevX2 = prev2X2;

    prevY2 = prev2Y2;

  }

  switch (dir1)

  {

  case LEFT:

    x1--;

    break;

  case RIGHT:

    x1++;

    break;

  case UP:

    y1--;

    break;

  case DOWN:

    y1++;

    break;

  default:

    break;

  }

  if (x1 >= width)

    x1 = 0;

  else if (x1 < 0)

    x1 = width - 1;

  if (y1 >= height)

    y1 = 0;

  else if (y1 < 0)

    y1 = height - 1;

  if (x1 == fruitX && y1 == fruitY)

  {

    score1 += 10;

    fruitX = rand() % width;

    fruitY = rand() % height;

    nTail1++;

  }

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

    if (tailX[i] == x1 && tailY[i] == y1)

      gameOver = true;

  switch (dir2)

  {

  case LEFT:

    x2--;

    break;

  case RIGHT:

    x2++;

    break;

  case UP:

    y2--;

    break;

  case DOWN:

    y2++;

    break;

  default:

    break;

  }

  if (x2 >= width)

    x2 = 0;

  else if (x2 < 0)

    x2 = width - 1;

  if (y2 >= height)

    y2 = 0;

  else if (y2 < 0)

    y2 = height - 1;

  if (x2 == fruitX && y2 == fruitY)

  {

    score2 += 10;

    fruitX = rand() % width;

    fruitY = rand() % height;

    nTail2++;

  }

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

    if (tailY[i] == y2 && tailX[i] == x2)

      gameOver = true;

}

void Print()

{

  cout << "Player A Score:" << score1 << endl;

  cout << "Player B Score:" << score2 << endl;

  if (gameOver)

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

}

int main()

{

  Setup();

  while (!gameOver)

  {

    Draw();

    Input1();

    Input2();

    Logic();

    Print();

    Sleep(100);

  }

  return 0;

}

该代码实现了基本的双人贪吃蛇游戏,其中通过枚举类型eDirection定义了蛇前进的方向。在Setup()函数中初始化游戏,Draw()函数负责绘制游戏界面,Input1()和Input2()函数分别处理两个玩家的输入。Logic()函数处理游戏逻辑,Print()函数输出游戏的得分和结束信息。

在运行该代码时,我们需要先将conio.h和windows.h头文件引入,否则会编译错误。同时,代码中使用了_kbhit()和_getch()函数来获取玩家的输入。_kbhit()函数用于判断是否有输入,_getch()函数用于获取输入的字符。

以上是一份简单的C++双人贪吃蛇代码示例,希望对初学者学习C++语言有所帮助。

  
  

评论区

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