irpas技术客

贪吃蛇_EAGLE--

未知 7934

#include<time.h>#include<conio.h>#include<stdlib.h>#include<Windows.h>using namespace std;int speed,count;typedef struct _SNAKE{ int x; int y; struct _SNAKE *next;}Snake;Snake *s_Head = NULL;typedef struct _FOOD{ int x; int y;}food;food _food = NULL;enum{up=72,down=80,left=75,right=77};void BeginGame();//开始游戏 void RunGame();//运行游戏 void Welcomeplayer();//欢迎玩家 void setCursor(int x,int y);//设置光标 void color(int m);//设置颜色 void buildmap();//建立地图void setSnake();//生成一条蛇 void MoveSnake(int direction);//移动蛇 void ProduceFood();//随机生成食物void extend(int direction);//伸长蛇身void deadPrint();//死亡画面 int dead();//碰撞死亡 int check();//判断是否吃到食物 int main(){ BeginGame(); return 0;}void BeginGame(){ system(“c:\windows\system32\mode.com con cols=120 lines=50”); Welcomeplayer();}void Welcomeplayer(){ while(1) { setCursor(39,15); printf("\t 欢迎来到贪吃蛇的世界\n"); setCursor(39,17); puts("\t在这里你将体会到一种别样的贪吃蛇\n"); puts("\n\n\n\n\n\n\n\n\n\n"); puts(“按任意键继续”); puts(“按Esc退出”); char ch=getch(); if(ch==27)return; system(“cls”); RunGame(); }}void buildmap(){ for(int i=0;i<=60;i+=2) { setCursor(i,0); printf(“■”); setCursor(i,1); printf(“■”); setCursor(i,29); printf(“■”); setCursor(i,30); printf(“■”); } for(int i=0;i<=30;i++) { setCursor(0,i); printf(“■”); setCursor(2,i); printf(“■”); setCursor(60,i); printf(“■”); setCursor(62,i); printf(“■”); }}void setSnake(){ Snake temp = (Snake)malloc(sizeof(Snake)); temp->x=20; temp->y=15; temp->next=NULL; s_Head=temp; for(int i=1;i<=3;i++) { Snake p=(Snake)malloc(sizeof(Snake)); p->x=20+2i; p->y=15; p->next=NULL; temp->next=p; temp=p; } temp=s_Head; while(temp!=NULL) { setCursor(temp->x,temp->y); printf(“■”); temp=temp->next; }}void RunGame(){ loop: setCursor(33,15); puts("\t\t 请选择游戏难度\n"); puts("\t\t\t\t\t\t 1.简单\n"); puts("\t\t\t\t\t\t 2.一般\n"); puts("\t\t\t\t\t\t 3.困难\n"); puts("\t\t\t\t\t\t 4.特别困难"); char ch=getch(); while(ch!=‘1’&&ch!=‘2’&&ch!=‘3’&&ch!=‘4’) { ch=getch(); } speed=(5-(ch-‘0’))*90; system(“cls”); buildmap(); setSnake(); int direction=right; ProduceFood(); int res=0; res= res>count?res:count; setCursor(80,15); printf(“历史最高分:\n”); setCursor(89,16); printf("%d",res); setCursor(80,20); count=0; printf(“得分:\n”); while(1) { setCursor(83,21); printf("%d",count); char ch; if(kbhit()) { ch=getch(); switch(ch) { case 72://上 if(fabs(ch-direction)!=8) direction=up; break; case 75://左 if(fabs(ch-direction)!=2) direction=left; break; case 77://右 if(fabs(ch-direction)!=2) direction=right; break; case 80://下 if(fabs(ch-direction)!=8) direction=down; break; } } MoveSnake(direction); if(check()) { extend(direction); count++; ProduceFood(); } if(dead()1) { system(“cls”); goto loop; } else if(dead()0) { system(“cls”); setCursor(55,20); printf(“游戏结束”); puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); system(“pause”); system(“cls”); return; } Sleep(speed); } }int check(){ Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if(p->x_food->x&&p->y_food->y) return 1; return 0;} void extend(int direction){ Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if(p->x==_food->x&&p->y==_food->y) { Snake *temp=(Snake *)malloc(sizeof(Snake)); temp->next = NULL; if(direction == right) { temp->x = p->x + 2; temp->y = p->y; } if(direction == left) { temp->x = p->x - 2; temp->y = p->y; } if(direction == up) { temp->x = p->x; temp->y = p->y - 1; } if(direction == down) { temp->x = p->x; temp->y = p->y + 1; } p->next = temp; setCursor(temp->x,temp->y); printf(“■”); }}void MoveSnake(int direction){ //找到链尾 (即蛇头) Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } //增尾 Snake temp = (Snake)malloc(sizeof(Snake)); temp->next = NULL; if(direction == right) { temp->x = p->x + 2; temp->y = p->y; } if(direction == left) { temp->x = p->x - 2; temp->y = p->y; } if(direction == up) { temp->x = p->x; temp->y = p->y - 1; } if(direction == down) { temp->x = p->x; temp->y = p->y + 1; } p->next = temp; //打印 setCursor(temp->x,temp->y); printf(“■”); setCursor(s_Head->x,s_Head->y); printf(" "); //删头 temp=s_Head->next; delete s_Head; s_Head=temp; }void ProduceFood(){ srand(time(NULL)); food p=(food)malloc(sizeof(food)); while(1) { p->x=rand()%60; p->y=rand()%29; if(p->x > 4&&p->x<57&&p->y > 4&&p->y< 30&&p->x%20&&p->y%20) break; } _food=p; setCursor(p->x,p->y); color(4); printf(“●”); color(7);}int dead(){ Snake *temp=s_Head->next; while(temp!=NULL) { if(temp->xs_Head->x && temp->ys_Head->y) { deadPrint(); char ch=getch(); while(ch!=‘Y’&&ch!=‘y’&&ch!=‘N’&&ch!=‘n’) { ch=getch(); } if(ch==‘Y’||ch==‘y’)return 1; if(ch==‘N’||ch==‘n’)return 0; } temp=temp->next; } Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if((p->x == 60||p->x2)||(p->y1||p->y29)){ deadPrint(); char ch=getch(); while(ch!=‘Y’&&ch!=‘y’&&ch!=‘N’&&ch!=‘n’) { ch=getch(); } if(ch’Y’||ch==‘y’)return 1; if(ch==‘N’||ch==‘n’)return 0; }} void deadPrint(){ system(“cls”); setCursor(54,15); printf(“你已经死亡”); setCursor(54,17); puts(“是否重新开始”); setCursor(45,19); puts(“是Y\t\t\t否N “); puts(”\n\n\n\n\n\n\n”);}void color(int m){ if(m>=0 && m<=15)//参数在0-15的范围颜色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), m); //只有一个参数,改变字体颜色 else//默认的颜色白色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);}void setCursor(int x,int y){ COORD gotoxy{x,y}; HANDLE hand=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hand,gotoxy);}


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #贪吃蛇 #namespace #stdint #speed #counttypedef #struct #_SNAKEint