C语言一个简单的贪吃蛇程序「建议收藏」

C语言一个简单的贪吃蛇程序「建议收藏」这是很早以前写的.好像还是有问题,”食物”的出现有可能跟墙壁重叠,懒得改啦#include#include#include#include#includeusingnamespacestd;constintHALL=20;constintLEVEL=300;constcharT=’#’;charhall[HALL][HALL]={”};//墙壁bool

大家好,欢迎来到IT知识分享网。

这是很早以前写的.好像还是有问题,”食物”的出现有可能跟墙壁重叠,懒得改啦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)
   {

   case ‘w’:phead->add_head(phead->get_x()-1,phead->get_y());break;
   case ‘s’:phead->add_head(phead->get_x()+1,phead->get_y());break;
   case ‘a’:phead->add_head(phead->get_x(),phead->get_y()-1);break;
   case ‘d’:phead->add_head(phead->get_x(),phead->get_y()+1);break;
   }
  }
 }
}

bool moving::over()
{

 if(phead->get_x()==HALL-1 || phead->get_x()==0 || phead->get_y()==HALL-1 || phead->get_y()==0 )
  return 0;
 else
  return 1;
}
void gameover()
{

 system(“CLS”);
 printf(“\n\n\n\t\tGAME OVER!\n\n\n”);
 system(“pause”);
 exit(0);
}
bool ifgameover(int a,int b)
{

 if(hall[a][b]==’*’|| hall[a][b]==’T’)
  return 0;
 return 1;
}
int main()
{

 food f;
 start t;
 moving r;
 r.point=’d’;
 t.draw1();
 f.getfood();
 t.draw();
 char key;
 while(1)
 {

  fflush(stdin);
  key=NULL;
  if(phead->get_x()==f.getfood_x() && phead->get_y()==f.getfood_y())
  {

   r.change_point(2,0);
   f.getfood();
  }
  if(kbhit()==0)
   {

    r.change_point(0,0);
   }
  else
   {

    key=getch();
    r.change_point(1,key);
       }
   t.draw();
  if(r.over()==0)
  {

   gameover();
  }
  Sleep(LEVEL);
 }
}

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/24351.html

(0)
上一篇 2023-08-23 14:33
下一篇 2023-08-26 14:45

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信