【C】贪吃蛇

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <windows.h>

#include <conio.h>

 

#define H 10

#define L 20

void map();

void map_print();

void up_go();

void down_go();

void left_go();

void right_go();

 

 

 

 

 

int coordinate[50][2];                      //坐标

int count=0;                      //吃到小虫子的个数

int i,j,k,temp1_x,temp1_y;

int count_test=0;

char anniu;

char ditu[H][L];

int time_x,time_y;

int main()

{

         srand(time(NULL));

         time_x=rand()%11;

         time_y=(rand()*2/5+6)%21;

         ditu[time_x][time_y]='o';

         map();

         //让地图中间有个东西

         ditu[H/2][L/2]='*';

         map_print();

         //设置主角的坐标

         temp1_x=H/2;

         temp1_y=L/2;

         //先判定按钮,然后判断按钮是什么,

         anniu='w';

         coordinate[0][0]=temp1_x;

         coordinate[0][1]=temp1_y;

         for(i=0;;i++)

         {

         system("cls");

         map_print();

        

        

//     count_test=count;

         switch(anniu)

         {

                  case 'w':

                                   up_go();

                          break;

                  case 's':

                                   down_go();

                          break;

                  case 'a':

                                   left_go();

                          break;

                  case 'd':

                                   right_go();

                          break;

         }

         }

 

}

 

 

 

 

void map()                                 //给地图画空格号

{

         for(i=0;j<H;j++)

         {

                  for(k=0;k<L;k++)

                  {

                          ditu[j][k]=' ';

                  }

                  printf("\n");

         }

}

void map_print()              //先描绘一下地图 

{

         for(j=0;j<H;j++)

         {

                  for(k=0;k<L;k++)

                  {

                          printf("%c",ditu[j][k]);

                  }

                  printf("\n");

         }

}

 

 

 

 

 

 

 

 

//******************************************************************************上下左右移动代码************************************************************************************

void up_go()

{

         //********************************

         while(1)

         {

         for(i=0;i<20;i++)

         {

                  Sleep(20);

                  if(kbhit())

                  {

                          anniu=getch();

                          break;

                  }

         }

         if(anniu!='w')

         break;

         //*********************************

                 

 

                          //先把尾巴去掉,然后判断吃了虫子没,吃了就张一根之后循环把每一个前面的值都附在后面的点上,然后脑袋往前走,然后给脑袋加个形状,

         //     ditu[coordinate[count][0]][coordinate[count][1]]=' ';

                 

                  if(ditu[coordinate[0][0]][coordinate[0][1]]=='o')

                  {

                          count++;

                  }

                  ditu[coordinate[count][0]][coordinate[count][1]]=' ';

                  for(i=count;i>0;i--)

                  {

                          coordinate[i][0]=coordinate[i-1][0];                               //每一点的坐标

                          coordinate[i][1]=coordinate[i-1][1];                              

                  }

                 

                  coordinate[0][0]-=1;

                  ditu[coordinate[0][0]][coordinate[0][1]]='*';

 

                  if(count>count_test)

                  {

                  //     ditu[time_x][time_y]='*';

                          ditu[coordinate[count][0]][coordinate[count][1]]='-';

                          srand(time(NULL));

                          time_x=rand()%11;

                          time_y=(rand()*2/5+6)%21;

                          ditu[time_x][time_y]='o';

                          count_test=count;

                  }

        

                 

                 

 

                 

//**************************************************************

 

 

        

        

        

        

        

         ditu[time_x][time_y]='o';                                          //这是最神奇的BUG!!!!

         system("cls");

         map_print();

                 

         }      

}

 

 

void down_go()

{

while(1)

         {

         for(i=0;i<20;i++)

         {

                  Sleep(20);

                  if(kbhit())

                          {

                                   anniu=getch();

                                   break;

                          }      

         }

         if(anniu!='s')

         break;

        

 

//*********************************************************************************

                  //先把尾巴去掉,然后判断吃了虫子没,吃了就张一根之后循环把每一个前面的值都附在后面的点上,然后脑袋往前走,然后给脑袋加个形状,

         //     ditu[coordinate[count][0]][coordinate[count][1]]=' ';

                  if(ditu[coordinate[0][0]][coordinate[0][1]]=='o')

                  {

                          count++;

                         

                  }

                  ditu[coordinate[count][0]][coordinate[count][1]]=' ';

                  for(i=count;i>0;i--)

                  {

                          coordinate[i][0]=coordinate[i-1][0];                               //每一点的坐标

                          coordinate[i][1]=coordinate[i-1][1];                              

                  }

                 

                  coordinate[0][0]+=1;

                  ditu[coordinate[0][0]][coordinate[0][1]]='*';

                  if(count>count_test)

                  {

                  //     ditu[time_x][time_y]='*';

                          ditu[coordinate[count][0]][coordinate[count][1]]='-';                                                                    

                          srand(time(NULL));

                          time_x=rand()%11;

                          time_y=(rand()*2/5+6)%21;

                          ditu[time_x][time_y]='o';

                          count_test=count;

                         

                         

                         

                  }

        

                 

                 

                 

        

//*********************************************************************************

        

        

         ditu[time_x][time_y]='o';

         system("cls");

         map_print();

                 

         }      

}

 

 

void left_go()

{

         while(1)

         {

         for(i=0;i<20;i++)

         {

                  Sleep(20);

                  if(kbhit())

                          {

                                   anniu=getch();

                                   break;

                          }      

         }

         if(anniu!='a')

         break;     

        

 

//******************************************************************************

         //先把尾巴去掉,然后判断吃了虫子没,吃了就张一根之后循环把每一个前面的值都附在后面的点上,然后脑袋往前走,然后给脑袋加个形状,

         //     ditu[coordinate[count][0]][coordinate[count][1]]=' ';

        

        

                  if(ditu[coordinate[0][0]][coordinate[0][1]]=='o')

                  {

                          count++;

                 

                         

                  }

                  ditu[coordinate[count][0]][coordinate[count][1]]=' ';

                  for(i=count;i>0;i--)

                  {

                          coordinate[i][0]=coordinate[i-1][0];                               //每一点的坐标

                          coordinate[i][1]=coordinate[i-1][1];                              

                  }

                 

                  coordinate[0][1]-=1;

                  ditu[coordinate[0][0]][coordinate[0][1]]='*';

                 

                  if(count>count_test)

                  {//    ditu[time_x][time_y]='*';

                          ditu[coordinate[count][0]][coordinate[count][1]]='-';

                          srand(time(NULL));

                          time_x=rand()%11;

                          time_y=(rand()*2/5+6)%21;

                          ditu[time_x][time_y]='o';

                          count_test=count;

                  }

                 

                 

                 

        

//******************************************************************************

        

         ditu[time_x][time_y]='o';

 

         system("cls");

         map_print();

         }      

}

 

 

void right_go()

{

         while(1)

         {

         for(i=0;i<20;i++)

         {

                  Sleep(20);

                  if(kbhit())

                          {

                                   anniu=getch();

                                   break;

                          }      

         }

         if(anniu!='d')

         break;

        

 

//******************************************************************************

        

//ditu[coordinate[count][0]][coordinate[count][1]]=' ';

if(ditu[coordinate[0][0]][coordinate[0][1]]=='o')

                  {

                          count++;

                  }

                  ditu[coordinate[count][0]][coordinate[count][1]]=' ';

                  for(i=count;i>0;i--)

                  {

                          coordinate[i][0]=coordinate[i-1][0];                               //每一点的坐标

                          coordinate[i][1]=coordinate[i-1][1];                              

                  }

                 

                  coordinate[0][1]+=1;

                  ditu[coordinate[0][0]][coordinate[0][1]]='*';

                 

                  if(count>count_test)

                  {      

         //              ditu[time_x][time_y]='*';

                          ditu[coordinate[count][0]][coordinate[count][1]]='-';

                          srand(time(NULL));

                          time_x=rand()%11;

                          time_y=(rand()*2/5+6)%21;

                          ditu[time_x][time_y]='o';

                          count_test=count;

                  }

                 

                 

                 

                 

        

        

//******************************************************************************

 

 

         ditu[time_x][time_y]='o';

         system("cls");

         map_print();

         }      

}

//******************************************************************************上下左右移动代码************************************************************************************

 

posted @ 2017-11-24 12:26  想成为黑客  阅读(263)  评论(0编辑  收藏  举报