holyya.com
2025-09-04 19:26:11 Thursday
登录
文章检索 我的文章 写文章
C++实现简易版贪吃蛇代码
2023-07-03 03:00:20 深夜i     --     --
C++ 贪吃蛇 简易版 代码实现 游戏

贪吃蛇是一种经典的游戏,在苹果和手机等设备上都有它的身影。如果您想在C++上实现一个简单的贪吃蛇游戏,那么您来对地方了,本文将会为您提供相关的代码。

首先我们需要定义一些变量和函数,用来绘制贪吃蛇和处理用户输入。接下来就是核心的代码部分,首先我们需要将贪吃蛇画在屏幕上,然后我们需要获取用户输入,并根据用户输入移动蛇的位置。当蛇吃到苹果时,就需要增加蛇的长度,并更新蛇的位置。

以下是完整代码:


#include <conio.h> //使用getch(),需要包含conio.h头文件

#include <windows.h> //使用Sleep()函数,需要包含windows.h头文件

bool endgame; //游戏结束判断

int score; //得分

int map_width = 20; //地图宽度

int map_height = 20; //地图高度

int snake_x[100], snake_y[100]; //保存蛇的位置

int apple_x, apple_y; //苹果位置

char dir; //方向

void init() //初始化,绘制边界和第一个苹果

{

  score = 0;

  endgame = false;

  for (int i = 0; i < map_width; i++) //绘制边界

  {

    putchar('#');

  }

  putchar('\n');

  for (int i = 1; i < map_height - 1; i++)

  {

    putchar('#');

    for (int j = 1; j < map_width - 1; j++)

    {

      putchar(' ');

    }

    putchar('#');

    putchar('\n');

  }

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

  {

    putchar('#');

  }

  putchar('\n');

  srand(time(0)); //为了防止随机数种子重复,使用当前时间作为随机数种子

  apple_x = 1 + rand() % (map_width - 2); //生成随机苹果位置

  apple_y = 1 + rand() % (map_height - 2);

  putchar('\r'); //将光标移至行首,准备绘制苹果

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

  {

    putchar('\n');

  }

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

  {

    putchar(' ');

  }

  putchar('O');

}

void update_snake() //更新蛇的位置

{

  int tail_x = snake_x[score - 1]; //保存尾巴位置,稍后用于更新地图

  int tail_y = snake_y[score - 1];

  for (int i = score - 1; i > 0; i--) //更新蛇的位置,从后往前遍历

  {

    snake_x[i] = snake_x[i - 1];

    snake_y[i] = snake_y[i - 1];

  }

  if (dir == 'w') //根据用户输入移动头部

  {

    snake_y[0]--;

  }

  else if (dir == 's')

  {

    snake_y[0]++;

  }

  else if (dir == 'a')

  {

    snake_x[0]--;

  }

  else if (dir == 'd')

  {

    snake_x[0]++;

  }

  if (snake_x[0] == apple_x && snake_y[0] == apple_y) //如果吃到苹果,增加长度

  {

    score++;

    apple_x = 1 + rand() % (map_width - 2);

    apple_y = 1 + rand() % (map_height - 2);

  }

  putchar('\r'); //将光标移至行首,准备更新地图

  for (int i = 1; i < score; i++) //绘制蛇的身体

  {

    if (snake_x[i] == tail_x && snake_y[i] == tail_y) //如果蛇的身体覆盖到了尾巴

    {

      putchar(' ');

    }

    else

    {

      putchar('*');

    }

    if (i != score - 1) //下一格不是尾巴,则移动至下一格

    {

      putchar('\r');

      for (int j = 0; j < snake_y[i]; j++)

      {

        putchar('\n');

      }

      for (int j = 0; j < snake_x[i]; j++)

      {

        putchar(' ');

      }

    }

  }

  if (snake_x[0] < 1 || snake_x[0] > map_width - 2 || snake_y[0] < 1 || snake_y[0] > map_height - 2) //判断是否撞墙了

  

    endgame = true;

  

  else if (snake_x[0] == tail_x && snake_y[0] == tail_y) //判断是否咬到自己了

  

    endgame = true;

  

  putchar('\r');

  for (int i = 0; i < tail_y; i++) //将尾巴擦除

  {

    putchar('\n');

  }

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

  {

    putchar(' ');

  }

  putchar(' ');

}

int main()

{

  init();

  while (!endgame) //持续更新蛇的位置,直到游戏结束

  {

    if (_kbhit()) //判断是否有输入

    {

      dir = _getch(); //获取用户输入,并保存方向

    }

    update_snake();

    Sleep(100); //通过Sleep函数控制更新速度

  }

  putchar('\r');

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

  {

    putchar('\n');

  }

  printf("Game Over! Score: %d", score);

  return 0;

}

现在您已经拥有了一个简单的贪吃蛇游戏,当然,您还可以继续完善它,比如添加闪烁效果、计时器等等,让您的游戏变得更加精彩!

  
  

评论区

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