失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > C语言命令行小游戏之贪吃蛇

C语言命令行小游戏之贪吃蛇

时间:2019-03-17 21:59:21

相关推荐

C语言命令行小游戏之贪吃蛇

这是一个通过链表实现的命令行小游戏——贪吃蛇。

#include <stdio.h>#include <stdlib.h>#include <time.h>//声明储存蛇坐标的链表struct Snake{int position[2];struct Snake * next;};//游戏地图char Game[12][36];//记录玩家的默认坐标int PlayerPosition[2]={5,16};//玩家的上一个坐标int BeforePosition[2];//食物坐标int FoodPosition[2];//蛇头的头节点struct Snake *Headf = NULL;//蛇的长度,默认为1int SnakeLength=1;//分数int Score = 0;//游戏是否结束的变量bool IsOver = false;//随机生成食物void Food(){//随机生成食物的坐标srand((unsigned int)time(NULL));// % (n - m + 1) + m;//生成m~n的随机数FoodPosition[1] = rand()% 34 + 1;FoodPosition[0] = rand()% 10 + 1;//将食物在地图中生成Game[FoodPosition[0]][FoodPosition[1]] = '$';}//创建蛇身体,通过链表进行链接每一块身体struct Snake *CreatBody(struct Snake *Head,int position[2]){struct Snake *head;struct Snake *p1,*p2;head = Head;p1 = (struct Snake *)malloc(sizeof(struct Snake));p1->position[0] = position[0];p1->position[1]=position[1];p1->next = NULL;//判断头部是否创建,创建则生成身体if(head==NULL){head = p1;}else{p2 = head;while(p2->next!=NULL){p2 = p2->next;}p2->next = p1;// printf("Body Created 1st\n");}return head;}//绘出游戏界面void DrawUI(struct Snake *Head){struct Snake *p1=Head;printf("Score:%d\n",Score);p1 = Head;//先确定蛇的长度while (p1->next!=NULL){ p1 = p1->next;SnakeLength+=1;// printf("Got SnakeLength!%d\n",SnakeLength);}p1 = Head;//根据蛇的长度,读取链表,将蛇添加在游戏界面里for(int i = 0; i < SnakeLength; i++){Game[p1->position[0]][p1->position[1]]='+';p1 = p1->next;// printf("Draw Snake!\n");}SnakeLength =1 ;//画出游戏地图for (int i = 0; i < 12; i++){for(int j = 0; j < 36; j++){printf("%c",Game[i][j]);}printf("\n");}}//蛇的移动逻辑void Move(struct Snake *Head){struct Snake *p1,*p2;p1 = Head;p2 = Head;//方向char Direction;int pos[2];int BeforePos[2];BeforePos[0] = p1->position[0];BeforePos[1] = p1->position[1];Direction = getchar();if(BeforePos[0]== 0|| BeforePos[0] ==11 || BeforePos[1]==0|| BeforePos[1]==35){IsOver = true;}if(Direction == 'w'){if(Score == 0){p1->position[0] -= 1;Game[BeforePos[0]][BeforePos[1]]=' ';}else{p1 = Head;p2 = (struct Snake*)malloc(sizeof(struct Snake));p2->position[0]=p1->position[0];p2->position[1]=p1->position[1];struct Snake *p3;p3 = p1->next;p1->next = p2;p2 ->next = p3;p1 = Head;while (p1->next != NULL){p2 = p1;p1 = p1->next;}Game[p1->position[0]][p1->position[1]]=' ';BeforePosition[0]=p1->position[0];BeforePosition[1]=p1->position[1];p2->next = NULL;free(p1);p1 = Head;p1->position[0] -= 1;}}if(Direction == 's'){if(Score == 0){p1 = Head;p1->position[0] += 1;Game[BeforePos[0]][BeforePos[1]]=' ';}else{p1 = Head;p2 = (struct Snake*)malloc(sizeof(struct Snake));p2->position[0]=p1->position[0];p2->position[1]=p1->position[1];struct Snake *p3;p3 = p1->next;p1->next = p2;p2 ->next = p3;p1 = Head;while (p1->next != NULL){p2 = p1;p1 = p1->next;}BeforePosition[0]=p1->position[0];BeforePosition[1]=p1->position[1];Game[p1->position[0]][p1->position[1]]=' ';p2->next = NULL;free(p1);p1 = Head;p1->position[0] += 1;}}if(Direction == 'a'){if(Score == 0){p1 = Head;p1->position[1] -= 1;Game[BeforePos[0]][BeforePos[1]]=' ';}else{p1 = Head;p2 = (struct Snake*)malloc(sizeof(struct Snake));p2->position[0]=p1->position[0];p2->position[1]=p1->position[1];struct Snake *p3;p3 = p1->next;p1->next = p2;p2 ->next = p3;p1 = Head;while (p1->next != NULL){p2 = p1;p1 = p1->next;// Game[p1->position[0]][p1->position[1]]=' ';}BeforePosition[0]=p1->position[0];BeforePosition[1]=p1->position[1];Game[p1->position[0]][p1->position[1]]=' ';p2->next = NULL;free(p1);p1 = Head;p1->position[1] -= 1;}}if(Direction == 'd'){if(Score == 0){p1 = Head;p1->position[1] += 1;Game[BeforePos[0]][BeforePos[1]]=' ';}else{p1 = Head;p2 = (struct Snake*)malloc(sizeof(struct Snake));p2->position[0]=p1->position[0];p2->position[1]=p1->position[1];struct Snake *p3;p3 = p1->next;p1->next = p2;p2 ->next = p3;p1 = Head;while (p1->next != NULL){p2 = p1;p1 = p1->next;// Game[p1->position[0]][p1->position[1]]=' ';}BeforePosition[0]=p1->position[0];BeforePosition[1]=p1->position[1];Game[p1->position[0]][p1->position[1]]=' ';p2->next = NULL;free(p1);p1 = Head;p1->position[1] += 1;}} }int main(){struct Snake *head;head = CreatBody(Headf,PlayerPosition);for (int i = 0; i < 12; i++){for(int j = 0; j < 36; j++){if(i==0 || j==0 || j==35 || i==11)Game[i][j] = 'x';else Game[i][j]=' ';}}Food();while (!IsOver){struct Snake *p1 = head;DrawUI(head);Move(head);if(p1->position[0] == FoodPosition[0]&& p1->position[1] == FoodPosition[1]){//Game[BeforePosition[0]][BeforePosition[1]] = '*';Score+=1;Food();head = CreatBody(head,BeforePosition);}}printf("Game Over!\n");}

如果觉得《C语言命令行小游戏之贪吃蛇》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。