1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<ctime> 5 #include<conio.h> 6 using namespace std; 7 8 typedef struct{ int x, y; }Point; 9 10 char map[22][22]; //定义一个22*22的地图(含边界) 11 Point snake[400], food, Next; //定义蛇、食物、下一步蛇头的位置 12 int head, tail; //用于储存蛇头和蛇尾的下标 13 int grade, length, autotime; //游戏等级、蛇长、自动前进所需时间 14 char direction; //前进方向 15 16 //用inline定义内联函数节省程序运行时的调用开销 17 //刷新地图 18 inline void Update(char map[][22], int grade, int length, int autotime) 19 { 20 system("cls"); //清屏 21 int i, j; 22 printf("\n"); 23 for (i = 0; i < 22; i++) 24 { 25 printf("\t"); 26 for (j = 0; j < 22; j++) 27 printf("%c ", map[i][j]); 28 if (i == 0) 29 printf("\t等级为:%d", grade); 30 if (i == 2) 31 printf("\t长度为:%d", length); 32 if (i == 6) 33 printf("\t自动前进时间"); 34 if (i == 8) 35 printf("\t间隔为:%d ms", autotime); 36 printf("\n"); 37 } 38 } 39 40 //欢迎界面 41 inline void hello() 42 { 43 puts("\n\n\n\t\t\t贪吃蛇游戏即将开始!"); //准备开始 44 double start; 45 for (int i = 3; i >= 0; i--) 46 { 47 start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前为止运行的时间 48 while ((double)clock() / CLOCKS_PER_SEC - start <= 1); //经过1秒之后 49 if (i > 0) 50 { 51 system("cls"); //清屏 52 printf("\n\n\n\t\t\t进入倒计时:%d\n", i); //倒计时 53 } 54 else 55 Update(map, grade, length, autotime); //刷新地图 56 } 57 } 58 59 //随机生成食物位置 60 inline void f() 61 { 62 srand(int(time(0))); //调用种子函数 63 do{ 64 food.x = rand() % 20 + 1; 65 food.y = rand() % 20 + 1; 66 } while (map[food.x][food.y] != ‘ ‘); 67 map[food.x][food.y] = ‘!‘; //食物为“!” 68 } 69 70 //初始化 71 inline void init() 72 { 73 int i, j; 74 for (i = 1; i <= 20; i++) 75 for (j = 1; j <= 20; j++) 76 map[i][j] = ‘ ‘; 77 for (i = 0; i <= 21; i++) 78 map[0][i] = map[21][i] = map[i][0] = map[i][21] = ‘*‘; //边界 79 map[1][1] = map[1][2] = ‘O‘; //蛇身(含蛇尾) 80 map[1][3] = ‘@‘; //蛇头 81 head = 2; tail = 0; //开始时头和尾的下标 82 snake[head].x = 1; snake[head].y = 3; //开始时蛇头在地图上的下标 83 snake[tail].x = 1; snake[tail].y = 1; //开始时蛇尾在地图上的下标 84 snake[1].x = 1; snake[1].y = 2; //开始时蛇身在地图上的下标 85 f(); //随机生成食物位置 86 grade = 1; length = 3; autotime = 500; //开始的等级、长度、自动前进时间 87 direction = 77; //初始的运动方向向右 88 } 89 90 //预前进 91 inline int GO() 92 { 93 bool timeover = true; 94 double start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前为止运行的时间 95 96 //自动经过1秒或者等待1秒内的键盘输入 97 while ((timeover = ((double)clock() / CLOCKS_PER_SEC - start <= autotime / 1000.0)) && !_kbhit()); 98 //键盘输入 99 if (timeover) 100 { 101 _getch(); 102 direction = _getch(); //获取方向 103 } 104 switch (direction) 105 { 106 case 72: 107 Next.x = snake[head].x - 1; Next.y = snake[head].y; //向上 108 break; 109 case 80: 110 Next.x = snake[head].x + 1; Next.y = snake[head].y; //向下 111 break; 112 case 75: 113 Next.x = snake[head].x; Next.y = snake[head].y - 1; //向左 114 break; 115 case 77: 116 Next.x = snake[head].x; Next.y = snake[head].y + 1; //向右 117 break; 118 default: 119 puts("\tGame over!"); //按下非方向键游戏失败 120 return 0; 121 } 122 if (Next.x == 0 || Next.x == 21 || Next.y == 0 || Next.y == 21) //撞到边界 123 { 124 puts("\tGame over!"); 125 return 0; 126 } 127 if (map[Next.x][Next.y] != ‘ ‘&&!(Next.x == food.x&&Next.y == food.y)) //吃到自己 128 { 129 puts("\tGame over!"); 130 return 0; 131 } 132 if (length == 400) //最长蛇长 133 { 134 puts("\tGood game!"); 135 return 0; 136 } 137 return 1; 138 } 139 140 //吃到食物 141 inline void EAT() 142 { 143 length++; //长度增加1 144 int _grade = length / 10 + 1; //计算等级 145 if (_grade != grade) 146 { 147 grade = _grade; 148 if (autotime >= 100) 149 autotime = 550 - grade * 50; //增加一级自动时间减短50毫秒 150 } 151 map[Next.x][Next.y] = ‘@‘; //蛇头位置变化 152 map[snake[head].x][snake[head].y] = ‘O‘; //原蛇头位置变化为蛇身 153 head = (head + 1) % 400; //蛇头下标加1 154 snake[head].x = Next.x; snake[head].y = Next.y; //蛇头下标变化 155 f(); //随机生成食物位置 156 Update(map, grade, length, autotime); //刷新地图 157 } 158 159 //没吃到食物 160 inline void FAILURE() 161 { 162 map[snake[tail].x][snake[tail].y] = ‘ ‘; //蛇尾原来的位置变成“ ” 163 tail = (tail + 1) % 400; //蛇尾下标加1 164 map[Next.x][Next.y] = ‘@‘; //蛇头位置变化 165 map[snake[head].x][snake[head].y] = ‘O‘; //原蛇头位置变化为蛇身 166 head = (head + 1) % 400; //蛇头下标加1 167 snake[head].x = Next.x; //蛇头下标变化 168 snake[head].y = Next.y; 169 Update(map, grade, length, autotime); //刷新地图 170 } 171 172 //main函数 173 int main() 174 { 175 init(); //初始化 176 hello(); //欢迎界面 177 while (1) 178 { 179 if (GO()) //预前进 180 { 181 if (Next.x == food.x&&Next.y == food.y) 182 EAT(); //吃到食物 183 else 184 FAILURE(); //没吃到食物 185 } 186 else 187 return 0; //失败或者胜利,游戏结束 188 } 189 return 0; 190 } 191 //
时间: 2024-10-26 19:04:06