C语言贪吃蛇游戏:解决食物消失问题
'#include<stdio.h>\n#include<time.h>\n#include<windows.h>\n#include<stdlib.h>\n#include<conio.h>\n\n#define U 1\n#define D 2\n#define L 3\n#define R 4\ntypedef struct snake\n{\n int x; int y;\n struct snake *next;\n }snake;\n int score=0,add=10;\n int highscore=0;\n int status,sleeptime=200;\n snake *head,*food;\n snake q;\n int endgamestatus=0;\n HANDLE hOut;\n void gotoxy(int x,int y);\n int color(int c);\n void printsnake();\n void wlcome();\n void createmap();\n void scoreandtips();\n void initsnake();\n void createfood();\n int biteself();\n void cantcrosswall();\n void speedup();\n void speeddown();\n void snakemove();\n void keyboardcontrol();\n void lostdraw();\n void endgame();\n void choose();\n void file_out();\n void file_in();\n void explation();\n main()\n {\n system('mode con cols=100 lines=30');\n printsnake();\n wlcome();\n file_out();\n keyboardcontrol();\n endgame();\n }\n void gotoxy(int x,int y)//\n {COORD c;\n c.X=x;\n c.Y=y;\n SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);\n }\n int color(int c)//\n {\n SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);\n return 0;\n }\n void printsnake()//\n {\n gotoxy(2,5);\n color(3);\n printf('姓名:张');\n \n gotoxy(2,6);\n color(3);\n printf('工号:1');\n }\n \n void wlcome()\n {int n;\n int i,j=1;\n gotoxy(43,18);\n color(11);\n printf('贪吃蛇');\n color(14);\n for(i=20;i<=26;i++)\n {\n for(j=27;j<=74;j++)\n {\n gotoxy(j,i);\n if(i==20||i==26)\n {\n printf('-');\n }\n else if(j==27||j==74)\n {\n printf('|');\n }\n }\n }\n color(12);\n gotoxy(35,22);\n printf('1.开始');\n gotoxy(55,22);\n printf('2.说明');\n gotoxy(35,24);\n printf('3.退出');\n gotoxy(29,27);\n color(3);\n printf('请选择1 2 3\n');\n color(14);\n scanf('%d',&n);\n switch(n)\n {\n case 1:\n system('cls');\n createmap();\n initsnake();\n createfood();\n keyboardcontrol();\n break;\n case 2:\n explation();\n break;\n break;\n case 3:\n exit(0);\n break;\n }\n }\nvoid createmap()\n{\n int i,j;\n for(i=0;i<58;i+=2)\n {\n gotoxy(i,0);\n color(5);\n printf('□');\n gotoxy(i,26);\n printf('□');\n }\n for(i=0;i<26;i++)\n {\n gotoxy(0,i);\n printf('□');\n gotoxy(56,i);\n printf('□');\n }\n for(i=2;i<56;i+=2)\n {\n for(j=1;j<26;j++)\n {\n gotoxy(i,j);\n color(3);\n printf('■\n\n');\n }\n \n }\n}\n void scoreandtips()//\n {\n file_out();\n gotoxy(64,4);\n color(11);\n printf('最高纪录: %d',highscore);\n gotoxy(64,8);\n color(14);\n printf('得分: %d ',score);\n color(13);\n gotoxy(73,11);\n printf('小提示');\n gotoxy(60,13);\n color(6);\n printf('+---------------------+');\n gotoxy(60,25);\n printf('+---------------------+');\n color(3);\n gotoxy(64,14);\n printf('每个食物得分:%d分',add);\n gotoxy(64,16);\n printf('不能、咬到自己');\n gotoxy(64,18);\n printf('↑↓←→控制蛇的移动');\n gotoxy(64,20);\n printf('F1加速,F2减速');\n gotoxy(64,22);\n printf('space: 暂停');\n gotoxy(64,24);\n printf('ESC:退出');\n}\nvoid file_out()\n{\n FILE fp;\n fp=fopen('save.txt','a+');\n fscanf(fp,'%d',&highscore);\n fclose(fp);\n }\n void initsnake()\n {\n snake tail;\n int i;\n tail=(snake)malloc(sizeof(snake));\n tail->x=24;\n tail->y=5;\n tail->next=NULL;\n for(i=1;i<=4;i++)\n {\n head=(snake)malloc(sizeof(snake));\n head->next=tail;\n head->x=24+2i;\n head->y=5;\n tail=head;\n }\n while(tail!=NULL)\n {gotoxy(tail->x,tail->y);\n color(14);\n printf('★');\n tail=tail->next;\n }\n}\nvoid createfood()\n{\n snake food_1;\n srand((unsigned)time(NULL));\n int flag = 0;\n while (!flag) {\n food_1 = (snake)malloc(sizeof(snake));\n food_1->x = rand() % 52 + 2;\n food_1->y = rand() % 24 + 1;\n q = head;\n flag = 1;\n while (q != NULL) {\n if (q->x == food_1->x && q->y == food_1->y) {\n flag = 0;\n break;\n }\n q = q->next;\n }\n if (flag) {\n gotoxy(food_1->x, food_1->y);\n food = food_1;\n color(12);\n printf('@');\n } else {\n free(food_1);\n }\n }\n}\nint biteself()\n{\n snake *self;\n self=head->next;\n while(self!=NULL)\n {\n if(self->x==head->x&&self->y==head->y)\n {\n return 1;\n }\n self=self->next;\n }\n return 0;\n }\n \n void cantcrosswall()\n{\n if(head->x==0||head->x==56||head->y==0||head->y==26)\n {\n endgamestatus=1;\n endgame();\n }\n }\n void speedup()\n{\n if(sleeptime>=50)\n {\n sleeptime=sleeptime-10;\n add=add+2;\n }\n}\n \nvoid speeddown()\n{\n if(sleeptime<350)\n {\n sleeptime=sleeptime+30;\n add=add-2;\n if(sleeptime==350)\n {\n add=1;\n }\n }\n}\n \n void snakemove()\n{\n snake nexthead;\n cantcrosswall();\n nexthead=(snake)malloc(sizeof(snake));\n if(status==U)\n {\n nexthead->x=head->x;\n nexthead->y=head->y-1;\n nexthead->next=head;\n head=nexthead;\n q=head;\n if(nexthead->x==food->x&&nexthead->y==food->y)\n {\n while(q!=NULL)\n {\n gotoxy(q->x,q->y);\n color(14);\n printf('★');\n q=q->next;\n }\n score=score+add;\n speedup();\n createfood();\n }\n else\n {\n while(q->next->next!=NULL)\n {\n gotoxy(q->x,q->y);\n color(14);\n printf('★');\n q=q->next;\n }\n gotoxy(q->next->x,q->next->y);\n color(3);\n printf('■');\n free(q->next);\n q->next=NULL;\n }\n }\n // 同理修改D、L、R方向的移动逻辑\n \n // 添加判断条件,当food为NULL时重新生成食物\n if(food == NULL) {\n createfood();\n }\n \n if(biteself()==1)\n {\n endgamestatus=2;\n endgame();\n }\n}\nvoid keyboardcontrol()\n{\n status=R;\n while(1)\n {\n scoreandtips();\n if(GetAsyncKeyState(VK_UP)&&status!=D)\n {\n status=U;\n }\n else if(GetAsyncKeyState(VK_DOWN)&&status!=U)\n {\n status=D;\n }\n else if(GetAsyncKeyState(VK_LEFT)&&status!=R)\n {\n status=L;\n }\n else if(GetAsyncKeyState(VK_RIGHT)&&status!=L)\n {\n status=R;\n }\n if(GetAsyncKeyState(VK_SPACE))\n {\n while(1)\n {\n Sleep(300);\n if(GetAsyncKeyState(VK_SPACE))\n {\n break;\n }\n }\n }\n else if(GetAsyncKeyState(VK_ESCAPE))\n {\n endgamestatus=3;\n break;\n }\n else if(GetAsyncKeyState(VK_F1))\n {\n speedup();\n }\n else if(GetAsyncKeyState(VK_F2))\n {\n if(sleeptime<350)\n {\n sleeptime=sleeptime+30;\n add=add-2;\n if(sleeptime==350)\n {\n add=1;\n }\n }\n }\n Sleep(sleeptime);\n snakemove();\n }\n}\n \nvoid lostdraw()\n{\n system('cls');\n int i,j;\n gotoxy(17,5);\n color(11);\n printf('+------------------------');\n \n gotoxy(35,5);\n color(14);\n printf('o00o');\n \n gotoxy(39,5);\n color(11);\n printf('----------');\n \n gotoxy(48,5);\n color(14);\n printf('---');\n \n gotoxy(51,5);\n color(11);\n printf('----------');\n \n gotoxy(61,5);\n color(14);\n printf('o00o');\n \n gotoxy(65,5);\n color(11);\n printf('-----------------+');\n \n for(i=6;i<=19;i++)\n {\n gotoxy(17,i);\n printf('|');\n gotoxy(82,i);\n printf('|');\n }\n gotoxy(17,20);\n printf('+----------------------------------');\n \n gotoxy(52,20);\n color(11);\n printf('-----------------------------+');\n }\n \n void endgame()\n {\n system('cls');\n if(endgamestatus==1)\n {\n lostdraw();\n gotoxy(35,9);\n color(12);\n printf('撞墙。游戏结束!');\n }\n else if(endgamestatus==2)\n {\n lostdraw();\n gotoxy(35,9);\n color(12);\n printf('对不起,您咬到自己了。游戏结束!');\n }\n else if(endgamestatus==3)\n {\n lostdraw();\n gotoxy(40,9);\n color(12);\n printf('您结束了游戏。');\n }\n gotoxy(43,12);\n color(13);\n printf('您的得分是 %d',score);\n if(score>=highscore)\n {\n color(10);\n gotoxy(33,16);\n printf('创新纪录啦!你真棒!!!');\n file_in();\n }\n choose();\n }\n void file_in()\n {\n FILE *fp;\n fp=fopen('save.txt','w+');\n fprintf(fp,'%d',score);\n fclose(fp);\n }\n void choose()\n {\n int n;\n gotoxy(25,23);\n color(12);\n printf('重玩一局-------1');\n gotoxy(52,23);\n printf('不玩了,退出-------2');\n gotoxy(46,25);\n color(11);\n printf('选择:');\n scanf('%d',&n);\n switch(n)\n {\n case 1:\n system('cls');\n score=0;\n sleeptime=200;\n add=10;\n printsnake();\n wlcome();\n break;\n case 2:\n exit(0);\n break;\n default:\n gotoxy(35,27);\n color(12);\n printf('输入有误,请重输');\n system('pause >nul');\n endgame();\n choose();\n break;\n }\n }\n void explation()\n {\n int i,j=1;\n system('cls');\n color(13);\n gotoxy(44,3);\n printf('游戏说明');\n color(2);\n for(i=6;i<=22;i++)\n {\n for(j=20;j<=75;j++)\n {\n gotoxy(j,i);\n if(i==6||i==22)printf('=');\n else if(j==20||j==75)printf('||');\n }\n }\n color(3);\n gotoxy(30,8);\n printf('1、不能穿墙、咬到自己');\n color(3);\n gotoxy(30,8);\n printf('1、不能穿墙、咬到自己');\n color(10);\n gotoxy(30,11);\n printf('2、用↑↓←→控制蛇移动');\n color(14);\n gotoxy(30,14);\n printf('3、F1加速,F2减速');\n color(11);\n gotoxy(30,17);\n printf('4、按空格暂停,再按继续');\n color(4);\n gotoxy(30,20);\n printf('5、ESC:退出,space:暂停');\n getch();\n system('cls');\n printsnake();\n wlcome();\n }\n\n在贪吃蛇程序中,如果没有食物,可以在snakemove()函数中添加一个判断条件,当food为NULL时,表示没有食物存在,可以重新生成一个食物。具体修改如下:\n\nc\nvoid snakemove()\n{\n snake *nexthead;\n cantcrosswall();\n nexthead=(snake*)malloc(sizeof(snake));\n if(status==U)\n {\n nexthead->x=head->x;\n nexthead->y=head->y-1;\n nexthead->next=head;\n head=nexthead;\n q=head;\n if(nexthead->x==food->x&&nexthead->y==food->y)\n {\n while(q!=NULL)\n {\n gotoxy(q->x,q->y);\n color(14);\n printf(\'★\');\n q=q->next;\n }\n score=score+add;\n speedup();\n createfood();\n }\n else\n {\n while(q->next->next!=NULL)\n {\n gotoxy(q->x,q->y);\n color(14);\n printf(\'★\');\n q=q->next;\n }\n gotoxy(q->next->x,q->next->y);\n color(3);\n printf(\'■\');\n free(q->next);\n q->next=NULL;\n }\n }\n // 同理修改D、L、R方向的移动逻辑\n\n // 添加判断条件,当food为NULL时重新生成食物\n if(food == NULL) {\n createfood();\n }\n\n if(biteself()==1)\n {\n endgamestatus=2;\n endgame();\n }\n}\n\n\n这样就可以解决没有食物的情况。当food为空时,会重新调用createfood()函数生成一个新的食物。\n
原文地址: https://www.cveoy.top/t/topic/p5dS 著作权归作者所有。请勿转载和采集!