c语言最复杂贪吃蛇,新手做的贪吃蛇,感觉逻辑很乱很复杂,求大神来指点!...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

新手做的贪吃蛇,感觉逻辑很乱很复杂,求大神来指点!

/*wsad控制方向*/ #include

#include

#include

#include

#include

#define up 119

#define down 115

#define left 97

#define rigth 100

#define kuan 39

#define gao 25

#define sudu 100 //速度(毫秒)

typedef struct sheo

{

int x;

int y;

struct sheo * pNext;

}shee, *pshee;

void HideCursor();//隐藏光标

void gotoxy(int x, int y); //到xy坐标位置

pshee create_list(void); //创建蛇头

void shuchu_list(pshee phead, int ditu[gao][kuan*2]); //输出链表

bool charu(pshee phead); //插入链表

pshee shetou(pshee phead); //蛇头的地址

pshee shewei(pshee phead); //蛇尾的地址

void gaibian_list(pshee phead, int t); //改变蛇的坐标

void shengchengsw(int ditu[gao][kuan*2]); //生成食物

int fangxiang(char ch, int t); //方向 int main(void)

{

char ch;

int i, j, t;

int ditu[gao][kuan*2];

pshee phead, pshe, swei;

for(i=1; i

for(j=1; j

ditu[i][2*j]=0;

for(i=0; i

{

ditu[0][2*i]=1;

ditu[gao-1][2*i]=1;

}

for(i=0; i

{

ditu[i][0]=1;

ditu[i][1]=1;

ditu[i][kuan*2-1]=1;

ditu[i][kuan*2-2]=1;

}

for(i=0; i

for(j=0; j

{

if (ditu[i][j*2]==1)

{

gotoxy(2*j,i);

printf("口");

}

}

shengchengsw(ditu);

HideCursor();

phead = create_list();

shuchu_list(phead, ditu);

ch = 'a';

while(1)

{

while(kbhit())

{

ch=getch();

}

t = fangxiang(ch, t);

swei = shewei(phead);

if(4==ditu[phead->pNext->y][2*phead->pNext->x])

{

charu(phead);

ditu[phead->pNext->y][2*phead->pNext->x]=0;

shengchengsw(ditu); }

gotoxy(2 * swei->x, swei->y);

printf(" ");

gaibian_list(phead, t);

shuchu_list(phead, ditu);

Sleep(sudu);

}

return 0;

} pshee create_list(void) //创建链表

{

int i, len=3;

pshee phead, ptail;

phead = (pshee)malloc(sizeof(shee));

phead->pNext = NULL;

ptail =phead;

for(i=0; i

{

pshee pNew;

pNew = (pshee)malloc(sizeof(shee));

pNew->x = kuan/2+i;

pNew->y = 10;

pNew->pNext = NULL;

ptail->pNext = pNew;

ptail = pNew;

}

return phead;

} void shuchu_list(pshee phead, int ditu[gao][kuan*2]) //输出链表

{

char ch;

pshee psc;

psc=phead->pNext;

int i=1;

while(psc!=NULL)

{ if(0==ditu[psc->y][2*psc->x]||4==ditu[psc->y][2*psc->x])

{

gotoxy(2 * psc->x, psc->y);

printf("▇");

}

else

{

system("cls");

gotoxy(20, 10);

printf("死亡,退出请按#,继续请按Y");

while(1)

{

ch = getch();

if(ch=='#')

exit(1);

else if('y'==ch||'Y'==ch)

main();

else

continue;

}

}

psc = psc->pNext;

}

} bool charu(pshee phead) //插入链表

{

pshee pcr, pt, pn;

pt = phead->pNext; while(pt!=NULL)

{

pn = pt; // pt->pnext已经为空,所以要有pn先保存上一次的pt地址

pt = pt->pNext;

}

pcr = (pshee)malloc(sizeof(shee));

pcr->x = pn->x;

pcr->y = pn->y;

pcr->pNext = NULL;

pn->pNext = pcr;

pn = pcr;

return true;

} pshee shetou(pshee phead) //蛇头的地址

{

return phead->pNext;

} pshee shewei(pshee phead) //蛇尾的地址

{

pshee ptail, pwei;

ptail = phead->pNext;

while(NULL!=ptail)

{

pwei = ptail;

ptail = ptail->pNext;

}

return pwei;

} void gaibian_list(pshee phead, int t) //改变蛇的坐标

{

int map[4][2] = {0,-1,0,1,-1,0,1,0};

int i=0, len;

int *m, *n;

pshee ptail;

ptail = phead->pNext;

while(NULL!=ptail)

{

i++;

ptail = ptail->pNext;

}

len = i;

m = (int*)malloc(i * sizeof(int));

n = (int*)malloc(i * sizeof(int));

ptail = phead->pNext;

i=0;

while(NULL!=ptail)

{

m[i] = ptail->x;

n[i] = ptail->y;

i++;

ptail = ptail->pNext;

}

ptail = phead->pNext;

ptail->x = ptail->x + map[t][0];

ptail->y = ptail->y + map[t][1];

ptail = ptail->pNext;

i=0;

for(i=0; i

{

ptail->x = m[i];

ptail->y = n[i];

ptail = ptail->pNext;

} return ;

}

void HideCursor()//隐藏光标

{

CONSOLE_CURSOR_INFO cursor_info = {1, 0};

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

} void gotoxy(int x, int y) //到xy坐标位置

{

COORD c;

c.X = x;

c.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);

}

void shengchengsw(int ditu[gao][kuan*2]) //生成食物

{

int i, j;

srand((unsigned)time(NULL));

i = rand()%(gao-2)+1;

j = 2 * (rand()%(kuan-2)+1);

ditu[i][j]=4;

gotoxy(j,i);

printf("⊕");

return ;

}

int fangxiang(char ch, int t) //方向

{

switch(ch)

{

case 'w':

{

if(t!=1)

t=0;

return t;

}

case 's':

{

if(t!=0)

t=1;

return t;

}

case 'a':

{

if(t!=3)

t=2;

return t;

}

case 'd':

{

if(t!=2)

t=3;

return t;

}

case 'W':

{

if(t!=1)

t=0;

return t;

}

case 'S':

{

if(t!=0)

t=1;

return t;

}

case 'A':

{

if(t!=3)

t=2;

return t;

}

case 'D':

{

if(t!=2)

t=3;

return t;

}

default : break;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值