C++实践 极简版本贪吃蛇小游戏

来源:互联网 发布:nginx的反向代理配置 编辑:程序博客网 时间:2024/06/03 20:13

借鉴了很多网上大神们的代码,给了我思路和制作方法。

这个版本是最为简单的贪吃蛇,单纯在运行框中运行。最为基础的。

这个版本还有一个问题,由于每次都要刷新屏幕,结果屏幕一闪一闪的,特别不舒服。现在正在努力改进中


#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
int i,j,dengji,speed,grade,fenshu,head,weiba;
void shuaxin(char ditu[22][22])
{
    system("cls");
    for(i=0;i<22;i++)
    {
       cout<<"  ";
       for(j=0;j<22;j++)

           cout<<ditu[i][j]<<" ";
           if(i==0)
             cout<<"你现在的等级为:"<<dengji;
           if(i==3)
              cout<<"自动前进时间间隔为"<<speed;
           if(i==6)
               cout<<"你的得分为"<<fenshu;


       cout<<endl;
    }
}
int main()
{
    //地图绘制
      //这一部分是地图的构建
    char ditu[22][22];     //  贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
    int i,j;
    for(i=1;i<=20;i++)
        for(j=1;j<=20;j++)
            ditu[i][j]=' ';     //     初始化贪吃蛇棋盘中间空白部分
    for(i=0;i<=20;i++)
        ditu[0][i] = ditu[21][i] = '-';      //初始化贪吃蛇棋盘上下墙壁
    for(i=1;i<=28;i++)
        ditu[i][0] = ditu[i][21] = '|';      //初始化贪吃蛇棋盘左右墙壁



    //蛇的绘制
    int x_she[100],y_she[100];//分别代表x和y的坐标
    for(i=0;i<4;i++)  //初始长度为4
    {
        x_she[i]=1;//都在第一行
        y_she[i]=i+1; //横着排
    }
        head=3;
        weiba=0;//头和尾巴的位置,方便以后进行移动!!!!
        for(i=1;i<=3;i++)
            ditu[1][i]='*';  //以下两行则是在地图上实现蛇的初始位置
        ditu[1][4]='#';


    //食物的自动生成
    int x1,y1;
    srand(time(0));//产生随机数
    do
    {
        x1=rand()%22+1;   //随机产生的数对地图取余再加1;
        y1=rand()%22+1;

    }while(ditu[x1][y1]!=' ');  //直到合理位置
    ditu[x1][y1]='*';  //在地图上表现;

    //开始
     int speed;
     cout<<"请选择难度\n1:简单\n2:中等\n3:困难\n4:炼狱\n";
     cin>>speed;
     if(speed==1)
        speed=100;
     else if(speed==2)
        speed=400;
     else if(speed==3)
        speed=200;
     else if(speed==4)
        speed=1;
     cout<<"\n\n\t\t贪吃蛇游戏即将开始 !"<<endl;
     long start;
     int dengji=1;//初始等级
     int length=4;//初始长度
     //开始倒计时
     for(i=3;i>=0;i--)//i就是倒计时
     {
         start=clock();
         while(clock()-start<=1000);  //刷新页面的倒计时
              system("cls");
         if(i>0)
            cout<<"\n\n\t\t进入倒计时:"<<i<<endl;
         else
            shuaxin(ditu); //刷新页面,打印地图
     }
     /////////////////////正式开始操作
     int timeover;
     char direction=77;//默认向右出发
     int x,y;
     while(1)
     {
         timeover=1;
         start=clock(); //随机产生
          while((timeover=(clock()-start<=speed))&&!kbhit());//时间超过了游戏速度,则自动前进
          if(timeover)
          {
              getch(); //不需要回车键的输入,用来操作游戏
              direction=getch();//!!一定要赋值
          }
          switch(direction)
          {
              case 72: x= x_she[head]-1; y= y_she[head];break;     // 向上
              case 80: x= x_she[head]+1; y= y_she[head];break;      // 向下
              case 75: x= x_she[head]; y= y_she[head]-1;break;      // 向左
              case 77: x= x_she[head]; y= y_she[head]+1;      //默认向右
          }

          if(!(direction==72||direction==80||direction==75 ||direction==77))
           {   //  按键非方向键
                 cout << "\tGame over!" << endl;return 0;
           }
           if(x==0 || x==21 ||y==0 || y==21)
            {   // 碰到墙壁
                cout << "\tGame over!" << endl;return 0;
            }
           if(ditu[x][y]!=' '&&!(x==x1&&y==y1))
            { //   蛇头碰到蛇身
                cout << "\tGame over!" << endl;return 0;
            }
            if(x==x1&&y==y1) //吃到了食物
            {

                fenshu++;
                length++;
                if(length>=8)
                {
                    length=length-8;
                    if(speed>=200)
                            speed=550-grade*50;
                }
                ditu[x][y]='#';//食物那个位置变成了头!
                ditu[x_she[head]][y_she[head]]='*';//原来头的位置变成了身子
                head=(head+1)%100; //头的位置刷新一下
                x_she[head]=x;
                y_she[head]=y;
                do  //在产生一个食物,方便刷新地图时一起出现
                {
                     x1=rand()%20+1;
                     y1=rand()%20+1;
                 }while(ditu[x1][y1]!=' ');

                 ditu[x1][y1]='*';
                 shuaxin(ditu);
             }
             else//没有吃到米,这个时很重要很重要的一部分.解答一个疑惑,很多人不明白为什么
                //转了弯,身子会沿着头的方向走,下面就是答案。因为头就是下一个的身子,一个连着一个
                //顺序不会变
             {
                 ditu[x_she[weiba]][y_she[weiba]]=' '; //前进了一步
                 weiba=(weiba+1)%100; //刷新尾巴
                ditu[x_she[head]][y_she[head]]='*'; //头变成了身子
                 head=(head+1)%100; //刷新尾巴
                 x_she[head]=x;//更新头
                 y_she[head]=y;
                 ditu[x_she[head]][y_she[head]]='#';
                 shuaxin(ditu);

             }

        }



}


0 0
原创粉丝点击