c语言链表写贪吃蛇代码,链表贪吃蛇 附代码

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

//代码放二楼

#include

#include

#include

#include

#include

void eat();

void fuc();

int Xfeet = 10;

int Yfeet = 10;

int var = 0;

struct abc

{

int x = 0;

int y = 0;

};

struct snake

{

int x;

int y;

int UDLR;

snake* prev;

snake* next;

};

abc Map[900];

int var1 = 0;

char strA [20];

int hang = 0;

int kr = 0;

int slee = 200;

int ci = 1;

int color = BLUE;

snake *q = NULL, *head = NULL, *tail = NULL, *Ahead ,*A66 = NULL;

void PintfMap() //初始化地图

{

Xfeet = rand() % 28 + 2;

Yfeet = rand() % 28 + 2;

int x = 10;

int y = 10;

int cnt = 5;

q = (snake*)malloc(sizeof(struct snake));

q->x = x;

q->y = y;

q->UDLR = 1;//1上2下3左4右

q->prev = NULL;

head = q;

tail = q;

do

{

q = (snake*)malloc(sizeof(struct snake));

q->x = x++;

q->y = y;

q->UDLR = 1;//1上2下3左4右

q->next = NULL;

tail->next = q;

q->prev = tail;

tail = tail->next;

} while (--cnt);

for (int i = 0; i < 900; i++)

{

++var;

Map[i].x = var * 17;

Map[i].y = hang * 17+17;

if (var == 30)

{

++hang;

var = 0;

}

}

}

void Game_Play()//画地图

{

for (int i = 0; i < 900; i++)

{

if (Map[i].y == 17 || Map[i].y == 510 || Map[i].x == 17 || Map[i].x == 510)

{

setfillcolor(WHITE);

solidrectangle(Map[i].x, Map[i].y, Map[i].x + 15, Map[i].y + 15);

}

else

{

setfillcolor(BLACK);

solidrectangle(Map[i].x, Map[i].y, Map[i].x + 15, Map[i].y + 15);

}

}

setfillcolor(YELLOW);

solidrectangle(Xfeet*17, Yfeet*17, Xfeet *17+15, Yfeet *17+15);

Ahead = head;

while (head != NULL)

{

setfillcolor(color);

solidrectangle(head->x * 17, head->y * 17, head->x * 17 + 15, head->y * 17 + 15);

head = head->next;

}

head = Ahead;

}

void Game_Move()

{

Ahead = head;

A66 = head;

while (head != NULL)

{

if (head->UDLR == 1) //1上2下3左4右

{

head->x -= 1;

}

else if (head->UDLR == 2)

{

head->x += 1;

}

else if (head->UDLR == 3)

{

head->y += 1;

}

else if (head->UDLR == 4)

{

head->y -= 1;

}

head = head->next;

}

head = Ahead;

}

void Game_Key()

{

if (GetAsyncKeyState('W'))//1左2右3上4下

head->UDLR = 4;

else if (GetAsyncKeyState('S'))

head->UDLR = 3;

else if (GetAsyncKeyState('A'))

head->UDLR = 1;

else if (GetAsyncKeyState('D'))

head->UDLR = 2;

else if (GetAsyncKeyState('B'))

color = BROWN;

else if (GetAsyncKeyState('N'))

color = LIGHTMAGENTA;

else if (GetAsyncKeyState('M'))

color = LIGHTGRAY;

else if (GetAsyncKeyState('C'))

++kr;

else if (GetAsyncKeyState('V'))

fuc();

}

void Game_AWSD()

{

Ahead = tail;

while (tail->prev != NULL)

{

tail->UDLR = tail->prev->UDLR;

tail = tail->prev;

}

tail = Ahead;

}

void Game_Ifmove()

{

Ahead = head;

do

{

int AX = A66->x;

int AY = A66->y;

if (A66->UDLR == 1) //1上2下3左4右

{

AX = A66->x;

AY = A66->y;

--AX;

}else if (A66->UDLR == 2)

{

AX = A66->x;

AY = A66->y;

++AX;

}else if (A66->UDLR == 3)

{

AX = A66->x;

AY = A66->y;

++AY;

}else if (A66->UDLR == 4)

{

AX = A66->x;

AY = A66->y;

--AY;

}

if (head->next==NULL)

{

break;

}

head = head->next;

if ( AX == head->x && AY == head->y && head!=A66)

{

for (int i = 0; i < 10; i++)

{

setfillcolor(RED);

solidrectangle(AX * 17, AY * 17, AX * 17 + 15, AY * 17 + 15);

Sleep(200);

setfillcolor(WHITE);

solidrectangle(AX * 17, AY * 17, AX * 17 + 15, AY * 17 + 15);

Sleep(200);

RECT r = { 0, 0, 640, 480 };

drawtext(_T("十秒后复活"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

}

}

if (Xfeet == AX && Yfeet == AY)

{

++var1;

fuc();

eat();

}

} while (head!=NULL);

head = Ahead;

}

void fuc()

{

q = (snake*)malloc(sizeof(struct snake));

if (tail->UDLR == 1)

{

q->x = (tail->x) + 1;

q->y = (tail->y);

}

else if (tail->UDLR == 2)

{

q->x = (tail->x) - 1;

q->y = (tail->y);

}

else if (tail->UDLR == 3)

{

q->x = (tail->x);

q->y = (tail->y)-1;

}

else if (tail->UDLR == 4)

{

q->x = (tail->x);

q->y = (tail->y)+1;

}

q->UDLR = tail->UDLR;//1上2下3左4右

q->next = NULL;

tail->next = q;

q->prev = tail;

tail = tail->next;

}

void eat()

{

Xfeet = rand() % 28+2;

Yfeet = rand() % 28+2;

}

int main()

{

int var = 0;

PintfMap();

initgraph(740, 540);

while (1)

{

Game_Play();

Game_Key();

Game_AWSD();

Game_Move();

Game_Ifmove();

if (kr%2==1)

{

++ci;

if (ci == 1)

color = RED;

else if (ci == 2)

color = DARKGRAY;

else if (ci == 3)

color = CYAN;

else if (ci >= 4)

{

color = GREEN;

ci = 1;

}

slee = 100;

}

else if (kr % 2 == 0)

{

color = BLUE;

slee = 200;

}

sprintf(strA, "你当前分数%d", var1);

outtextxy(550, 20, strA);

outtextxy(550, 70, "请启用大写输入控制蛇");

outtextxy(550, 120, "B,N,M键切换蛇颜色");

outtextxy(550, 170, "C键进入狂热模式");

outtextxy(550, 220, "再次摁C键关闭狂热模式");

outtextxy(550, 270, "V键使蛇长度+1 可以长按");

Sleep(slee);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值