貪吃蛇(C語言版)鏈表實現
貪吃蛇
gitee:貪吃蛇C語言版: Snake
蛇的結構
typedef struct Snake
{
int x;
int y;
struct Snake *next;
};
遊戲開始歡迎介面
//遊戲開始歡迎介面
void meun()
{
printf(" \n");
printf(" __________ ___ \n");
printf(" / \\ / \\ \\ |____ __\\__ \n");
printf(" / ________ \\ / ___ \\ _/ __ | | / \n");
printf(" | | |__| _/_ |_| / [|] |/ \n");
printf(" | | | | | / _|_ \\__/ \n");
printf(" \\ \\_______ / \\ |___/ ____ \n");
printf(" \\ \\ ____ ____ ____ __ | | ___ ______ \n");
printf(" \\_______ \\ | |/ \\ / \\_/ / | | / / / \\ \n");
printf(" \\ \\ | ___ \\ / ____ / | |/ / / ____ \\ \n");
printf(" __ | | | / \\ \\ | | | / | / | /____\\ | \n");
printf(" \\ \\_______| | | | | | | |__| | | \\ | ________/ \n");
printf(" \\ / | | | | \\ \\ | |\\ \\ \\ \\____ \n");
printf(" \\__________/ |__| |__| \\___/\\__\\ |__| \\__\\ \\______/ \n");
printf("按回車鍵開始遊戲");
}
初始化蛇身
//初始化蛇身
void init()
{
pSnake tmp = (pSnake)malloc(sizeof(Snake *));
tmp->next = NULL;
head = tmp;
tmp->x = 30;
tmp->y = 10;
for (int i = 1; i < size; i++)
{
pSnake temp = (pSnake)malloc(sizeof(Snake *));
temp->next = NULL;
tmp->next = temp;
temp->x = tmp->x + 2;
temp->y = tmp->y;
tmp = tmp->next;
}
}
遊戲開始歡迎介面
//遊戲開始歡迎介面
void meun()
{
printf(" \n");
printf(" __________ ___ \n");
printf(" / \\ / \\ \\ |____ __\\__ \n");
printf(" / ________ \\ / ___ \\ _/ __ | | / \n");
printf(" | | |__| _/_ |_| / [|] |/ \n");
printf(" | | | | | / _|_ \\__/ \n");
printf(" \\ \\_______ / \\ |___/ ____ \n");
printf(" \\ \\ ____ ____ ____ __ | | ___ ______ \n");
printf(" \\_______ \\ | |/ \\ / \\_/ / | | / / / \\ \n");
printf(" \\ \\ | ___ \\ / ____ / | |/ / / ____ \\ \n");
printf(" __ | | | / \\ \\ | | | / | / | /____\\ | \n");
printf(" \\ \\_______| | | | | | | |__| | | \\ | ________/ \n");
printf(" \\ / | | | | \\ \\ | |\\ \\ \\ \\____ \n");
printf(" \\__________/ |__| |__| \\___/\\__\\ |__| \\__\\ \\______/ \n");
printf("按回車鍵開始遊戲");
}
畫牆
//畫牆
void printWall()
{
for (int i = 0; i <= 80; i += 2)
{
pos(i, 0);
printf("■");
pos(i, 26);
printf("■");
}
for (int i = 0; i <= 26; i++)
{
pos(0, i);
printf("■");
pos(80, i);
printf("■");
}
}
初始化蛇身
//初始化蛇身
void init()
{
pSnake tmp = (pSnake)malloc(sizeof(Snake *));
tmp->next = NULL;
head = tmp;
tmp->x = 30;
tmp->y = 10;
for (int i = 1; i < size; i++)
{
pSnake temp = (pSnake)malloc(sizeof(Snake *));
temp->next = NULL;
tmp->next = temp;
temp->x = tmp->x + 2;
temp->y = tmp->y;
tmp = tmp->next;
}
}
隨機函數
//隨機函數
void random(int *x, int *y)
{
srand((unsigned)time(NULL));
// 2~78 1~39
int a = rand() % 39 + 1;
int b = rand() % 25 + 1;
*x = a;
*y = b;
}
坐標函數
//坐標函數
void pos(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
列印蛇身
//列印蛇身
void print()
{
pSnake tmp = head;
while (tmp != NULL)
{
pos(tmp->x, tmp->y);
printf("■");
tmp = tmp->next;
}
}
蛇動
void move()
{
if (flag == 77 || flag == 72 || flag == 80 || flag == 75)
{
}
else
{
return;
}
//增加頭
pSnake temp = (pSnake)malloc(sizeof(pSnake));
temp->next = head;
head = temp;
if (flag == 77) //右
{
temp->x = temp->next->x + 2;
temp->y = temp->next->y;
}
else if (flag == 72) //上
{
temp->x = temp->next->x;
temp->y = temp->next->y - 1;
}
else if (flag == 80)
{
temp->x = temp->next->x;
temp->y = temp->next->y + 1;
}
else if (flag == 75)
{
temp->x = temp->next->x - 2;
temp->y = temp->next->y;
}
//刪尾巴
pSnake tmp = head;
while (tmp->next->next != NULL)
{
tmp = tmp->next;
}
pos(tmp->next->x, tmp->next->y);
free(tmp->next);
printf(" ");
tmp->next = NULL;
print();
}
清除游標
//隱藏游標
void HideCursor()
{
CONSOLE_CURSOR_INFO curInfo; //定義游標資訊的結構體變數
curInfo.dwSize = 1; //如果沒賦值的話,游標隱藏無效
curInfo.bVisible = FALSE; //將游標設置為不可見
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //獲取控制台句柄
SetConsoleCursorInfo(handle, &curInfo); //設置游標資訊
}
產生食物
void Cfood()
{
int x, y;
random(&x, &y);
if ((x >= 2 && x <= 78) && (y <= 25 && y >= 1)) //在牆裡面
{
if (isIn(x, y) == 0) //不在蛇內
{
fx = x;
fy = y;
pos(x, y);
printf("★");
return;
}
}
Cfood();
}
判斷是否在蛇身上
int isIn(int x, int y)
{
pSnake tmp = head;
while (tmp != NULL)
{
if (tmp->x == x && tmp->y == y)
{
return 1;
}
tmp = tmp->next;
}
return 0;
}
隨機函數
//隨機函數
void random(int *x, int *y)
{
srand((unsigned)time(NULL));
// 2~78 1~39
int a = rand() % 39 + 1;
int b = rand() % 25 + 1;
*x = a * 2;
*y = b;
}
判斷食物是否被吃
int isFood()
{
pSnake tmp = head;
if (tmp->x == fx && tmp->y == fy)
{
Cfood();
add();
return 1;
}
return 0;
}
蛇的增長
void addSnake()
{
pSnake tmp = head;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
pSnake temp = (pSnake)malloc(sizeof(Snake));
tmp->next = temp;
temp->next = NULL;
}
蛇的死亡
int isDe()
{
pSnake tmp = head;
if (tmp->x == 0 || tmp->x == 80 || tmp->y == 0 || tmp->y == 26)
{
return 1;
}
tmp = tmp->next;
while (tmp != NULL)
{
if (head->x == tmp->x && head->y == tmp->y)
{
return 1;
}
tmp = tmp->next;
}
return 0;
}
增加積分
//增加積分
void add()
{
score += 5;
pos(90, 15);
printf("您現在的積分是:%d", score);
}
顏色
int color(int num)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), num);
return 0;
}
已經死了
void death()
{
system("cls");
pos(40, 15);
printf("您獲得的分數是:%d", score);
pos(40, 16);
if (score <= 20)
{
printf("你已經死亡,真是個垃圾");
}
else if (score <= 30)
{
printf("呦呵小夥子有點東西");
}
else if (score <= 40)
{
printf("傳說中的訓蛇大師");
}
else if (score > 40)
{
printf("你就是神!!!!");
}
else if (score > 100)
{
printf("超越神啦!!!!!!!!!!");
}
getchar();
}