贪吃蛇 c++

来源:互联网 发布:linux查看jdk版本 编辑:程序博客网 时间:2024/06/17 13:44

    各位大神帮我改一下,谢谢。

  1. #include <bits/stdc++.h>
    #include <windows.h>
    #include <time.h>
    #include <conio.h>
    using namespace std;
    struct in{
    int x,y; //蛇身 
    };
    char q[19][22]={'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',
    '#','*','*','*','O',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',
    '#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'};
    //初始化地图 
    void start(int times)
    {
    int d=0,j=0;
    int x1=9,y1=9;//豆子
    int lenth = 3 ,x=1,y=4;//蛇身,蛇头 
    int number=0;//吃豆的数量 
    in a[340]; //蛇身最大值 
    for(int i=0;i<3;i++)
    {
    a[i].x=1;
    a[i].y=i+1;
    }
    system("cls");
    cout<<"得分"<<d<<endl;
    cout<<"等级"<<j<<endl;
    cout<<""<<times<<"毫秒移动一次"<<endl; 
    for(int i=0;i<19;i++)
    {
    for(int j=0;j<22;j++)
    {

    cout<<q[i][j];
    }
    cout<<endl;
    }
    int n , num ;
    char ch='d'; //默认向右
    while(1)
    {
    n = clock();
    while(num=(clock()-n<=times));
    if(kbhit())
    {
    ch=getch();
    }




    if(x==x1 && y==y1) //吃米
    {
    lenth++;
    q[x][y]='*';
    number++;
    if(number==4)
    {
    number=0;
    j++;
    times=times-j*50;
    }

    do
    {
    srand(time(0));
    x1 = rand()%16+1;
    y1 = rand()%20+1; //刷米 
    }
    while(q[x1][y1]!=' ');
    d+=100;
    }
    else //不吃米 
    {
    q[a[0].x][a[0].y]=' ';
    for(int i=0;i<lenth;i++)
    {
    swap(a[i],a[i+1]);
    }
    a[lenth-1].x=x;
    a[lenth-1].y=y;
    }
    for(int i=0;i<lenth;i++)
    {
    q[a[i].x][a[i].y]='*';
    }
    q[x][y]='*';
    q[x1][y1]='*';
    switch(ch)
    {
    case 'd': y++;break ;
    case 'a': y--;break ;
    case 'w': x--;break ;
    case 's': x++;break ;
    }
    q[x][y]='O';
    if((x==0 || x==18 || y==0 || y==21 )&& 
    (ch!='a' && ch!='d'&&ch!='w' && ch!= 's')  )
    {
    system("cls");
    cout<<"\t\tgame over";
    return ;
    }
    else 
    {
    for(int i=0;i<lenth;i++)
    {
    if(x==a[i].x && y== a[i].y)
    {
    system("cls");
    cout<<"\t\tgame over";
    return ;
    }
    }
    }
    system("cls");
    cout<<"得分"<<d<<endl;
    cout<<"等级"<<j<<endl;
    cout<<""<<times<<"毫秒移动一次"<<endl; 
    for(int i=0;i<19;i++)
    {
    for(int j=0;j<22;j++)
    {
    cout<<q[i][j];
    }
    cout<<endl;
    }
      }
  2. int main()
  3. {
  4. system("color f4");
    cout<<"\n\n\t\t\t游戏即将开始!";
    Sleep(500);
    for(int i=3;i>=0;i--)
    {
    system("cls");
    cout<<"\n\n\t\t\t\t倒计时:"<<i;
    Sleep(1000);
    }
    start(500);
    return 0;
    }
  5.   
  6.    

  

原创粉丝点击