c语言贪吃蛇easyx数组,原来是用数组做的,今天试了下用双向链表做的贪吃蛇,只实现了最...

这个博客内容是一个使用C++编写的控制台版贪吃蛇游戏。游戏包括了蛇的移动、食物生成、碰撞检测等功能。玩家可以通过键盘控制蛇的方向,当蛇吃到食物时,会增长。游戏循环进行,直到玩家停止。

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

#include

#include

#include

#include

#pragma warning(disable:4996)

#define radius 10

#define height 600

#define width 600

#define movespeed 100

typedef struct snake{

int x;

int y;

snake *next;

snake *prior;

}snake;

typedef struct food{

int x;

int y;

int color_x;

int color_y;

int color_z;

int food_radius;

}food;

food *foodl = new food;

char direction,memory='s';

snake *p = new snake, *head = p, *tail = new snake, *delete_tail = new snake, *p1 = NULL;

BOOL judge = false;

void foodcolor()

{

foodl->color_x = rand() % 255;

foodl->color_y = rand() % 255;

foodl->color_z = rand() % 255;

setfillcolor(RGB(foodl->color_x, foodl->color_y, foodl->color_z));

solidcircle( foodl->x,foodl->y , radius);

}

void judgefood(snake *head)

{

if (pow((double)(head->x - foodl->x), 2) + pow((double)(head->y - foodl->y), 2) <= pow(double(radius + radius), 2))

{

judge = true;

setfillcolor(BLACK);

solidcircle(foodl->x, foodl->y, radius);

foodl->x = rand() % (height - radius);

foodl->y = rand() % (width - radius) + radius;//初始化食物坐标

}

}

void showsnake(snake *head)

{

snake *p = head;

while (p)

{

setfillcolor(YELLOW);

solidcircle(p->x, p->y, radius);

p = p->next;

}

}

void judgesnake()//判断蛇是否吃到食物

{

showsnake(head);

if (judge == false)

{

setfillcolor(BLACK);

solidcircle(delete_tail->x, delete_tail->y, radius);

delete_tail = delete_tail->prior; //删除尾节点

delete_tail->next = NULL;

}

else

{

judge = false;

}

}

void boardcontrol()

{

p1 = new snake; //开辟一个新节点

judgesnake();

Sleep(movespeed);

while (kbhit())

direction = getch();

if (direction == 'k')

system("pause");

if (direction == 'w' || direction == 's' || direction == 'a' || direction == 'd')

memory = direction;

else

{

direction = memory;

}

switch (direction) //根据按键不同的方向 控制尾节点排在头结点的坐标位置 一次来判断蛇头的移动

{

case 'w':

{

p1->x = head->x;

p1->y = head->y - 2 * radius; break;

}

case 's':

{

p1->x = head->x;

p1->y = head->y + 2 * radius; break;

}

case 'a':

{

p1->x = head->x - 2 * radius;

p1->y = head->y; break;

}

case 'd':

{

p1->x = head->x + 2 * radius;

p1->y = head->y; break;

}

default:break;

}

p1->next = head;

head->prior = p1; //p1成为头结点

head = p1;

showsnake(head);

FlushBatchDraw();

}

void initsnake()//把最开始的是三个节点连接起来

{

p->x = 200;

p->y = 200;

tail->x = 200 + 2 * radius;

tail->y = 200;

delete_tail->x = tail->x + 2 * radius;

delete_tail->y = 200;//给最开始蛇三个节点的坐标赋值

delete_tail->next = NULL;

p->next = tail;

tail->next = delete_tail; //三个节点坐标的地址前后相互连接起来

tail->prior = p;

delete_tail->prior = tail;

}

void main()

{

foodl->x = rand() % (height - radius);

foodl->y = rand() % (width - radius) + radius;//初始化食物坐标

head = p;

initgraph(height, width);

initsnake();

showsnake(head);

char c = getch();

BeginBatchDraw();//开始批量绘图操作

while (1)

{

foodcolor();

judgefood(head);

boardcontrol();

}

EndBatchDraw();//结束批量绘图操作

closegraph();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值