21xrx.com
2025-04-28 03:06:17 Monday
文章检索 我的文章 写文章
C++贪吃蛇代码
2023-07-03 00:06:58 深夜i     16     0
C++ 贪吃蛇 代码 游戏 控制台界面

贪吃蛇是一款经典游戏,可以锻炼玩家的反应能力和思维能力,同时也是许多程序员开始学习编程的练手小项目。下面,让我们来看一下C++贪吃蛇代码的实现。

首先,我们需要定义蛇的数据结构,包括蛇的长度、当前的位置和方向等信息。我们可以使用一个链表来表示蛇的身体,其中每个节点代表蛇的一节身体,通过指针来连接起来。

接下来,我们需要定义食物的位置和吃食物的逻辑。每次蛇吃到一个食物,就在蛇的尾部添加一个新的节点,同时更新食物的位置,让蛇能够不断地成长。

再来,我们需要实现蛇的移动。蛇的移动可以通过更新蛇头位置,然后把蛇的身体节点顺次前移来实现。当蛇头与蛇身重合时即为游戏失败。

最后,我们还需要实现蛇的控制。玩家可以通过键盘控制蛇的方向,例如按下上键让蛇向上移动,按下左键让蛇向左移动等。

接下来是一个简单的C++贪吃蛇实现代码:

#include<iostream>
#include<conio.h>
#include<Windows.h>
using namespace std;
const int weight = 25, hight = 25;
int x, y, fruit_x, fruit_y, score;
int tail_x[100], tail_y[100];
int n_tail;
bool game_over;
enum Direction DOWN ;
Direction dir;
void Setup() {
  game_over = false;
  dir = STOP;
  x = weight / 2;
  y = hight / 2;
  fruit_x = rand() % weight;
  fruit_y = rand() % hight;
  score = 0;
}
void Draw() {
  system("cls");
  for (int i = 0; i <= weight; i++)
    cout << "#";
  
  cout << endl;
  for (int i = 0; i < hight; i++) {
    for (int j = 0; j < weight; j++) {
      if (j == 0 || j == weight - 1)
        cout << "#";
      
      else if (i == y && j == x)
        cout << "O";
      
      else if (i == fruit_y && j == fruit_x)
        cout << "@";
      
      else {
        bool print = false;
        for (int k = 0; k < n_tail; k++) {
          if (tail_x[k] == j && tail_y[k] == i)
            print = true;
            cout << "o";
          
        }
        if (!print)
          cout << " ";
        
      }
    }
    cout << endl;
  }
  for (int i = 0; i <= weight; i++)
    cout << "#";
  
  cout << endl;
  cout << "Score: " << score << endl;
  cout << "Use arrow keys to move" << 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':
      game_over = true;
      break;
    
  }
}
void Logic() {
  int prev_x = tail_x[0];
  int prev_y = tail_y[0];
  int tmp_x, tmp_y;
  tail_x[0] = x;
  tail_y[0] = y;
  for (int i = 1; i < n_tail; i++) {
    tmp_x = tail_x[i];
    tmp_y = tail_y[i];
    tail_x[i] = prev_x;
    tail_y[i] = prev_y;
    prev_x = tmp_x;
    prev_y = tmp_y;
  }
  switch (dir) {
  case LEFT:
    x--;
    break;
  case RIGHT:
    x++;
    break;
  case UP:
    y--;
    break;
  case DOWN:
    y++;
    break;
  }
  if (x < 0 || x >= weight || y < 0 || y >= hight)
    game_over = true;
  
  if (x == fruit_x && y == fruit_y) {
    score += 10;
    fruit_x = rand() % weight;
    fruit_y = rand() % hight;
    n_tail++;
  }
  for (int i = 0; i < n_tail; i++) {
    if (tail_x[i] == x && tail_y[i] == y)
      game_over = true;
    
  }
}
int main() {
  Setup();
  while (!game_over) {
    Draw();
    Input();
    Logic();
    Sleep(50);
  }
  return 0;
}

以上就是一个简单的C++贪吃蛇实现代码,大家可以根据自己的需求进行修改和完善。通过这个实践,不仅可以帮助大家更好地学习C++语言,还可以锻炼自己的编程能力,同时也可以体验到实现一个经典游戏的成就感。

  
  

评论区

请求出错了