void Pos(int x, int y) { //设置光标位置
COORD pos; //COORD改变光标位置的函数?
HANDLE hOutput; //https://blog.csdn.net/qq_38241045/article/details/69941464
pos.X = x; //x改变的距离()??
pos.Y = y; //y改变的距离()??
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}void creatMap() { //创建地图
int i;
for (i = 0; i<58; i += 2) { //打印上下边框 //为什么i+=2?? 测试规律位移+=i,i-1的位置
Pos(i, 0);
printf("■");
Pos(i, 26);
printf("■");
}
for (i = 1; i<26; i++) { //打印左右边框 //为什么x,y打印的不一样啊??????????
Pos(0, i);
printf("■");
Pos(56, i);
printf("■");
}
}void initsnake() { //初始化蛇身
snake *tail;
int i;
tail = (snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x = 24;
tail->y = 5;
tail->next = NULL;
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) { //从头到为,输出蛇身
Pos(tail->x, tail->y);
printf("■");
tail = tail->next;
}
}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 createfood() { //随机出现食物
snake *food_1;
srand((unsigned)time(NULL));
food_1 = (snake*)malloc(sizeof(snake));
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;
}
Pos(food_1->x, food_1->y);
food = food_1;
printf("■");
}void cantcrosswall() { //不能穿墙
if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26) {
endgamestatus = 1;
endgame();
}
COORD pos; //COORD改变光标位置的函数?
HANDLE hOutput; //https://blog.csdn.net/qq_38241045/article/details/69941464
pos.X = x; //x改变的距离()??
pos.Y = y; //y改变的距离()??
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}void creatMap() { //创建地图
int i;
for (i = 0; i<58; i += 2) { //打印上下边框 //为什么i+=2?? 测试规律位移+=i,i-1的位置
Pos(i, 0);
printf("■");
Pos(i, 26);
printf("■");
}
for (i = 1; i<26; i++) { //打印左右边框 //为什么x,y打印的不一样啊??????????
Pos(0, i);
printf("■");
Pos(56, i);
printf("■");
}
}void initsnake() { //初始化蛇身
snake *tail;
int i;
tail = (snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x = 24;
tail->y = 5;
tail->next = NULL;
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) { //从头到为,输出蛇身
Pos(tail->x, tail->y);
printf("■");
tail = tail->next;
}
}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 createfood() { //随机出现食物
snake *food_1;
srand((unsigned)time(NULL));
food_1 = (snake*)malloc(sizeof(snake));
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;
}
Pos(food_1->x, food_1->y);
food = food_1;
printf("■");
}void cantcrosswall() { //不能穿墙
if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26) {
endgamestatus = 1;
endgame();
}