zoukankan      html  css  js  c++  java
  • 【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(" ");

             }

    }

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

    {

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

             {

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

                      {

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

                      }

                      printf(" ");

             }

    }

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

    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();

             }      

    }

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

    充满金属光泽的脑袋持有者,怎么会因为害怕而放弃挣扎。----------------------------------------------------------------------------------------------------------------------------------------------------------------------- 让暴风雨来的更猛烈些吧!猛烈到撕裂我的灵魂!------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 看看吧,暴风雨...你输了!---------------------------------------------------------------------------------------------------------------------------------
  • 相关阅读:
    01MySQL内核分析-The Skeleton of the Server Code
    debug PostgreSQL 9.6.18 using Eclipse IDE on CentOS7
    开启PG的归档模式
    11G 新特性之 密码延迟认证
    Oracle细粒度审计
    MySQL数据库回表与索引
    zabbix_api的调用(curl测试)和SQL一些解答
    【强化学习】Markov Decision processes【二】
    【强化学习】强化学习的一些基础理念【一】
    Mysql 索引优化分析
  • 原文地址:https://www.cnblogs.com/naraka/p/7890040.html
Copyright © 2011-2022 走看看