《贪吃蛇游戏c语言源代码学习》由会员分享,可在线阅读,更多相关《贪吃蛇游戏c语言源代码学习(11页珍藏版)》请在金锄头文库上搜索。
1、贪吃蛇游戏C语言源代码学习阅读学习了源代码,并做了简单的注释和修改,里面只用了链表数据结构,非常适合C语言入门者学习阅读。程序可在VS2013下编译运行。 1 #include 2 #include 3 #include 4 #include 5 6 #define U 1 7 #define D 2 8 #define L 3 9 #define R 4 /蛇的状态,U:上 ;D:下;L:左 R:右 10 11 typedef struct SNAKE /蛇身的一个节点 12 13 int x; 14 int y; 15 struct SNAKE *next; 16 snake; 17 18
2、 /全局变量/ 19 int score = 0, add = 10;/总得分与每次吃食物得分。 20 int status, sleeptime = 200;/每次运行的时间间隔 21 snake *head, *food;/蛇头指针,食物指针 22 snake *q;/遍历蛇的时候用到的指针 23 int endGamestatus = 0; /游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。 24 25 /声明全部函数/ 26 void Pos(); 27 void creatMap(); 28 void initSnake(); 29 int biteSelf(); 30
3、void createFood(); 31 void cantCrossWall(); 32 void snakeMove(); 33 void pause(); 34 void runGame(); 35 void initGame(); 36 void endGame(); 37 void gameStart(); 38 39 void Pos(int x, int y)/设置光标位置 40 41 COORD pos; 42 HANDLE hOutput; 43 pos.X = x; 44 pos.Y = y; 45 hOutput = GetStdHandle(STD_OUTPUT_HA
4、NDLE);/返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄 46 SetConsoleCursorPosition(hOutput, pos); 47 48 49 void creatMap()/创建地图 50 51 int i; 52 for (i = 0; i58; i += 2)/打印上下边框 53 54 Pos(i, 0); 55 printf();/一个方块占两个位置 56 Pos(i, 26); 57 printf(); 58 59 for (i = 1; ix = 24; 74 tail-y = 5; 75 tail-next = NULL
5、; 76 for (i = 1; i next = tail; 80 head-x = 24 + 2 * i; 81 head-y = 5; 82 tail = head; 83 84 while (tail != NULL)/从头到为,输出蛇身 85 86 Pos(tail-x, tail-y); 87 printf(); 88 tail = tail-next; 89 90 91 /? 92 int biteSelf()/判断是否咬到了自己 93 94 snake *self; 95 self = head-next; 96 while (self != NULL) 97 98 if (s
6、elf-x = head-x & self-y = head-y) 99 100 return 1;101 102 self = self-next;103 104 return 0;105 106 107 void createFood()/随机出现食物108 109 snake *food_1;110 srand(unsigned)time(NULL);/为了防止每次产生的随机数相同,种子设置为time111 food_1 = (snake*)malloc(sizeof(snake);112 while (food_1-x % 2) != 0) /保证其为偶数,使得食物能与蛇头对其113
7、114 food_1-x = rand() % 52 + 2;115 116 food_1-y = rand() % 24 + 1;117 q = head;118 while (q-next = NULL)119 120 if (q-x = food_1-x & q-y = food_1-y) /判断蛇身是否与食物重合121 122 free(food_1);123 createFood();124 125 q = q-next;126 127 Pos(food_1-x, food_1-y);128 food = food_1;129 printf();130 131 132 void ca
8、ntCrossWall()/不能穿墙133 134 if (head-x = 0 | head-x = 56 | head-y = 0 | head-y = 26)135 136 endGamestatus = 1;137 endGame();138 139 140 141 void snakeMove()/蛇前进,上U,下D,左L,右R142 143 snake * nexthead;144 cantCrossWall();145 146 nexthead = (snake*)malloc(sizeof(snake);147 if (status = U)148 149 nexthead-x
9、 = head-x;150 nexthead-y = head-y - 1;151 if (nexthead-x = food-x & nexthead-y = food-y)/如果下一个有食物/152 153 nexthead-next = head;154 head = nexthead;155 q = head;156 while (q != NULL)157 158 Pos(q-x, q-y);159 printf();160 q = q-next;161 162 score = score + add;163 createFood();164 165 else /如果没有食物/166
10、 167 nexthead-next = head;168 head = nexthead;169 q = head;170 while (q-next-next != NULL)171 172 Pos(q-x, q-y);173 printf();174 q = q-next;175 176 Pos(q-next-x, q-next-y);177 printf( );178 free(q-next);179 q-next = NULL;180 181 182 if (status = D)183 184 nexthead-x = head-x;185 nexthead-y = head-y + 1;186 if (nexthead-x = food-x & nexthead-y = food-y) /有食物187 188 nexthead-next = head;189