全部评论 2

  • 开dev c++

    2025-03-29 来自 浙江

    0
  • 运行不了

    2025-03-24 来自 浙江

    0
    • #include <iostream>
      #include <windows.h>
      #include <conio.h>
      #include <time.h>
      using namespace std;

      // 定义游戏区域大小
      #define WIDTH 20
      #define HEIGHT 20

      // 定义蛇的结构体
      struct Snake {
      int x[100];
      int y[100];
      int length;
      int direction;
      };

      // 定义食物的结构体
      struct Food {
      int x;
      int y;
      };

      // 绘制游戏界面
      void draw(Snake snake, Food food) {
      system("cls");
      for (int i = 0; i < WIDTH + 2; i++) {
      cout << "#";
      }
      cout << endl;

      for (int i = 0; i < HEIGHT; i++) {
          for (int j = 0; j < WIDTH; j++) {
              if (j == 0) {
                  cout << "#";
              }
              bool isSnakePart = false;
              for (int k = 0; k < snake.length; k++) {
                  if (snake.x[k] == j && snake.y[k] == i) {
                      cout << "O";
                      isSnakePart = true;
                      break;
                  }
              }
              if (!isSnakePart) {
                  if (food.x == j && food.y == i) {
                      cout << "F";
                  } else {
                      cout << " ";
                  }
              }
              if (j == WIDTH - 1) {
                  cout << "#";
              }
          }
          cout << endl;
      }
      
      for (int i = 0; i < WIDTH + 2; i++) {
          cout << "#";
      }
      cout << endl;
      

      }

      // 初始化蛇和食物
      void init(Snake &snake, Food &food) {
      snake.length = 3;
      snake.x[0] = WIDTH / 2;
      snake.y[0] = HEIGHT / 2;
      for (int i = 1; i < snake.length; i++) {
      snake.x[i] = snake.x[i - 1] - 1;
      snake.y[i] = snake.y[i - 1];
      }
      snake.direction = 1;

      srand(time(NULL));
      food.x = rand() % WIDTH;
      food.y = rand() % HEIGHT;
      

      }

      // 移动蛇
      void move(Snake &snake) {
      for (int i = snake.length - 1; i > 0; i--) {
      snake.x[i] = snake.x[i - 1];
      snake.y[i] = snake.y[i - 1];
      }
      switch (snake.direction) {
      case 0: // 上
      snake.y[0]--;
      break;
      case 1: // 右
      snake.x[0]++;
      break;
      case 2:

      2天前 来自 浙江

      0
    •        snake.y[0]++;
              break;
          case 3: // 左
              snake.x[0]--;
              break;
      }
      

      }

      // 改变蛇的方向
      void changeDirection(Snake &snake) {
      if (_kbhit()) {
      char ch = _getch();
      switch (ch) {
      case 'w':
      if (snake.direction != 2) {
      snake.direction = 0;
      }
      break;
      case 'd':
      if (snake.direction != 3) {
      snake.direction = 1;
      }
      break;
      case 's':
      if (snake.direction != 0) {
      snake.direction = 2;
      }
      break;
      case 'a':
      if (snake.direction != 1) {
      snake.direction = 3;
      }
      break;
      }
      }
      }

      // 检查是否吃到食物
      bool checkEat(Snake &snake, Food &food) {
      if (snake.x[0] == food.x && snake.y[0] == food.y) {
      snake.length++;
      srand(time(NULL));
      food.x = rand() % WIDTH;
      food.y = rand() % HEIGHT;
      return true;
      }
      return false;
      }

      // 检查是否撞到墙壁或自己
      bool checkCollision(Snake snake) {
      if (snake.x[0] < 0 || snake.x[0] >= WIDTH || snake.y[0] < 0 || snake.y[0] >= HEIGHT) {
      return true;
      }
      for (int i = 1; i < snake.length; i++) {
      if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]) {
      return true;
      }
      }
      return false;
      }

      int main() {
      Snake snake;
      Food food;
      init(snake, food);

      while (true) {
          draw(snake, food);
          changeDirection(snake);
          move(snake);
          checkEat(snake, food);
          if (checkCollision(snake)) {
              cout << "Game Over!" << endl;
              break;
          }
          Sleep(100);
      }
      
      return 0;
      

      }

      2天前 来自 浙江

      0

热门讨论