C语言一个简单的贪吃蛇程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wlzzhcsdn/article/details/78225613

这是很早以前写的.好像还是有问题,"食物"的出现有可能跟墙壁重叠,懒得改啦C语言一个简单的贪吃蛇程序

#include
#include
#include
#include
#include
using namespace std;
const int HALL=20;
const int LEVEL=300;
const char T='#';
char hall[HALL][HALL]={' '};//墙壁
bool ifgameover(int,int);
void gameover();
class snake//蛇结构
{
public:
 int get_x();
 int get_y();
 void change(int,int);
 void draw(int);
 int add_head(int,int);
 void delete_tail();
 snake *snext;
 snake *slast;
private:
 int x,y;
}*phead,*ptail; //蛇头蛇尾
int snake::get_x()
{
 return x;
}
int snake::get_y()
{
 return y;
}
void snake::change(int a,int b)
{
 x=a;
 y=b;
}
int snake::add_head(int a,int b)
{
 if(hall[a][b]=='*')
  return -1;
 else{
 snake *q=new snake;
 q->change(a,b);
 q->draw(1);
 q->slast=NULL;
 q->snext=phead;
 phead->slast=q;
 phead=q;
 return 1;}
}
void snake::delete_tail()
{
 ptail->draw(2);
 snake *p=new snake;
 p=ptail->slast;
 free(ptail);
 ptail=p;
 ptail->snext=NULL;
}
void snake::draw(int a)
{
 if(a==1)
  hall[x][y]='*';
 if(a==2)
  hall[x][y]=' ';
 else
  return;
}

class food//食物
{
public:
 void getfood();
 food();
    int getfood_x();
 int getfood_y();
private:
 int x,y;
};
void food::getfood()
{
 srand((unsigned int) time(NULL));
 x=rand()%(HALL-1);
 y=rand()%(HALL-1);
 while(hall[x][y]=='*')
 {
 x=rand()%(HALL-1);
 y=rand()%(HALL-1);
 }
 hall[x][y]='$';
}
int food::getfood_x()
{
 return x;
}
int food::getfood_y()
{
 return y;
}
food::food()
{
 x=7;
 y=7;
}
class start//初始化
{
public:
 void draw();//画框框
 void draw1();//画蛇
};
void start::draw()
{
 system("CLS");
 int i,j;
 for(i=0;i<=HALL;i++){
  for(j=0;j<=HALL;j++)
  {
   if(i==0||i==HALL||j==0||j==HALL)
    cout<<T;
   else
    cout<<hall[i][j];
  }
  cout<<endl;
 }
}

void start::draw1()
{
 snake *pnew=new snake;
 phead=pnew;
 phead->slast=NULL;
 phead->change(5,6);
 pnew=(snake *)malloc(sizeof snake);
 phead->snext=pnew;
 pnew->slast=phead;
 pnew->change(5,5);
 ptail=pnew;

 pnew=(snake *)malloc(sizeof snake);
 ptail->snext=pnew;
 pnew->slast=ptail;
 pnew->change(5,4);
 ptail=pnew;

 hall[5][6]='*';
 hall[5][5]='*';
 hall[5][4]='*';
}

class moving
{
public:
 void change_point(int,char);
 bool over();
 char point;
};
void moving::change_point(int n,char a) //n==0没有按键 n==1有按键输入
{
 if(n==0)
 {
  switch(point)
  {
  case 'w':
   if(phead->add_head(phead->get_x()-1,phead->get_y())==1){}
   else gameover();
   break;
  case 's':
   if(phead->add_head(phead->get_x()+1,phead->get_y())==1){}
   else gameover();
   break;
  case 'a':
   if(phead->add_head(phead->get_x(),phead->get_y()-1)==1){}
   else gameover();
   break;
  case 'd':
   if(phead->add_head(phead->get_x(),phead->get_y()+1)==1){}
   else gameover();
   break;
  }
  ptail->delete_tail();
 }
 if(n==1)
 {
  if(a=='w' && point!='s')
   {
    if(phead->add_head(phead->get_x()-1,phead->get_y())==1)
    {ptail->delete_tail(),point=a;}
    else
     gameover();
   }
  else if(a=='s' && point!='w')
   {
    if(phead->add_head(phead->get_x()+1,phead->get_y())==1){ptail->delete_tail(),point=a;}
    else
     gameover();
   }
  else if(a=='a' && point!='d')
   {
    if(phead->add_head(phead->get_x(),phead->get_y()-1)==1){ptail->delete_tail(),point=a;}
    else
     gameover();
   }
  else if(a=='d' && point!='a')
   {
    if(phead->add_head(phead->get_x(),phead->get_y()+1)==1){ptail->delete_tail(),point=a;}
    else
     gameover();
   }
  else
   change_point(0,1);
 }
 if(n==2)
 {
  if(a=='w' && point!='s')
   {
    if(ifgameover(phead->get_x()-1,phead->get_y()))
    phead->add_head(phead->get_x()-1,phead->get_y());
    else
     gameover();
   }
  else if(a=='s' && point!='w')
  {
   if(ifgameover(phead->get_x()+1,phead->get_y()))
    phead->add_head(phead->get_x()+1,phead->get_y());
   else
    gameover();
  }
  else if(a=='a' && point!='d')
   {
    if(ifgameover(phead->get_x(),phead->get_y()-1))
    phead->add_head(phead->get_x(),phead->get_y()-1);
   else
    gameover();
   }
  else if(a=='d' && point!='a')
  {
   if(ifgameover(phead->get_x(),phead->get_y()+1))
    phead->add_head(phead->get_x(),phead->get_y()+1);
   else
    gameover();
  }
  else
  {
   switch(point)
  

猜你喜欢

转载自blog.csdn.net/wlzzhcsdn/article/details/78225613