21xrx.com
2024-11-05 14:51:41 Tuesday
登录
文章检索 我的文章 写文章
C++游戏代码:贪吃蛇
2023-07-01 08:13:02 深夜i     --     --
C++ 游戏 代码 贪吃蛇 编程

贪吃蛇是一款经典的游戏,玩家需要控制一条蛇不断地吃食物,同时避免碰到自己的身体或墙壁。下面是一份基于C++语言的贪吃蛇代码,希望对大家学习编程有所帮助。

首先,在代码中定义了一个蛇类Snake,其中包括蛇的位置、长度、速度等属性,以及蛇移动、吃食物、判断是否碰到身体或墙壁的方法。在主函数中,我们创建了一个Snake对象,并进行游戏逻辑的处理,包括判断按键输入、蛇的移动、吃食物、更新游戏画面等。

代码如下:

#include

#include

#include

#include

using namespace std;

const int WIDTH = 40;

const int HEIGHT = 20;

const int FOOD_NUM = 5;

const int SNAKE_INIT_LEN = 3;

const int GAME_SPEED = 100;

enum DIRECTION DOWN;

class Snake {

private:

  int x, y;

  int len;

  int speed;

  int bodyX[WIDTH * HEIGHT];

  int bodyY[WIDTH * HEIGHT];

public:

  Snake();

  void move(int direction);

  void eatFood(int foodX, int foodY);

  bool isOverlapped();

  bool isHitWall();

  void init();

  void draw();

};

Snake::Snake()

  x = WIDTH / 2;

  y = HEIGHT / 2;

  len = SNAKE_INIT_LEN;

  speed = GAME_SPEED;

void Snake::move(int direction) {

  for (int i = len; i > 0; i--) {

    bodyX[i] = bodyX[i - 1];

    bodyY[i] = bodyY[i - 1];

  }

  switch (direction) {

  case UP:

    y--;

    break;

  case DOWN:

    y++;

    break;

  case LEFT:

    x--;

    break;

  case RIGHT:

    x++;

    break;

  }

  bodyX[0] = x;

  bodyY[0] = y;

}

void Snake::eatFood(int foodX, int foodY) {

  if (x == foodX && y == foodY) {

    bodyX[len] = x;

    bodyY[len] = y;

    len++;

  }

}

bool Snake::isOverlapped() {

  for (int i = 1; i < len; i++) {

    if (x == bodyX[i] && y == bodyY[i])

      return true;

  }

  return false;

}

bool Snake::isHitWall() {

  if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT)

    return true;

  return false;

}

void Snake::init() {

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

    bodyX[i] = x - i;

    bodyY[i] = y;

  }

}

void Snake::draw() {

  system("cls");

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

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

      bool flag = false;

      for (int k = 0; k < len; k++) {

        if (j == bodyX[k] && i == bodyY[k]) {

          if (k == 0)

            cout << "O";

          else

            cout << "o";

          flag = true;

        }

      }

      if (!flag) {

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

          cout << "#";

        else

          cout << " ";

      }

    }

    cout << endl;

  }

}

int main() {

  srand(time(NULL));

  Snake snake;

  snake.init();

  int foodX[FOOD_NUM];

  int foodY[FOOD_NUM];

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

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

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

  }

  int direction = RIGHT;

  while (true) {

    if (_kbhit()) {

      char ch = _getch();

      switch (ch) {

      case 'w':

        if (direction != DOWN)

          direction = UP;

        break;

      case 'a':

        if (direction != RIGHT)

          direction = LEFT;

        break;

      case 's':

        if (direction != UP)

          direction = DOWN;

        break;

      case 'd':

        if (direction != LEFT)

          direction = RIGHT;

        break;

      }

    }

    snake.move(direction);

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

      snake.eatFood(foodX[i], foodY[i]);

    }

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

      if (snake.isOverlapped()) {

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

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

        snake.draw();

      }

    }

    if (snake.isHitWall() || snake.isOverlapped())

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

      break;

    snake.draw();

    Sleep(snake.speed);

  }

  return 0;

}

这份C++贪吃蛇代码虽然不是很复杂,但也涵盖了许多基础知识点,例如类、数组、枚举、按键输入、随机数、图形界面等。通过学习这份代码,我们可以更深入地理解和掌握C++编程语言,同时也可以将所学的知识应用到更加实际的开发场景中。

  
  

评论区

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