• 首页
  • 资讯
  • 专家
  • 话题
  • 问题库
  • 礼品商店
  • 分类
    • 新闻资讯
    • 娱乐八卦
    • 3C数码
    • 医疗健康
    • 健康养生
    • 云盘解析
    首页   ›   正文

C++贪吃蛇编程代码

2021-12-23 11:33
1301  1
IT网络 未结
关注
遥遥妈888
遥遥妈888 2021-12-23 11:33
相关标签:
1条回答
  • 树友ll9aon
    2021-12-23 12:08

    这个可参考的网上很多的,我收藏的一个参考:

    #include
    #include
    #include
    #include
    using namespace std;
    // 刷新当前屏幕
    inline void Refresh(char q[][22], int grade, int gamespeed){
       system("cls");     //  清屏
    int i,j;
    cout << endl;
    for(i=0;i<22;i++){
        cout << "\t";
     for(j=0;j<22;j++)
      cout<        if(i==0) cout << "\t等级为:" << grade;
           if(i==4) cout << "\t自动前进时间";
           if(i==6) cout << "\t间隔为:" << gamespeed << "ms";
     cout< }
    }

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

       int tcsZuobiao[2][100];     //蛇的坐标数组
       for(i=0; i<4; i++){
           tcsZuobiao[0][i] = 1;
           tcsZuobiao[1][i] = i + 1;
       }
       int head = 3,tail = 0;

       for(i=1;i<=3;i++)
           tcsQipan[1][i]='*';    //蛇身
       tcsQipan[1][4]='#';       //蛇头

       int x1, y1;           // 随机出米
       srand(time(0));
       do{
     x1=rand()%20+1;
     y1=rand()%20+1;
    }while(tcsQipan[x1][y1]!=' ');
    tcsQipan[x1][y1]='*';

    cout<<"\n\n\t\t贪吃蛇游戏即将开始 !"< long start;

       int grade = 1, length = 4;
       int gamespeed = 500;  //前进时间间隔
    for(i=3;i>=0;i--){
     start=clock();
     while(clock()-start<=1000);
     system("cls");
     if(i>0)
      cout << "\n\n\t\t进入倒计时:" << i << endl;
     else
      Refresh(tcsQipan,grade,gamespeed);
    }

       int timeover;
       char direction = 77;  // 初始情况下,向右运动
       int x,y;
       while(1){
           timeover = 1;
           start = clock();
           while((timeover=(clock()-start<=gamespeed))&&!kbhit());
                //如果有键按下或时间超过自动前进时间间隔则终止循环
           if(timeover){
               getch();direction = getch();
           }
           switch(direction){
           case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break;     // 向上
           case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break;      // 向下
           case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break;      // 向左
           case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][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(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ //   蛇头碰到蛇身
               cout << "\tGame over!" << endl;return 0;
           }

           if(x==x1 && y==y1){      //  吃米,长度加1
               length ++;
               if(length>=8){
                   length -= 8;
                   grade ++;
                   if(gamespeed>=200)
                       gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
               }
               tcsQipan[x][y]= '#';
               tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
               head = (head+1)%100;
               tcsZuobiao[0][head] = x;
               tcsZuobiao[1][head] = y;
               do
               {
                   x1=rand()%20+1;
                   y1=rand()%20+1;
               }while(tcsQipan[x1][y1]!=' ');
               tcsQipan[x1][y1]='*';
               Refresh(tcsQipan,grade,gamespeed);
           }
           else{       //  不吃米
               tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';
               tail=(tail+1)%100;
               tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';
               head=(head+1)%100;
               tcsZuobiao[0][head]=x;
               tcsZuobiao[1][head]=y;
               tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';
               Refresh(tcsQipan,grade,gamespeed);
           }
       }
       return 0;
    }


    0
 看不清?
提交回复

如本站内容“对您有用”,欢迎随意打赏,让我们持续更新!

打赏
游客
登录后展示个人签名去登录
0文章 0问题 0回答 0点赞
写作
发布问题
发布文章
关注微信
加QQ群
随机文章
张玉环获496万元国家赔偿 张玉环冤案详情披露
2021-12-09 21:20:43
360揭批美国网络攻击:对中国无所不用其极
2022-03-04 08:23:31
小龙虾隔夜了能吃吗 小龙虾隔夜了还能继续吃吗
2022-09-29 22:19:33
一加Ace Pro超12万人预约:黑色版本差点没量产
2022-07-29 09:33:57
老机型流畅运行48个月不卡!一图了解OriginOS 3
2023-03-06 09:35:25
核磁共振检查会伤害身体吗 核磁共振检查注意事项
2022-02-19 08:31:57
2月25日见!Redmi K40系列旗舰发布时间曝光:为老大哥让路?
2021-12-05 21:20:27
果酸换肤好吗 果酸换肤功效与作用
2022-02-19 09:15:00
iPad mini 6更新iPadOS 15.5后无法充电:苹果已着手调查
2022-07-11 20:00:11
北方入汛以来最强降雨来袭:多地或现极端性强降雨、雷暴大风
2022-06-26 11:04:31
iPad Pro支持Wi-Fi 6E:国行版无缘
2023-03-27 09:27:41
土狗被扔50公里外一星期后跑回家 狗狗是如何认路的?靠三点
2023-05-16 09:45:20
豆瓣9.4分 刘慈欣《三体》英文版提前续约卖出800万元
2022-02-19 09:17:02
女王驾到 《黑袍纠察队》幕后故事
2022-06-11 09:54:49
黑芝麻糊的功效与作用分别是什么 黑芝麻糊的功效有哪些
2021-12-18 07:43:33
热门标签
智能手机为何越卖越贵 马斯克加速抢滩一文了解脑机接口 凛冬将至苹果还能靠iphone支撑多久 新野蛮人马斯克 630wrtx 4090解锁极限功耗 马斯克没有打价格战的基因 ipad营收锐减卖不动了 小米13 lite现身小米13家族最便宜版本 感受守护网络安全的黑科技app 感受守护网络安全的黑科技 新一代影像性能旗舰努比亚z50官宣 黑亚当纽约首映式 黑亚当创下巨石强森个人最好成绩 超人归来电影免费观看 超人回归dc 超人回归 贝鲁奇谈贝鲁奇 辐射剧集首张剧照 大超与白狼不可兼得 用废品造的iphone你会买单吗
Copyright © 2025 网站备案号: 闽ICP备2020021158号-10 本站所有信息来自于互联网或网友上传,如有侵权,敬请来信联系我们,1494738443@qq.com 我们立刻删除。
responsive_hankin 主题. Designed by 极速问答社区
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付