c语言五子棋代码_基于控制台的C语言贪吃蛇
生活随笔
收集整理的這篇文章主要介紹了
c语言五子棋代码_基于控制台的C语言贪吃蛇
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
相信對很多人來說,學完C語言以后,都會找一些小程序來練練手。例如貪吃蛇、五子棋、俄羅斯方塊等等。
今天給大家分享一個基于控制臺的C語言貪吃蛇小程序。
基礎知識要求:C語言基礎。
知識點補充
這里寫一些你可能沒學到的知識點
- system("cls"):清屏函數
- Sleep(300):等待函數,其中括號內的為毫秒。
- _getch():獲取一個鍵盤字符,不顯示該字符就返回
- kbhit():判斷是否有鍵按下,是返回1,否返回0.
代碼部分
廢話不多說,直接貼代碼
#include #include #include #include #include #include struct node{int x;int y;struct node *next;};node *init(int length);//初始化蛇void print(int a[15][30],node *head);//輸出函數void food(node *head);//食物產生函數void move(node *head);//移動函數bool dead(node *head);//判斷死亡函數int food_x=0,food_y=0;int food_flag = 0;int target = 2;void main(){int a[15][30];node *head;head = init(2);while(1){system("cls");move(head);if(dead(head))break;print(a,head);Sleep(300);}system("cls");printf("Game Over!!!");}node *init(int length){int i = 2;node *head;head = (node*)malloc(sizeof(node));//分配地址node *p = head;head->x = 1;head->y = 1;while(i <= length){node *s;s = (node*)malloc(sizeof(node));s->x = 1;s->y = i;s->next = NULL;p->next = s;p = p->next;i++;}food(head);return head;}void print(int a[15][30],node *head){//0為空格,-1為墻壁#,1為蛇身*,2為蛇頭@,3為食物$int i,j;node *p = head;for(i = 0; i < 15; i++){for(j = 0; j < 30; j++){if(i == 0 || j == 0 || i == 14 || j == 29)a[i][j] = -1;elsea[i][j] = 0;}}while(p != NULL){if(p->next == NULL)a[p->x][p->y] = 2;elsea[p->x][p->y] = 1;p = p->next;}a[food_x][food_y] = 3;for(i = 0; i < 15; i++){for(j = 0; j < 30; j++){if(a[i][j] == -1)printf("#");if(a[i][j] == 0)printf(" ");if(a[i][j] == 1)printf("*");if(a[i][j] == 2)printf("@");if(a[i][j] == 3)printf("$");}printf("");}}void food(node *head){node *p = head;srand((unsigned)time(NULL));food_x = rand()%12 + 1;food_y = rand()%27 + 1;while(p != NULL){if(p->x == food_x && p->y == food_y){food_x = rand()%12 + 1;food_y = rand()%27 + 1;p = head;}p = p->next;}}void move(node *head){char ch;if(kbhit()){ch = _getch();//VS中getch()用_getch()代替。switch (ch){case -32: //上下左右占兩個字節,低八位存ASCII碼,高八位存按鍵掃描碼ch = _getch(); //其中-32為我的電腦的按鍵掃描碼switch (ch){case 72:if(target != 1)target = 0; break;//72為上的ASCII碼case 80:if(target != 0)target = 1; break;//80為下的ASCII碼case 77:if(target != 3)target = 2; break;//77為右的ASCII碼case 75:if(target != 2)target = 3; break;//75位左的ASCII碼default:break;}}}node *p;node *q = head->next;while(q->next != NULL)q = q->next;switch(target){case 0:if(q->x - 1 == food_x && q->y == food_y)food_flag = 1;break;case 1:if(q->x + 1 == food_x && q->y == food_y)food_flag = 1;break;case 2:if(q->x == food_x && q->y + 1 == food_y)food_flag = 1;break;case 3:if(q->x == food_x && q->y - 1 == food_y)food_flag = 1;break;}if(food_flag == 1){node *s;s = (node*)malloc(sizeof(node));s->x = food_x;s->y = food_y;s->next = NULL;q->next = s;food(head);food_flag = 0;}else{p = head;q = head->next;while(q != NULL){p->x = q->x;p->y = q->y;if(q->next == NULL){switch(target){case 0:q->x = q->x - 1;break;case 1:q->x = q->x + 1;break;case 2:q->y = q->y + 1;break;case 3:q->y = q->y - 1;break;}}p = p->next;q = q->next;}}}bool dead(node *head){node *p = head;while(p->next != NULL)p = p->next;if(p->x >= 14 || p->x <= 0 || p->y >= 29 || p->y <= 0)return true;node *q = head;while(q->next != NULL){if(q->x == p->x && q->y == p->y)return true;q = q->next;}return false;}以上就是全部內容。
下次再見!!!
總結
以上是生活随笔為你收集整理的c语言五子棋代码_基于控制台的C语言贪吃蛇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用Python分析《庆余年》人物图谱和
- 下一篇: 怎么用手机看wmv格式