#include "stdio.h"
#include "windows.h"
#include "time.h" //随机函数
#include "stdlib.h"
#include "conio.h"
/*打开文件 fpoen() 读取数据 fscanf() 关闭文件 fclose()*/
//宏定义
#define U 1
#define D 2
#define L 3
#define R 4
//全局变量
//链表
typedef struct snake //蛇身的一个节点
{
int x; //节点的x坐标
int y; //节点的y坐标
struct snake *next; //蛇身的下一个节点
}snake;
int score = 0,add = 10;
int HighScore = 0;
int status, sleeptime = 200;
snake * head, *food;
snake *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(); //创建并初始化食物
void biteself(); //判断是否咬到自己
void cantcrosswall(); //设置蛇撞墙的情况
void speedup(); //加速
void speeddown(); //减速
void snakemove(); //控制蛇前进方向
void keyboardControl(); //控制键盘按键
void Lostdtaw(); //游戏结束界面
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()
{
color(6); //土黄色
gotoxy(35,1);//坐标位置
printf(" /^\\/^\\");
gotoxy(34,2);
printf(" _|__| O|");
gotoxy(33, 3);
printf("\\/ /~ \\_/ \\");
gotoxy(32, 4);
printf(" \\____|__________/ \\");
gotoxy(31,5);
printf(" \\_______ \\");
gotoxy(30, 6);
printf(" `\\ \\ \\");
gotoxy(29, 7);
printf(" | | \\");
gotoxy(28, 8);
printf(" / / \\");
gotoxy(27,9);
printf(" / / \\\\");
gotoxy(26, 10);
printf(" / / \\ \\");
gotoxy(25, 11);
printf(" / / \\ \\");
gotoxy(24, 12);
printf(" / / _----_ \\ \\");
gotoxy(23, 13);
printf(" / / _-~ ~-_ | |");
gotoxy(22, 14);
printf(" ( ( _-~ _--_ ~-_ _/ |");
gotoxy(21, 15);
printf(" \\ ~-____-~ _-~ ~-_ ~-_-~ /");
gotoxy(20, 16);
printf(" ~-_ _-~ ~-_ _-~ ");
gotoxy(19, 17);
printf(" ~--______-~ ~-___-~\n");
}
/*欢迎界面*/
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(i,j);
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_s("%d",&n);
switch (n)
{
case 1:
system("cls");
createMap();
initsnake();
//createfood();
break;
case 2:
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 = 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("■\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(63, 13);
printf("╫-------------------------------╫");
gotoxy(60,25);
printf("╫-------------------------------╫");
color(3);
gotoxy(64,14);
printf("每个食物得分: %d分",add);
gotoxy(64, 16);
printf("不能穿墙咬到自己");
gotoxy(64, 18);
printf("用↑ ↓ ← → 分别控制蛇的移动");
gotoxy(64, 22);
printf("space : 游戏暂停");
gotoxy(64, 24);
printf("ESC : 退出游戏");
}
/*在文件中读取最高分*/
void File_out()
{
FILE *fp;
fp = fopen("save.txt","a+"); //fopen()中第二个参数 r 只读 r+ 读写 w 只写
fscanf(fp,"%d",&HighScore);
fclose(fp);
}
/*绘制蛇身*/
void initsnake()
{
snake *tail;
int i;
tail = (snake*)malloc(sizeof(snake));//malloc 初始化指针,开辟指针空间
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;
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 main()
{
system("mode con cols=100 lines=30");//讲控制台设置100*30的宽高
printsnake();
welcometogame();
File_out();
return 0;
}