谁有贪吃蛇_vs2008 C++代码啊?
发布网友
发布时间:2022-07-29 21:41
我来回答
共2个回答
热心网友
时间:2024-11-17 23:37
使用链表完成贪吃蛇游戏的代码非常适合,贪吃蛇的每次移动可以看做链表的头部插入和尾部删除
具体代码如下
#include "snake.h"
#define UP_CMD 'w'
#define DOWN_CMD 's'
#define LEFT_CMD 'a'
#define RIGHT_CMD 'd'
#define EAT_FOOD 0x02//吃到食物
#define EAT_SELF 0x04//迟到自己
#define EAT_NONE 0x05//无吃到
typedef char DIRECTION;
class SnakeNode
{
private:
COORD pos;//节点坐标
SnakeNode *next;
public:
static COORD foodxy;//食物坐标
static SnakeNode *head;//头部
static DIRECTION direction;//方向
static int speed;//移动速度
static void food();//产生食物
static void create();//产生蛇
static void free();//销毁
static int move();//移动
static void getcmd(DIRECTION d);//改变方向
};
COORD SnakeNode::foodxy;
SnakeNode* SnakeNode::head = NULL;
DIRECTION SnakeNode::direction = RIGHT_CMD;//方向
int SnakeNode::speed = 400;//方向
//蛇类
void SnakeNode::free()
{
SnakeNode *p,*q;
p = head;
while(p)
{
q = p->next;
delete p;
p=q;
}
}
void SnakeNode::getcmd(DIRECTION d)
{
switch(d)
{
case 'w':case 'W':
if(direction!='s') direction='w';break;
case 's':case 'S':
if(direction!='w') direction='s';break;
case 'a':case 'A':
if(direction!='d') direction='a';break;
case 'd':case 'D':
if(direction!='a') direction='d';break;
}
}
void SnakeNode::food()
{
rand:
srand(GetTickCount());
foodxy.X=(rand()%38)*2;
foodxy.Y=rand()%25;
SnakeNode *p = head;
while(p!=NULL)
{
if(p->pos.X==foodxy.X&&p->pos.Y==foodxy.Y)
{
goto rand;
break;
}
p = p->next;
}
gotoxy(foodxy.X,foodxy.Y);
printf("☆");
}
void SnakeNode::create()
{
SnakeNode *p = new SnakeNode;
head = p;
int i;
for(i=26;i>=20;i-=2)
{
p->pos.X=i;
p->pos.Y=10;
gotoxy(i,10);
cout<<"■";
p->next = new SnakeNode;
if(i==20)
{
p->next = NULL;
}
p = p->next;
}
food();
}
int SnakeNode::move()
{
SnakeNode *newhead = new SnakeNode;
int cX,cY;
switch(direction)
{
case 'w':
{
cX = 0; cY = -1;
}break;//创建一个新头部,删除尾巴
case 's':
{
cX = 0; cY = 1;
}break;
case 'a':
{
cX = -2; cY = 0;
}break;
case 'd':
{
cX = +2; cY = 0;
}break;
}
newhead->pos.X = head->pos.X + cX;
newhead->pos.Y = head->pos.Y + cY;
newhead->next = head;
head = newhead;
if(head->pos.Y<0) head->pos.Y=24;
if(head->pos.Y>24) head->pos.Y=0;
if(head->pos.X<0) head->pos.X=78;
if(head->pos.X>78) head->pos.X=0;
//判断是否咬到自己
SnakeNode *q = head->next;
while(q!=NULL)
{
if((q->pos.X==head->pos.X)&&(q->pos.Y==head->pos.Y))
{
free();
return EAT_SELF;
}
q=q->next;
}
gotoxy(head->pos.X,head->pos.Y);
printf("■");
//判断是否吃到食物
SnakeNode *p = head;
while(p!=NULL)
{
if(head->pos.X==foodxy.X&&head->pos.Y==foodxy.Y)//是否吃到食物
{
//吃到食物加速
if(speed >= 100)
speed -= 20;
food();
return EAT_FOOD;
break;//吃到了则不删除尾部,相当于增加一节
}
//删除尾部
if(p->next->next==NULL)
{
gotoxy(p->next->pos.X,p->next->pos.Y);
printf(" ");
delete p->next;
p->next=NULL;
return EAT_NONE;
}
p = p->next;
}
}
int main(){
SnakeNode::create();
while(1)
{
if(_kbhit())
{
int ch=getch();
switch(ch)
{
case 'w':case 'W': case 's':case 'S':case 'a':case 'A':
case 'd':case 'D':
{
SnakeNode::getcmd(ch);
}break;
case 'b':
{
SnakeNode::free();
return 0;
}
}
}
if(SnakeNode::move()==EAT_SELF)
{
gotoxy(30,11);
cout<<"游戏结束";
getch();
return 0;
}
Sleep(SnakeNode::speed);
}
}
热心网友
时间:2024-11-17 23:37
你好!具体什么要求呢