21xrx.com
2024-09-20 00:49:02 Friday
登录
文章检索 我的文章 写文章
C++语言编写贪吃蛇游戏的最简单代码
2023-07-03 01:16:15 深夜i     --     --
C++ 编写 贪吃蛇 游戏 最简单代码

在编程领域中,C++语言一直是被广泛应用的语言之一。其操作简单,功能强大,适用于各种应用场景。本文将介绍使用C++语言编写贪吃蛇游戏的最简单代码。

贪吃蛇游戏是一款经典的电脑游戏,其规则简单,玩法有趣,风靡全球。在游戏中,玩家需要控制一个蛇通过移动吃下屏幕上的食物,每吃一个食物蛇的长度就增加1个单位,并且游戏速度会逐渐加快。如果蛇的头部碰到了游戏界面上的边界或者自己的身体,则游戏结束。

为了编写一个最简单的贪吃蛇游戏,我们需要使用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 RIGHT;

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(10);

  }

  return 0;

}

上述代码主要分为四个部分:设置、绘制、输入和逻辑。其中,设置部分主要设置了游戏的初始状态,包括游戏是否结束、蛇的状态和位置、食物的位置和得分等。绘制部分则是根据蛇和食物的位置绘制游戏界面,并显示当前得分。输入部分则是监听玩家的键盘输入,并根据输入修改蛇的运动方向。逻辑部分则是根据蛇的运动状态判断游戏是否结束,以及处理蛇与食物相遇时的得分和蛇的长度。

通过上述代码,我们可以很轻松地编写出一个简单的贪吃蛇游戏,为学习C++语言的同学提供了一个很好的参考。

  
  

评论区

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