科学教育

搜索
查看: 556|回复: 1
打印 上一主题 下一主题

帮我生成贪吃蛇代码(c++)

[复制链接]

2

主题

3

帖子

10

积分

新手上路

Rank: 1

积分
10
跳转到指定楼层
楼主
发表于 2025-10-16 12:02:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成贪吃蛇代码(c++)
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-10-16 12:03:10 | 只看该作者
当我们编写贪吃蛇游戏的代码时,首先要考虑的是游戏中需要什么元素以及它们之间的交互。贪吃蛇游戏通常包括蛇身、食物和游戏界面等要素。在C++中,可以使用控制台来实现简单的贪吃蛇游戏。

这是一个简单的C++代码示例,实现了一个基本的贪吃蛇游戏:

  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4. using namespace std;

  5. bool gameOver;
  6. const int width = 20;
  7. const int height = 20;
  8. int x, y, fruitX, fruitY, score;
  9. int tailX[100], tailY[100];
  10. int nTail;
  11. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN};
  12. eDirection dir;

  13. void Setup()
  14. {
  15.     gameOver = false;
  16.     dir = STOP;
  17.     x = width / 2;
  18.     y = height / 2;
  19.     fruitX = rand() % width;
  20.     fruitY = rand() % height;
  21.     score = 0;
  22. }

  23. void Draw()
  24. {
  25.     system("cls");
  26.     for (int i = 0; i < width+2; i++)
  27.         cout << "#";
  28.     cout << endl;

  29.     for (int i = 0; i < height; i++)
  30.     {
  31.         for (int j = 0; j < width; j++)
  32.         {
  33.             if (j == 0)
  34.                 cout << "#";
  35.             if (i == y && j == x)
  36.                 cout << "O";
  37.             else if (i == fruitY && j == fruitX)
  38.                 cout << "F";
  39.             else
  40.             {
  41.                 bool print = false;
  42.                 for (int k = 0; k < nTail; k++)
  43.                 {
  44.                     if (tailX[k] == j && tailY[k] == i)
  45.                     {
  46.                         cout << "o";
  47.                         print = true;
  48.                     }
  49.                 }
  50.                 if (!print)
  51.                     cout << " ";
  52.             }

  53.             if (j == width - 1)
  54.                 cout << "#";
  55.         }
  56.         cout << endl;
  57.     }

  58.     for (int i = 0; i < width+2; i++)
  59.         cout << "#";
  60.     cout << endl;
  61.     cout << "Score:" << score << endl;
  62. }

  63. void Input()
  64. {
  65.     if (_kbhit())
  66.     {
  67.         switch (_getch())
  68.         {
  69.         case 'a':
  70.             dir = LEFT;
  71.             break;
  72.         case 'd':
  73.             dir = RIGHT;
  74.             break;
  75.         case 'w':
  76.             dir = UP;
  77.             break;
  78.         case 's':
  79.             dir = DOWN;
  80.             break;
  81.         case 'x':
  82.             gameOver = true;
  83.             break;
  84.         }
  85.     }
  86. }

  87. void Logic()
  88. {
  89.     int prevX = tailX[0];
  90.     int prevY = tailY[0];
  91.     int prev2X, prev2Y;
  92.     tailX[0] = x;
  93.     tailY[0] = y;
  94.     for (int i = 1; i < nTail; i++)
  95.     {
  96.         prev2X = tailX[i];
  97.         prev2Y = tailY[i];
  98.         tailX[i] = prevX;
  99.         tailY[i] = prevY;
  100.         prevX = prev2X;
  101.         prevY = prev2Y;
  102.     }
  103.     switch (dir)
  104.     {
  105.     case LEFT:
  106.         x--;
  107.         break;
  108.     case RIGHT:
  109.         x++;
  110.         break;
  111.     case UP:
  112.         y--;
  113.         break;
  114.     case DOWN:
  115.         y++;
  116.         break;
  117.     default:
  118.         break;
  119.     }
  120.     if (x >= width) x = 0; else if (x < 0) x = width - 1;
  121.     if (y >= height) y = 0; else if (y < 0) y = height - 1;

  122.     for (int i = 0; i < nTail; i++)
  123.         if (tailX[i] == x && tailY[i] == y)
  124.             gameOver = true;

  125.     if (x == fruitX && y == fruitY)
  126.     {
  127.         score += 10;
  128.         fruitX = rand() % width;
  129.         fruitY = rand() % height;
  130.         nTail++;
  131.     }
  132. }

  133. int main()
  134. {
  135.     Setup();
  136.     while (!gameOver)
  137.     {
  138.         Draw();
  139.         Input();
  140.         Logic();
  141.         Sleep(10); // 控制游戏速度
  142.     }
  143.     return 0;
  144. }
复制代码


你可以尝试运行这段代码,并使用WASD键来移动蛇。吃到食物后,蛇的身体会变长,同时得分也会增加。当蛇碰到自己的身体或游戏边界时,游戏结束。希望这个解答对你有帮助!如果有任何问题,请随时告诉我。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表