贪吃蛇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
    评论
C语言中的贪吃蛇游戏通常使用链表数据结构来实现蛇的身体部分,因为链表可以动态地添加和删除节点,非常适合表示一个动态增长的数据集合。在贪吃蛇游戏中,每个节点代表蛇身上的一个方块,链表头节点是蛇的头部,每当蛇移动时,尾部节点会被移动到头部的位置,新的头部位置则根据蛇的移动方向添加。 以下是实现贪吃蛇链表的基本步骤: 1. 定义节点结构:创建一个结构体,包含位置信息(通常是x, y坐标)和下一个节点的指针。 ```c typedef struct Node { int x, y; // 蛇的位置 struct Node* next; // 指向下一个节点的指针 } SnakeNode; ``` 2. 创建链表:初始化链表,通常只有一个头节点,初始位置为蛇的起始位置。 ```c SnakeNode* head = (SnakeNode*)malloc(sizeof(SnakeNode)); head->x = start_x; head->y = start_y; head->next = NULL; ``` 3. 移动和增长蛇:每次蛇移动,更新头部节点的位置,并根据吃到食物的行为决定是否增长链表。如果吃到食物,就在当前头部后面增加一个新的节点,更新头节点的next指针。 ```c if (eat_food(head)) { SnakeNode* newNode = (SnakeNode*)malloc(sizeof(SnakeNode)); newNode->x = head->x; newNode->y = head->y; newNode->next = head->next; head->next = newNode; } ``` 4. 渲染链表:遍历链表,根据每个节点的位置显示蛇的身体。 5. 检查边界和碰撞:在移动过程中,检查蛇是否碰到了墙壁或自己的身体,这会导致游戏结束。 6. 用户输入处理:接收用户的键盘输入,更新蛇的移动方向。 要了解更多细节,你可以询问具体如何处理用户输入、检测碰撞、以及游戏循环的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值