21xrx.com
2024-09-19 09:45:21 Thursday
登录
文章检索 我的文章 写文章
C++ 贪吃蛇程序代码
2023-07-04 21:18:25 深夜i     --     --
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 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(100);

  }

  return 0;

}

代码中包含了游戏的逻辑、绘制、输入和主函数。其中包含了一些关键变量和函数,如当前位置(x, y)、水果位置(fruitX, fruitY)、方向(dir)、分数(score)以及绘制屏幕元素的函数(Draw)、输入函数(Input)和游戏逻辑函数(Logic)。

通过以上代码,我们可以运行一款简单的贪吃蛇游戏。如需进行更多的游戏优化、添加新特性和玩法,还需要不断地改进代码和加强游戏逻辑。

  
  
下一篇: C++程序求合数

评论区

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