#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
#include <MMsystem.h>
#pragma comment(lib,"winmm.lib")
void start();
enum direction { west=-2, east=+2, north=-1, south=+1 };
char a[29][70] = { "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■ ■\n",
"■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n" };
//定义链表
struct Node
{
int a;
int b;
enum direction c;
struct Node* pnext;
};
struct Node *g_phead = NULL;
struct Node *g_pend = NULL;
//添加结点
void list(int a,int b,enum direction c)
{
struct Node* ptemp = (struct Node*)malloc(sizeof(struct Node));
ptemp->a = a;
ptemp->b = b;
ptemp->c = c;
ptemp->pnext = NULL;
if (g_phead == g_pend)
{
g_phead->pnext = ptemp;
}
else
{
g_pend->pnext = ptemp;
}
g_pend = ptemp;
}
void Delete()//只用于删除尾
{
struct Node* pTemp = g_phead->pnext;
while (pTemp->pnext != g_pend)
continue;
free(g_pend);
g_pend = pTemp;
}
//头添加
void AddHead()
{
struct Node* pTemp = (struct Node*)malloc(sizeof(struct Node));
pTemp->a = g_phead->pnext->a+2;
pTemp->b = g_phead->pnext->b;
pTemp->c = g_phead->pnext->c;
pTemp->pnext = NULL;
pTemp->pnext = g_phead->pnext;
g_phead->pnext = pTemp;
}
//void addlong()
//{
// struct Node* pTemp = (struct Node*)malloc(sizeof(struct Node));
// pTemp->a = g_pend->a - 2;
// pTemp->b = g_pend->b;
// pTemp->c = g_pend->c;
// pTemp->pnext = NULL;
// g_pend->pnext = pTemp;
// g_pend = pTemp;
//}
//结点复制
void copywestlist()
{
struct Node* ptemp1 = g_phead->pnext;
struct Node* ptemp2 = ptemp1->pnext;
while (ptemp2!=g_pend)
{
ptemp1->a = ptemp2->a;
ptemp1->b = ptemp2->b;
ptemp1->c = ptemp2->c;
ptemp1 = ptemp1->pnext;
ptemp2 = ptemp2->pnext;
}
ptemp1->a = ptemp2->a ;
ptemp1->b = ptemp2->b;
ptemp1->c = ptemp2->c;
struct Node* ptemp3 = g_pend;
if(ptemp3->c==east || ptemp3->c==west)
ptemp3->a = ptemp3->a +ptemp3->c;
else
{
ptemp3->b = ptemp3->b + ptemp3->c;
}
}
//void appear()
//{
// start();
// list(x,y,east);
//}
//空格开始游戏
void star()
{
char chstart;
chstart = _getch();
for (;1;)
{
if (' ' == chstart)
break;
chstart = _getch();
}
}
//首页
void firstpage()
{
printf("按空格开始游戏\n");
printf("←↑→↓控制\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\t\t\t\t\t\t\t**贪吃蛇**\n");
}
//音乐
void music()
{
PlaySound(L"卡农.wav", NULL, SND_FILENAME | SND_ASYNC);
}
//转换
void change()
{
struct Node* ptemp = g_phead->pnext;
while (1)
{
strncpy(&a[ptemp->b][ptemp->a], "■", 2);
if (ptemp==g_pend)
break;
ptemp = ptemp->pnext;
}
}
//打印
void back()
{
/*start();*/
for (int i = 0;i <= 28;i++)
{
printf(a[i]);
}
}
//死亡
int dead()
{
int q=1;
if (east == g_pend->c || g_pend->c == west)
{
if (0 == strncmp(&a[g_pend->b][g_pend->a + g_pend->c], "■", 2))
{
q=0;
}
}
else
{
if (0 == strncmp(&a[g_pend->b+g_pend->c][g_pend->a], "■", 2))
{
q=0;
}
}
return q;
}
//随即生成三节点的蛇
void start()
{
int x, y;
srand((unsigned int)time(NULL));
x = (rand() % 27 + 2)*2;
y = rand() % 27 + 1;
//if (x % 2 == 1)
// x = x + 1;
list(x, y, west);
AddHead();
AddHead();
//a[5][6] = "■";
}
//void run()
//{
// AddHead();
// Delete();
//
//}
//控制
int control()
{
if (GetAsyncKeyState(VK_UP))
g_pend->c = north;
if (GetAsyncKeyState(VK_DOWN))
g_pend->c = south;
if (GetAsyncKeyState(VK_RIGHT))
g_pend->c = east;
if (GetAsyncKeyState(VK_LEFT))
g_pend->c = west;
}
//销毁
int no()
{
struct Node* ptemp = g_phead->pnext;
while (1)
{
strncpy(&a[ptemp->b][ptemp->a], " ", 2);
if (ptemp == g_pend)
break;
ptemp = ptemp->pnext;
}
}
int food()
{
srand((unsigned int)time(NULL));
int z;
int v;
int i=0;
struct Node* ptemp = g_phead->pnext;
while (1)
{
z = (rand() % 32+1) * 2;
v = rand() % 27 + 1;
while (1)
{
/*strncpy(&a[ptemp->b][ptemp->a], "■", 2);*/
if (ptemp-> a == z && ptemp->b == v)
{
i = 1;
break;
}
if (ptemp == g_pend)
{
i = 2;
break;
}
ptemp = ptemp->pnext;
}
if (i == 1)
continue;
else if (i == 2)
break;
}
strncpy(&a[v][z], "★", 2);
}
int appearfood()
{
int i = 0, j = 0;int y=1;
for (i = 1;i <= 27;i = i + 1)
{
for (j = 2;j <= 68;j = j + 2)
if (0 == strncmp(&a[i][j], "★", 2))
{
y = 0;
break;
}
if (0 == y)
break;
}
if (y == 1)
food();
}
int makelong()
{
int q = 1;
if (east == g_pend->c || g_pend->c == west)
{
if (0 == strncmp(&a[g_pend->b][g_pend->a + g_pend->c], "★", 2))
{
q = 0;
}
}
else
{
if (0 == strncmp(&a[g_pend->b + g_pend->c][g_pend->a], "★", 2))
{
q = 0;
}
}
return q;
}
int main()
{
g_phead = (struct Node*)malloc(sizeof(struct Node));
g_phead->pnext = NULL;
g_pend = g_phead;
//addlong();
music();
firstpage();
star();
system("cls");
start();
/*food();*/
while (1)
{
system("cls");
appearfood();
control();
//判断是否死亡
change();
if (dead() == 0)
{
system("cls");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\t\t\t\t\t\t\t游戏结束\n");
break;
}
if (makelong() == 0)
{
AddHead();
}
no();
//链表复制
copywestlist();
//转换
change();
//打印
back();
//清空
no();
/*run();*/
Sleep(500);
}
system("pause");
return 0;
}
贪吃蛇
最新推荐文章于 2024-04-20 15:20:30 发布