21xrx.com
2025-06-25 03:27:35 Wednesday
文章检索 我的文章 写文章
C++利用deque实现贪吃蛇游戏
2023-07-07 14:09:15 深夜i     22     0
C++ deque 贪吃蛇游戏

贪吃蛇游戏是一款经典的游戏,它的游戏规则简单,操作容易上手,但是要想程序实现从而完成游戏却相对复杂。使用C++的deque容器可以实现一个简单、易懂、功能完备的贪吃蛇游戏程序。

deque容器是双端队列容器,具有头尾两个指针,可以在队首和队尾插入或删除元素。贪吃蛇游戏中需要使用到的就是双端队列的特点,因为每当蛇吃到一个食物后,蛇的身体就需要增加一节,而这一节就需要从头部插入列表中。当蛇移动的时候,只需要在尾部删除一个方格即可。

下面是一个简单的贪吃蛇游戏程序:

#include<iostream>
#include<deque>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 20;
const char SNAKE = '@';
const char BLANK = ' ';
const char WALL = '#';
const char FOOD = '*';
class SnakeGame {
private:
  int score; // 得分
  int width, height; // 地图长宽
  deque<pair<int, int>> snake; // 蛇的坐标队列
  pair<int, int> food; // 食物的坐标
public:
  SnakeGame(int w = WIDTH, int h = HEIGHT) : width(w), height(h) {
    score = 0;
    for (int i = 0; i < 3; ++i) {
      snake.push_back({ height / 2, width / 2 + i });
    }
    generate_food();
  }
  void generate_food() {
    food.first = rand() % height;
    food.second = rand() % width;
  }
  void show() { // 展示游戏界面
    system("cls"); // 清空终端界面
    for (int i = 0; i <= width + 1; ++i)
      cout << WALL;
    
    cout << endl;
    for (int i = 0; i < height; ++i) {
      cout << WALL;
      for (int j = 0; j < width; ++j) {
        if (i == snake.front().first && j == snake.front().second)
          cout << SNAKE;
        
        else if (i == food.first && j == food.second)
          cout << FOOD;
        
        else {
          bool flag = false;
          for (auto s : snake) {
            if (s.first == i && s.second == j)
              cout << SNAKE;
              flag = true;
              break;
            
          }
          if (!flag)
            cout << BLANK;
          
        }
      }
      cout << WALL << endl;
    }
    for (int i = 0; i <= width + 1; ++i)
      cout << WALL;
    
    cout << endl;
    cout << "Score: " << score << endl;
  }
  bool is_over() { // 判断游戏是否结束
    if (snake.front().first < 0 || snake.front().first >= height
      || snake.front().second < 0 || snake.front().second >= width)
      return true;
    
    for (int i = 1; i < snake.size(); ++i) {
      if (snake.front().first == snake[i].first && snake.front().second == snake[i].second)
        return true;
      
    }
    return false;
  }
  void move(char dir) { // 移动蛇
    pair<int, int> head = snake.front();
    switch (dir) {
    case 'w': // 上
      head.first--;
      break;
    case 'a': // 左
      head.second--;
      break;
    case 's': // 下
      head.first++;
      break;
    case 'd': // 右
      head.second++;
      break;
    }
    if (head.first == food.first && head.second == food.second) { // 如果吃到食物
      snake.push_front(head);
      score++;
      generate_food();
    }
    else {
      snake.push_front(head);
      snake.pop_back();
    }
  }
};
int main() {
  char input;
  SnakeGame game;
  while (true) {
    game.show();
    if (game.is_over()) // 判断游戏是否结束
      cout << "Game Over!" << endl;
      break;
    
    cin >> input;
    game.move(input);
  }
  return 0;
}

上述程序中,将展示游戏界面、判断游戏是否结束、移动蛇三个关键步骤封装到了SnakeGame类中。其中,展示游戏界面通过使用循环按照格式输出游戏界面,判断游戏是否结束需要通过判断蛇头是否碰到四周边界或自身,移动蛇需要按照头部坐标和方向更新双端队列并删除队尾。

通过使用deque容器可以实现贪吃蛇游戏程序,使得程序更加简单、易懂、功能完备,是一个比较实用的C++容器。

  
  

评论区