首页 > 代码库 > c++ 贪吃蛇小游戏

c++ 贪吃蛇小游戏

(地图大小为25*25,蛇初始位置为数组的map[3][3]~map[3][6],蛇头为map[3][6],方向向右)

#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <time.h> 
#include <conio.h>
using namespace std;

#define MAX_WIDE 25
#define MAX_HIGH 25

char map[MAX_HIGH][MAX_WIDE];    //地图大小

struct node
{
int x, y;
};
node snack[(MAX_WIDE - 2)*(MAX_HIGH - 2)], food;    //定义蛇身和食物坐标

int lenth, direct, live = 1;                  //蛇的长度,方向和存活
int speech = 200;

void initMap(node *n, int len) {              //初始化地图
  for (int i = 0; i < MAX_HIGH; i++)
    for (int j = 0; j < MAX_WIDE; j++)
      map[i][j] = ‘ ‘;
  for (int i = 0; i < MAX_HIGH; i++)          //设置左右的墙
    map[i][0] = map[i][MAX_WIDE - 1] = ‘|‘;
  for (int j = 0; j < MAX_WIDE; j++)
    map[0][j] = map[MAX_HIGH - 1][j] = ‘-‘;     //设置上下的墙
  for (int j = 1; j < len; j++)
    map[n[j].x][n[j].y] = ‘*‘;             //设置蛇身
  map[n[0].x][n[0].y] = ‘#‘;              //设置蛇头
}  

 

void showMap() {                     //输出数组map
  system("cls");                    //清屏
  for (int i = 0; i < MAX_HIGH; i++) {         
    for (int j = 0; j < MAX_WIDE; j++) {
      cout << map[i][j];
    }
    cout << endl;
  }
}

 

void showFood() {                    //产生食物,坐标在map中且不为蛇上
  srand((unsigned)time(NULL));            //食物的产生点随机
  do {
    food.x = rand() % MAX_WIDE;
    food.y = rand() % MAX_HIGH;
  } while (map[food.x][food.y] != ‘ ‘);
  map[food.x][food.y] = ‘o‘;
}

int updataGame() {                   //更新数组
  long start = clock();
  int a, b;                       //暂存 移动后蛇头的位置
  while (!_kbhit() && (clock() - start <= speech));   //直到有按键或者时间过了speech则跳出循环
  if (_kbhit()) {
    _getch();                    //上、下、左、右键是二个字节的,取第二个字节分别为(72,80,75,77)  
    direct = _getch();
  }
  switch (direct)
  {
  case 72:                       //up
    a = snack[0].x - 1;
    b = snack[0].y;
    break;
  case 80:                       //down
    a = snack[0].x + 1;
    b = snack[0].y;
    break;
  case 75:                       //left
    a = snack[0].x;
    b = snack[0].y - 1;
    break;
  case 77:                       //right
    a = snack[0].x;
    b = snack[0].y + 1;
    break;
  default:
    break;
  }
  if (a == 0 || a == MAX_HIGH - 1 || b == 0 || b == MAX_WIDE - 1)    //撞墙
    live = 0;
  if (map[a][b] == ‘*‘)                            //撞自身
    live = 0;
  map[snack[0].x][snack[0].y] = ‘*‘;                     //将蛇头置为蛇身
  map[snack[lenth - 1].x][snack[lenth - 1].y] = ‘ ‘;              //蛇尾置为空
  map[a][b] = ‘#‘;                              //更新蛇头
  for (int i = lenth - 1; i > 0; i--)                       //更新蛇数组
    snack[i] = snack[i - 1];
  snack[0].x = a;
  snack[0].y = b;
  if (a == food.x && b == food.y) {                      //蛇吃了食物
    map[snack[lenth - 1].x][snack[lenth - 1].y] = ‘*‘;            //取消蛇尾置空
    lenth++;
    showFood();                               //产生新食物
    speech -= lenth;                             //加速
  }
  showMap();
  return live;
}


int main() {
  snack[0].x = 3;
  snack[0].y = 6;
  lenth = 4;
  for (int i = 1; i < lenth; i++) {
    snack[i].x = 3;
    snack[i].y = snack[i - 1].y - 1;
  }
  direct = 77;
  initMap(snack, lenth);
  showFood();
  showMap();
  do {
    updataGame();
  } while (live);
  cout << "得分为:" << lenth-4 << endl;
  system("pause");
  return 0;
}

c++ 贪吃蛇小游戏