# 贪吃蛇 **Repository Path**: wjj912409/retro_snaker ## Basic Information - **Project Name**: 贪吃蛇 - **Description**: No description available - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-11-19 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #include #include #include #include #define high 15 //游戏画面尺寸 #define width 25 // 全局变量 int a[high][width]; //二维数组对应游戏画面中的元素 //0 为空格,1为蛇头@,大于1 为蛇身体*,-1 为边框# ,-2为食物F int movedirection; //小蛇移动方向,1,2,3,4,分别表示上下左右 int food_x,food_y; //记录食物位置 void gotoxy(int x,int y) //光标移动到(x,y)位置 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void HideCursor() // 用于隐藏光标 { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二个值为0表示隐藏光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() // 数据初始化 { int i,j; //以下初始化边框 for(i=0;i0) { //对所有大于0的元素+1 a[i][j]++; //求出最大值max if(max0||a[newhead_i][newhead_j]==-1) { printf("你真菜\n"); exit(0); } else { a[newhead_i][newhead_j]=1; } } void show() // 显示画面 { gotoxy(0,0); // 光标移动到原点位置,以下重画清屏 int i,j; for (i=0;i<=high-1;i++) { for (j=0;j<=width-1;j++) { if (a[i][j]==-1) printf("#"); //输出边框# else if (a[i][j]==1) printf("@"); //输出蛇头@ else if (a[i][j]>1) printf("*"); //输出蛇身* else if(a[i][j]==0) printf(" "); // 输出空格 else if(a[i][j]==-2) printf("F"); // 输出食物F } printf("\n"); } Sleep(100); } void updateWithoutInput() // 与用户输入无关的更新 { move(); } void updateWithInput() // 与用户输入有关的更新 { char input; if(kbhit()) { input=getch(); if(input=='a') movedirection=3; if(input=='d') movedirection=4; if(input=='w') movedirection=1; if(input=='s') movedirection=2; } } int main() { startup(); // 数据初始化 while (1) // 游戏循环执行 { show(); // 显示画面 updateWithoutInput(); // 与用户输入无关的更新 updateWithInput(); // 与用户输入有关的更新 } return 0; }