#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#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 = 10;//总得分与每次吃食物得分。
int status, sleeptime = 200;//每次运行的时间间隔
snake *head, *food;//蛇头指针,食物指针
snake *q;//遍历蛇的时候用到的指针
int endGamestatus = 0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
int fx=10;
int fy=10;
//声明全部函数//
void Pos();
void creatMap();
void initSnake();
int biteSelf();
void createFood();
void cantCrossWall();
void snakeMove();
void pause();
void runGame();
void initGame();
void endGame();
void gameStart();
void Pos(int x, int y)//设置光标位置
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄
SetConsoleCursorPosition(hOutput, pos);
}
void creatMap()//创建地图
{
int i;
for (i = 0; i<58; i += 2)//打印上下边框
{
Pos(i, 0);
printf("*");//一个方块占两个位置
Pos(i, 26);
printf("*");
}
for (i = 1; i<26; i++)//打印左右边框
{
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++)//初始长度为4
{
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));//为了防止每次产生的随机数相同,种子设置为time
food_1 = (snake*)malloc(sizeof(snake));
if (sleeptime >= 50)
{
sleeptime = sleeptime - 5;
//add = add + 2;
if (sleeptime == 320)
{
add = 2;//防止减到1之后再加回来有错
}
}
while(1)
{
//while ((food_1->x % 2) != 0) //保证其为偶数,使得食物能与蛇头对其
//{
food_1->x = (rand()*2) % 52 + 2;
//}
food_1->y = rand() % 24 + 1;
q = head;
int my=0;
while (q->next != NULL)
{
if (q->x == food_1->x && q->y == food_1->y) //判断蛇身是否与食物重合
{
my=1;
break;
// free(food_1);
// createFood();
}
q = q->next;
}
if(my!=1)
break;
}
/*
food_1->x=fx%50+2;
food_1->y=fy%25;
fx+=10;
fy+=5;
*/
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();
}
}
void snakeMove()//蛇前进,上U,下D,左L,右R
{
snake * nexthead;
cantCrossWall();
nexthead = (snake*)malloc(sizeof(snake));
if (status == U)
{
nexthead->x = head->x;
nexthead->y = head->y - 1;
if (nexthead->x == food->x && nexthead->y == food->y)//如果下一个有食物//
{
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
score = score + add;
createFood();
}
else //如果没有食物//
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
Pos(q->next->x, q->next->y);
printf(" ");
free(q->next);
q->next = NULL;
}
}
if (status == D)
{
nexthead->x = head->x;
nexthead->y = head->y + 1;
if (nexthead->x == food->x && nexthead->y == food->y) //有食物
{
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
score = score + add;
createFood();
}
else //没有食物
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
Pos(q->next->x, q->next->y);
printf(" ");
free(q->next);
q->next = NULL;
}
}
if (status == L)
{
nexthead->x = head->x - 2;
nexthead->y = head->y;
if (nexthead->x == food->x && nexthead->y == food->y)//有食物
{
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
score = score + add;
createFood();
}
else //没有食物
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
Pos(q->next->x, q->next->y);
printf(" ");
free(q->next);
q->next = NULL;
}
}
if (status == R)
{
nexthead->x = head->x + 2;
nexthead->y = head->y;
if (nexthead->x == food->x && nexthead->y == food->y)//有食物
{
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
score = score + add;
createFood();
}
else //没有食物
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
Pos(q->x, q->y);
printf("■");
q = q->next;
}
Pos(q->next->x, q->next->y);
printf(" ");
free(q->next);
q->next = NULL;
}
}
if (biteSelf() == 1) //判断是否会咬到自己
{
endGamestatus = 2;
endGame();
}
}
void pause()//暂停
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
vo
评论2
最新资源