21xrx.com
2024-11-22 07:14:37 Friday
登录
文章检索 我的文章 写文章
50行最简单的c++贪吃蛇代码
2023-07-02 02:01:41 深夜i     --     --
C++ 贪吃蛇 代码 最简单 50行

本文将介绍一份50行最简单的c++贪吃蛇代码。

贪吃蛇是一种经典的游戏,它的目的是让玩家操纵一条蛇吃掉尽可能多的食物并避免碰到墙壁或自身。以下是代码:

#include

#include

#include

#include

#include

using namespace std;

const int HEIGHT = 20; //游戏区域高度

const int WIDTH = 40; //游戏区域宽度

bool gameOver; //游戏结束标志

int score; //得分

int headX, headY; //蛇头坐标

int fruitX, fruitY; //食物坐标

int tailLength; //尾巴长度

int tailX[HEIGHT * WIDTH], tailY[HEIGHT * WIDTH]; //尾巴坐标

enum direction STOP = 0; //方向

direction dir;

void Setup()

{

  gameOver = false;

  tailLength = 0;

  headX = WIDTH / 2;

  headY = HEIGHT / 2;

  srand(time(NULL));

  fruitX = rand() % (WIDTH - 2) + 1;

  fruitY = rand() % (HEIGHT - 2) + 1;

  score = 0;

  dir = STOP;

}

void Draw()

{

  system("cls");

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

  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 == headY && j == headX) cout << "O";

      else if(i == fruitY && j == fruitX) cout << "F";

      else

      {

        bool print_tail = false;

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

        {

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

            cout << "o";

            print_tail = true;

        }

        if(!print_tail) cout << " ";

      }

      if(j == WIDTH - 1) cout << "#";

    }

    cout << endl;

  }

  for(int i = 0; i < WIDTH + 2; i++) cout << "#";

  cout << 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] = headX;

  tailY[0] = headY;

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

  {

    prev2X = tailX[i];

    prev2Y = tailY[i];

    tailX[i] = prevX;

    tailY[i] = prevY;

    prevX = prev2X;

    prevY = prev2Y;

  }

  switch(dir)

  {

    case LEFT:

      headX--;

      break;

    case RIGHT:

      headX++;

      break;

    case UP:

      headY--;

      break;

    case DOWN:

      headY++;

      break;

    default:

      break;

  }

  if(headX >= WIDTH) headX = 0;

  else if(headX < 0) headX = WIDTH - 1;

  if(headY >= HEIGHT) headY = 0;

  else if(headY < 0) headY = HEIGHT - 1;

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

  {

    if(tailX[i] == headX && tailY[i] == headY)

      gameOver = true;

  }

  if(headX == fruitX && headY == fruitY)

  {

    score += 10;

    fruitX = rand() % (WIDTH - 2) + 1;

    fruitY = rand() % (HEIGHT - 2) + 1;

    tailLength++;

  }

}

int main()

{

  Setup();

  while(!gameOver)

  {

    Draw();

    Input();

    Logic();

    Sleep(20);

  }

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

  return 0;

}

以上就是50行最简单的c++贪吃蛇代码。通过这份代码,您可以了解到贪吃蛇游戏的基本实现原理,并从中学习到一些基本的c++语法。欢迎大家进行修改和探索,尝试将游戏做得更好!

  
  

评论区

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