2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 贪吃蛇贪吃蛇代码--c语言版 visual c++6.0打开

贪吃蛇贪吃蛇代码--c语言版 visual c++6.0打开

时间:2024-06-14 07:30:26

相关推荐

贪吃蛇贪吃蛇代码--c语言版 visual c++6.0打开

// 贪吃蛇大战2.cpp : Defines the entry point for the console application.//// 贪吃蛇大作战1.cpp : Defines the entry point for the console application.//// 贪吃蛇大作战.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "stdio.h"#include "time.h"//随机数#include "windows.h"//文字颜色、坐标位置#include "stdlib.h"#include "conio.h"//接受键盘的输入输出//宏定义#define U 1#define D 2#define L 3#define R 4//全局变量typedef struct snake{int x;int y;struct snake *next;}snake;int score=0,add=10;int HighScore=0;int status,sleeptime = 200;snake *head,*food,*q;int endgamestatus=0;//游戏结束的状态HANDLE hOut;/*********函数声明********/void gotoxy(int x,int y);//设置光标位置int color(int c);//设置文字颜色void printsnake();//蛇的字符画void welcometogame();//开始界面void createMap();//绘制地图void scoreandtips();//游戏界面右侧的得分和小提示void initsnake();//初始化蛇身,画蛇身void createfood();//创建并随机出现食物int biteself();//判断是否咬到了自己void cantcrosswall();//设置蛇撞墙的情况void speedup();//加速void speeddown();//减速void snakemove();//控制蛇的前进方向void keyboardControl();//控制键盘按键void Lostdraw();//游戏结束界面void endgame();//游戏结束void choose();//游戏失败之后的选择void File_out();//在文件中读取最高分void File_in();//储存最高分的文件void explation();//游戏说明int color(int c)//设置文字颜色{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);return 0;}void gotoxy(int x,int y)//获得控制台的坐标位置{COORD c;c.X=x;c.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);}void printsnake() //绘制字符画 --蛇{system("cls");//清屏gotoxy(35, 1);color(6);printf("/^\\/^\\");//蛇眼睛gotoxy(34, 2);printf("|__| O|");//蛇眼睛gotoxy(33, 2);color(2);printf("_");gotoxy(25, 3);color(12);printf("\\/");//蛇信gotoxy(31, 3);color(2);printf("/");gotoxy(37, 3);color(6);printf(" \\_/");//蛇眼睛gotoxy(41, 3);color(10);printf(" \\");gotoxy(26, 4);color(12);printf("\\____"); //舌头gotoxy(32, 4);printf("_________/");gotoxy(31, 4);color(2);printf("|");gotoxy(43, 4);color(10);printf("\\");gotoxy(32, 5);color(2);printf("\\_______"); //蛇嘴gotoxy(44, 5);color(10);printf("\\");gotoxy(39, 6);printf("|| \\"); //下面都是画蛇身gotoxy(38, 7);printf("// \\");gotoxy(37, 8);printf("//\\ \\");gotoxy(35, 9);printf("// \\ \\");gotoxy(34, 10);printf("//\\ \\");gotoxy(33, 11);printf("// _----_ \\ \\");gotoxy(32, 12);printf("// _-~~-_ | |");gotoxy(31, 13);printf("(( _-~ _--_ ~-__/ |");gotoxy(32, 14);printf("\\~-____-~ _-~ ~-_ ~-_-~ /");gotoxy(33, 15);printf("~-_ _-~~-_ _-~");gotoxy(35, 16);printf("~--______-~~-___-~");}void welcometogame()//欢迎界面{int n;int i,j=1;color(11);gotoxy(43,18);printf("贪 吃 蛇 大 作 战");color(14);for(i=20;i<=26;i++){for(j=27;j<=74;j++){gotoxy(j,i);if(i==20||i==26){printf("-");}else if(j==27||j==74)printf("|");}}color(12);gotoxy(35,22);printf("1.开始游戏");gotoxy(55,22);printf("2.游戏说明");gotoxy(35,24);printf("3.退出游戏");color(3);gotoxy(29,27);printf("请选择[1 2 3]; [ ]\b\b");color(14);scanf("%d",&n);switch(n){case 1:system("cls");createMap();initsnake();createfood();keyboardControl();break;case 2:explation();break;case 3:exit(0);break;}}void createMap()//绘制欢迎地图{int i,j;for(i=0;i<58;i+=2){gotoxy(i,0);color(5);printf("□");gotoxy(i,26);printf("□");}for(i=0;i<26;i++){gotoxy(0,i);printf("□");gotoxy(56,i);printf("□");}for(i=2;i<56;i+=2){for(j=1;j<26;j++){gotoxy(i,j);color(3);printf("■\n\n");}}}void scoreandtips()//绘制游戏右边的东西{File_out();color(11);gotoxy(64,4);printf("☆最高纪录☆:%d分",HighScore);color(14);gotoxy(64,8);printf("得分:%d",score);color(13);gotoxy(73,11);printf("小 提 示");color(6);gotoxy(60,13);printf("╬-------------------------------╬");gotoxy(60,25);printf("╬-------------------------------╬");color(3);gotoxy(64,14);printf("每个食物得分:%d分",add);gotoxy(64,16);printf("不能穿墙,不能咬到自己");gotoxy(64,18);printf("用↑ ↓ ← →分别控制蛇的移动");gotoxy(64,20);printf("F1 为加速,F2 为减速");gotoxy(64,22);printf("space;暂停游戏");gotoxy(64,24);printf("ESC : 退出游戏");}void File_out()//在文件中读取最高分{FILE *fp;fp=fopen("save.txt","a+");fscanf(fp,"%d",&HighScore);fclose(fp);}void initsnake()//绘制蛇身{snake *tail;int i;tail=(snake *)malloc(sizeof(snake));tail->x=24;tail->y=5;for(i=1;i<=4;i++){head=(snake *)malloc(sizeof(snake));head->next=tail;head->x=24+2*i;head->y=5;tail=head;}while(tail!=NULL){gotoxy(tail->x,tail->y);color(14);printf("★");tail=tail->next;}}void createfood()//生成随机食物{snake *food_1;srand((unsigned)time(NULL));food_1=(snake *)malloc(sizeof(snake));srand((unsigned)time(NULL));while((food_1->x%2)!=0){food_1->x=rand()%52+2;}food_1->y=rand()%24+1;q=head;while(q->next== NULL){if((q->x==food_1->x)&&(q->y==food_1->y)){free(food_1);createfood();}q=q->next;}gotoxy(food_1->x,food_1->y);food=food_1;color(12);printf("●");}int biteself()//判断是否撞到了自己{snake *self;self=head->next;while(self!=NULL){if(self->x==head->x&&self->y==head->y){return 1;}self=self->next;}return 0;}void cantcrosswall(){if(head->x==0||head->x==56||head->y==0||head->y==26){//如果蛇头碰到了墙壁endgamestatus=1;//返回第一种情况endgame();//添加新的代码}}void speedup()//蛇加速前进{if(sleeptime>=50){sleeptime-=50;add+=2;}}void speeddown()//蛇减速前进{if(sleeptime<350){sleeptime+=50;add-=2;if(sleeptime==350){add=1;}}}void snakemove(){snake *nexthead;cantcrosswall();nexthead=(snake *)malloc(sizeof(snake));if(status==U){nexthead->x=head->x;nexthead->y=head->y-1;nexthead->next=head;head=nexthead;q=head;if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}gotoxy(q->next->x,q->next->y);color(3);printf("■");free(q->next);q->next=NULL;}}if(status==D){nexthead->x=head->x;nexthead->y=head->y+1;nexthead->next=head;head=nexthead;q=head;if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}gotoxy(q->next->x,q->next->y);color(3);printf("■");free(q->next);q->next=NULL;}}if(status==L){nexthead->x=head->x-2;nexthead->y=head->y;nexthead->next=head;head=nexthead;q=head;if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}gotoxy(q->next->x,q->next->y);color(3);printf("■");free(q->next);q->next=NULL;}}if(status==R){nexthead->x=head->x+2;nexthead->y=head->y;nexthead->next=head;head=nexthead;q=head;if(nexthead->x==food->x&&nexthead->y==food->y){while(q!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}score=score+add;speedup();createfood();}else{while(q->next->next!=NULL){gotoxy(q->x,q->y);color(14);printf("★");q=q->next;}gotoxy(q->next->x,q->next->y);color(3);printf("■");free(q->next);q->next=NULL;}}if(biteself()==1){endgamestatus=2;endgame();}}void keyboardControl(){status=R;while(1){scoreandtips();if(GetAsyncKeyState(VK_UP)&&status!=D){status=U;}else if(GetAsyncKeyState(VK_DOWN)&&status!=U){status=D;}else if(GetAsyncKeyState(VK_LEFT)&&status!=R){status=L;}else if(GetAsyncKeyState(VK_RIGHT)&&status!=L){status=R;}if(GetAsyncKeyState(VK_SPACE)){while(1){Sleep(300);if(GetAsyncKeyState(VK_SPACE)){break;}}}else if(GetAsyncKeyState(VK_ESCAPE)){endgamestatus=3;break;}else if(GetAsyncKeyState(VK_F1)){speedup();}else if(GetAsyncKeyState(VK_F2)){if(sleeptime<350){sleeptime=sleeptime+30;add=add-2;if(sleeptime==350){add=1;}}}Sleep(sleeptime);snakemove();}}void Lostdraw(){system("cls");int i;gotoxy(45,2);color(6);printf("\\\\\\|///");//小人的头发gotoxy(43,3);printf("\\\\");gotoxy(47,3);color(15);printf(".-.-");//眉毛gotoxy(54,3);color(6);printf("//");gotoxy(44,4);color(14);printf("(");//左耳gotoxy(47,4);color(15);printf(".@.@");//眼睛gotoxy(54,4);color(14);printf(")");//右耳gotoxy(17,5);color(11);printf("+-----------------------");//上边框gotoxy(35,5);color(14);printf("oOOo");//左手gotoxy(39,5);color(11);printf("----------");//上边框gotoxy(48,5);color(14);printf("(_)");//嘴gotoxy(51,5);gotoxy(61,5);color(14);printf("oOOo");//右手gotoxy(65,5);color(11);printf("-----------------------+");//上边框for(i=6;i<=19;i++){//竖边框gotoxy(17,i);printf("|");gotoxy(82,i);printf("|");}gotoxy(17,20);printf("+--------------------------------------");//上边框gotoxy(52,20);color(14);printf("☆☆☆”");gotoxy(60,20);color(11);printf("-------------------+");//下边框}void endgame(){system("cls");if(endgamestatus==1){//如果蛇撞到了墙Lostdraw();gotoxy(35,9);color(12);printf("对不起,您撞到墙了。游戏结束!");}else if(endgamestatus==2){//如果蛇咬到了自己Lostdraw();gotoxy(35,9);color(12);printf("对不起,您咬到自己了。游戏结束!");}else if(endgamestatus==3){//如果按ESC键退出Lostdraw();gotoxy(40,9);color(12);printf("您已经结束游戏了!");}gotoxy(43,12);color(13);printf("您的得分是 %d,score");if(score>=HighScore){//如果分数高于最高分color(10);gotoxy(33,16);printf("创纪录啦!最高分被你刷新了,真棒!!!");File_in();//把最高分写入文件}else{//如果分数低于最高分color(10);gotoxy(33,16);printf("继续努力吧 你离最高分还差 %d",HighScore-score);}choose();//边框下面的分支选项}void File_in(){//将最高分存储于文件中FILE *fp;fp=fopen("save.txt","w+");//以读写的方式建立一个名为save.txt的文件fprintf(fp,"%d",score);//把分数写进文件fclose(fp);//关闭文件}void choose(){int n;gotoxy(25,23);color(12);printf("我要重新玩一把 -------- 1");gotoxy(52,23);printf("不玩了,退出吧 -------- 2");gotoxy(46,25);color(11);printf("选择:");scanf("%d",&n);switch(n){case 1:system("cls");score=0;//分数归零sleeptime=200;//设定初始速度add=10;//使add设定未初值,吃一个食物得10分printsnake();//返回欢迎界面welcometogame();break;case 2:exit(0);//退出游戏break;default:gotoxy(35,27);color(12);printf("***您的输入有误,请重新输入***");system("pause>nul");//按任意键endgame();choose();break;}}void explation(){int i,j=1;system("cls");color(13);gotoxy(44,3);printf("游戏说明");color(2);for(i=6;i<=22;i++){for(j=20;j<=75;j++){gotoxy(j,i);if(i==6||i==22)printf("||");if(j==20||j==25) printf("||");}}color(3);gotoxy(30,8);printf("tip1:不能穿墙,不能咬到自己");color(10);gotoxy(30,11);printf("tips2:用↑↓←→分别控制蛇的移动");color(14);gotoxy(30,14);printf("tip3:F1为加速,F2为减速");color(11);gotoxy(30,17);printf("tip4:按空格键暂停游戏,在按空格键继续");color(4);gotoxy(30,20);printf("tip5:ESC:退出游戏。space:暂停游戏");getch();system("cls");printsnake();welcometogame();}int main(){system("mode con cols=100 lines=30");printsnake();welcometogame();File_out();keyboardControl();endgame();return 0;}

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