21xrx.com
2024-12-23 01:17:12 Monday
登录
文章检索 我的文章 写文章
「分享」贪吃蛇C++代码
2023-06-24 14:12:53 深夜i     --     --
C++ 贪吃蛇 代码 分享 编程

贪吃蛇是一款经典的游戏,它的玩法简单却富有趣味性。在这篇文章中,我将分享一份使用C++编写的贪吃蛇代码。

在开始编程之前,需要先定义几个必要的变量。首先是游戏地图的大小,可以根据自己的需求进行调整。其次是蛇头、蛇身和食物的坐标,以及蛇当前的方向。

接下来是主要的代码部分。在游戏开始前,先绘制出游戏地图,包括边框,蛇头,蛇身和食物。接着进入游戏循环,不断地更新蛇的位置,移动方向也随之改变。如果蛇头碰到边框或碰到蛇身,游戏结束。如果蛇头碰到食物,则将食物重新放置在地图上,并增加蛇的长度。

实现这个游戏需要使用一些C++的基础知识,包括循环、条件判断和数组操作等。总之,通过这个简单的游戏例子,我们能够掌握一些基础的编程技能,也能够快速体验到编程的乐趣。

下面是完整的C++代码,大家可以根据自己的实际情况进行修改和优化。


#include<iostream>

#include<conio.h>

using namespace std;

const int WIDTH = 50;

const int HEIGHT = 30;

const int SNAKE_MAX_LENGTH = (WIDTH - 2) * (HEIGHT - 2);

class SnakeGame {

private:

  int map[HEIGHT][WIDTH];

  int snakeX[SNAKE_MAX_LENGTH], snakeY[SNAKE_MAX_LENGTH];

  int snakeLength;

  int foodX, foodY;

  int direction;

  int score;

public:

  SnakeGame() {

    snakeLength = 1;

    snakeX[0] = WIDTH / 2;

    snakeY[0] = HEIGHT / 2;

    foodX = 0;

    foodY = 0;

    score = 0;

    direction = 0;

    InitMap();

  }

  void InitMap() {

    for (int i = 0; i < HEIGHT; i++) {

      for (int j = 0; j < WIDTH; j++) {

        if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {

          map[i][j] = -1;

        }

        else {

          map[i][j] = 0;

        }

      }

    }

    DrawSnake();

    DrawFood();

  }

  void DrawSnake() {

    for (int i = 0; i < snakeLength; i++) {

      map[snakeY[i]][snakeX[i]] = 1;

    }

  }

  void DrawFood() {

    srand(time(NULL));

    while (true) {

      foodX = rand() % (WIDTH - 2) + 1;

      foodY = rand() % (HEIGHT - 2) + 1;

      if (map[foodY][foodX] == 0) {

        map[foodY][foodX] = 2;

        break;

      }

    }

  }

  void MoveSnake() {

    int newX = snakeX[snakeLength - 1];

    int newY = snakeY[snakeLength - 1];

    if (direction == 0) newX++;

    if (direction == 1) newY--;

    if (direction == 2) newX--;

    if (direction == 3) newY++;

    if (newX == foodX && newY == foodY) {

      score += 10;

      map[foodY][foodX] = 0;

      snakeLength++;

      snakeX[snakeLength - 1] = snakeX[snakeLength - 2];

      snakeY[snakeLength - 1] = snakeY[snakeLength - 2];

      DrawFood();

    }

    for (int i = 0; i < snakeLength - 1; i++) {

      snakeX[i] = snakeX[i + 1];

      snakeY[i] = snakeY[i + 1];

    }

    snakeX[snakeLength - 1] = newX;

    snakeY[snakeLength - 1] = newY;

  }

  void UpdateGame() {

    MoveSnake();

    if (snakeX[snakeLength - 1] == 0

      || snakeX[snakeLength - 1] == WIDTH - 1

      || snakeY[snakeLength - 1] == 0

      || snakeY[snakeLength - 1] == HEIGHT - 1) {

      cout << "Game Over!" << endl;

      _getch();

      exit(0);

    }

    for (int i = 0; i < snakeLength - 1; i++) {

      if (snakeX[i] == snakeX[snakeLength - 1] && snakeY[i] == snakeY[snakeLength - 1]) {

        cout << "Game Over!" << endl;

        _getch();

        exit(0);

      }

    }

  }

  void DisplayGame() {

    system("cls");

    for (int i = 0; i < HEIGHT; i++) {

      for (int j = 0; j < WIDTH; j++) {

        if (map[i][j] == -1)

          cout << "#";

        

        else if (map[i][j] == 0)

          cout << " ";

        

        else if (map[i][j] == 1) {

          cout << "*";

        }

        else if (map[i][j] == 2) {

          cout << "$";

        }

      }

      cout << endl;

    }

    cout << "Score:" << score << endl;

  }

  void InputOperation() {

    if (_kbhit()) {

      switch (_getch())

      case 'w':

        direction = 1;

        break;

      case 's':

        direction = 3;

        break;

      case 'a':

        direction = 2;

        break;

      case 'd':

        direction = 0;

        break;

      

    }

  }

  void Run() {

    while (true) {

      InputOperation();

      UpdateGame();

      DisplayGame();

      Sleep(200);

    }

  }

};

int main() {

  SnakeGame game;

  game.Run();

  return 0;

}

  
  

评论区

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