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

C++贪吃蛇编程代码

2021-12-23 11:33
1286  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群370431002
随机文章
今日疫情最新消息数据:31省区市新增确诊病例16例 其中7例为本土病例
2021-12-05 21:57:03
喝什么东西解酒 喝什么解酒快有效
2021-12-05 22:55:03
吉利公布7nm、5nm车规芯片:7nm明年量产
2021-12-09 21:30:10
小品《你好李焕英》催泪预告上线 你好李焕英是真实的吗结局又是怎样
2021-12-05 21:43:26
襄阳7岁失踪女童被翻墙逃走邻居杀害
2021-12-09 21:39:28
iOS 15.4支持戴口罩解锁 实测仅支持iPhone 12/13!速度超快 支付也能用
2022-02-24 17:46:38
6岁女孩常玩购物小票导致性早熟?热敏纸还能碰吗?
2022-02-19 09:13:45
特赞Tezign宣布完成淡马锡领投的C2轮融资 C轮累计融资1亿美元
2021-12-06 19:52:11
茯神的功效与作用及食用方法 茯神的功效与作用
2022-02-19 09:21:34
iPhone 14系列除国行版美版外都涨价了!背后原因揭开
2023-03-06 09:33:14
中考体育将达到和语数外同等地位 以后小胖子升学难度大了
2021-12-05 22:52:32
两人在微信群里“对骂”14个月闹上法庭:被判群内公开手写道歉信
2023-05-28 09:17:56
女子结婚后第一次回丈夫老家过年 进村后一幕让她当场慌了
2021-12-09 21:01:47
痔疮吃什么好 痔疮患者日常要注意什么
2021-12-07 19:36:31
华为首款1亿像素手机!华为nova 9 SE开售:2199元起
2022-03-19 08:27:22
热门标签
智能手机为何越卖越贵 马斯克加速抢滩一文了解脑机接口 凛冬将至苹果还能靠iphone支撑多久 新野蛮人马斯克 630wrtx 4090解锁极限功耗 马斯克没有打价格战的基因 ipad营收锐减卖不动了 小米13 lite现身小米13家族最便宜版本 感受守护网络安全的黑科技app 感受守护网络安全的黑科技 新一代影像性能旗舰努比亚z50官宣 黑亚当纽约首映式 黑亚当创下巨石强森个人最好成绩 超人归来电影免费观看 超人回归dc 超人回归 贝鲁奇谈贝鲁奇 辐射剧集首张剧照 大超与白狼不可兼得 用废品造的iphone你会买单吗
Copyright © 2025 网站备案号: 闽ICP备2020021158号-10 本站所有信息来自于互联网或网友上传,如有侵权,敬请来信联系我们,1494738443@qq.com 我们立刻删除。
responsive_hankin 主题. Designed by 极速问答社区
赞赏作者

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

感谢您对作者的支持!

 支付宝 微信支付