#include<graphics.h>
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<Mmsystem.h>
#include<stdlib.h>
#include<conio.h>
#pragma comment(lib,"Winmm.lib")
#define U 1
#define D 2
#define L 3
#define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右
typedef struct snake //蛇身的一个节点
{
int x;
int y;
struct snake *next;
}snake;
int score=0,add,addt[5]={0,5,10,15,20}; //总得分与每次吃食物得分
int HighScore = 0; //最高分
int choice,degree_difficulty=1,game_sound=2; //设置选项,用于游戏设置,degree_difficulty游戏难度默认为“入门”,game_sound游戏音效默认为关闭
int status,sleeptime,sleept[5][2]={{0,0},{350,450},{250,360},{150,260},{50,160}}; //蛇前进状态,每次运行的时间间隔
snake *head, *food; //蛇头指针,食物指针
snake *q; //遍历蛇的时候用到的指针
int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
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(); //游戏说明
void intercalate(); //游戏设置函数
void music(); //播放游戏背景音乐
//设置游戏音乐
void music()
{
mciSendString("open D:\\music2.mp3 alias bkmusic", NULL, 0, NULL);//打开背景音乐
mciSendString("play bkmusic repeat", NULL, 0, NULL);
}
//设置光标位置
void gotoxy(int x,int y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
//文字颜色函数
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return 0;
}
//字符画---蛇
void printsnake()
{
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;
gotoxy(43,18);
color(11);
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.游戏设置");
gotoxy(55,24);
printf("4.退出游戏");
gotoxy(29,27);
color(3);
printf("请选择[1 2 3 4]:[ ]\b\b"); //\b为退格,使得光标处于[]中间
color(14);
scanf("%d", &n); //输入选项
switch(n)
{
case 1:
sleeptime=sleept[degree_difficulty][1];
add=addt[degree_difficulty];
system("cls");
if(game_sound==1) music();
createMap(); //创建地图
initsnake(); //初始化蛇身
createfood(); //创建食物
keyboardControl(); //按键控制
break;
case 2:
explation(); //游戏说明函数
break;
case 3:
intercalate(); //游戏设计函数
break;
case 4:
exit(0); //退出游戏
break;
default: //输入非1~3之间的选项
color(12);
gotoxy(40,28);
printf("请输入1~3之间的数!");
getch(); //输入任意键
system("cls"); //清屏
printsnake();
welcometogame();
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=1;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("■");
}
}
}
//游戏界面右侧的得分和小提示
void scoreandtips()
{
File_out();
gotoxy(64,3);
color(11);
printf("☆游戏难度☆:");
switch(degree_difficulty)
{
case 1:printf("入门");break;
case 2:printf("普通");break;
case 3:printf("较难");break;
case 4:printf("高难");break;
}
gotoxy(64,4);
printf("☆最高记录☆:%d",HighScore);
gotoxy(64,6);
color(12);
printf("☆游戏音效☆:");
if(game_sound==1) printf("开启");
else printf("关闭");
gotoxy(64,8);
color(14);
printf("得分:%d ",score);
color(13);
gotoxy(75,11);
printf("小 提 示");
gotoxy(60,13);
color(6);
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("空格键暂停游戏");
gotoxy(64,24);
printf("Esc键退出游戏");
}
//初始化蛇身,画蛇身
void initsnake()
{
snake *tail;
int i;
tail=(snake*)malloc(sizeof(snake)); //从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x=24; //蛇的初始位置(24,5)
tail->y=5;
tail->next=NULL;
for(i=1;i<=2;i++) //设置蛇身,长度为3
{
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)); //初始化food_1
food_1->x=rand()%52+2;
while((food_1->x%2)!=0) //保证其为偶数,使得食物能与蛇头