21xrx.com
2025-03-29 15:36:58 Saturday
文章检索 我的文章 写文章
C++语言编写贪吃蛇程序代码
2023-07-04 21:20:53 深夜i     7     0
C++ 贪吃蛇 编写 程序代码

贪吃蛇是一款非常经典的游戏,也是很多程序员学习编程的开端。在C++语言中,编写贪吃蛇程序代码也是很有挑战性的。下面分享一下C++编写贪吃蛇程序代码的方法。

首先,我们需要了解贪吃蛇游戏的基本要素。贪吃蛇游戏的主要元素包括蛇头、蛇身、食物和墙壁。在程序中,我们需要定义一个蛇结构体,包括蛇的起始坐标、方向和长度等信息。同时,我们还需要定义一个二维数组,来表示游戏地图。地图上的数字分别代表不同的元素,例如,0表示空地,1表示蛇头,2表示蛇身,3表示食物,4表示墙壁。

接下来,我们需要实现贪吃蛇的基本逻辑。当蛇头碰到食物时,蛇的长度会增加,同时生成一个新的食物。当蛇头碰到蛇身或墙壁时,游戏结束。我们需要使用循环语句来不断更新蛇的位置,并判断游戏是否结束。

代码如下:

#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 20;
int map[WIDTH][HEIGHT];
enum Direction LEFT;
Direction dir;
struct Snake
  int xs;
int score;
void drawMap() {
  for (int i = 0; i < WIDTH; i++) {
    for (int j = 0; j < HEIGHT; j++) {
      if (i == 0 || i == WIDTH - 1 || j == 0 || j == HEIGHT - 1) {
        map[i][j] = 4;
      }
      else {
        map[i][j] = 0;
      }
    }
  }
  map[s.x][s.y] = 1;
  for (int i = 0; i < s.len; i++) {
    map[s.x][s.y + i] = 2;
  }
}
void drawSnake() {
  system("cls");
  for (int i = 0; i < WIDTH; i++) {
    for (int j = 0; j < HEIGHT; j++) {
      if (map[i][j] == 0)
        cout << " ";
      
      else if (map[i][j] == 1)
        cout << "@";
      
      else if (map[i][j] == 2) {
        cout << "*";
      }
      else if (map[i][j] == 3)
        cout << "#";
      
      else
        cout << "#";
      
    }
    cout << endl;
  }
  cout << "Score: " << score << endl;
}
void createFood() {
  int x = rand() % (WIDTH - 2) + 1;
  int y = rand() % (HEIGHT - 2) + 1;
  if (map[x][y] == 0) {
    map[x][y] = 3;
  }
  else {
    createFood();
  }
}
void init() {
  srand(time(NULL));
  s.x = 10;
  s.y = 10;
  s.len = 3;
  score = 0;
  dir = RIGHT;
  drawMap();
  createFood();
}
void move() {
  int x = s.x;
  int y = s.y;
  if (dir == UP)
    s.x--;
  
  else if (dir == DOWN) {
    s.x++;
  }
  else if (dir == LEFT)
    s.y--;
  
  else if (dir == RIGHT) {
    s.y++;
  }
  if (map[s.x][s.y] == 2 || map[s.x][s.y] == 4) {
    cout << "Game Over!" << endl;
    exit(0);
  }
  if (map[s.x][s.y] == 3) {
    s.len++;
    score++;
    createFood();
  }
  map[x][y] = 2;
  map[s.x][s.y] = 1;
  for (int i = 0; i < WIDTH; i++) {
    for (int j = 0; j < HEIGHT; j++) {
      if (map[i][j] == 2 && i != x && j != y) {
        map[i][j] = 0;
      }
    }
  }
}
void input() {
  while (_kbhit()) {
    int key = _getch();
    if (key == 'w' || key == 'W') {
      if (dir != DOWN)
        dir = UP;
      
    }
    else if (key == 's' || key == 'S') {
      if (dir != UP)
        dir = DOWN;
      
    }
    else if (key == 'a' || key == 'A') {
      if (dir != RIGHT)
        dir = LEFT;
      
    }
    else if (key == 'd' || key == 'D') {
      if (dir != LEFT)
        dir = RIGHT;
      
    }
  }
}
int main() {
  init();
  while (true) {
    drawSnake();
    input();
    move();
    Sleep(200);
  }
  return 0;
}

上述代码实现了贪吃蛇程序的基本框架,包括生成地图、创建蛇、移动蛇、创建食物、处理输入和更新界面等功能。通过这个例子,我们可以学习到很多C++编程技巧,例如结构体、枚举、递归、随机数等。当然,如果想要让这个游戏更加完美,还可以加入音效、排行榜等功能。不过这已经超出了基础的贪吃蛇程序的范围,需要进一步学习和思考。

  
  

评论区

请求出错了