451浏览
查看: 451|回复: 6

c++贪吃蛇

[复制链接]
贪吃蛇(c++版)
程序:
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <time.h>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <iostream>
  8. #define  N 22
  9. using namespace std;
  10.     int gameover;
  11.     int x1, y1; // 随机出米
  12.     int x,y;
  13.     long start;
  14. //=======================================
  15. //类的实现与应用initialize
  16. //=======================================
  17. //下面定义贪吃蛇的坐标类
  18. class snake_position
  19. {
  20. public:
  21.     int x,y;      //x表示行,y表示列
  22.     snake_position(){};
  23.     void initialize(int &);//坐标初始化
  24. };
  25. snake_position position[(N-2)*(N-2)+1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标
  26. void snake_position::initialize(int &j)
  27. {
  28.         x = 1;
  29.         y = j;
  30. }
  31. //下面定义贪吃蛇的棋盘图
  32. class snake_map
  33. {
  34. private:
  35.     char s[N][N];//定义贪吃蛇棋盘,包括墙壁。
  36.     int grade, length;
  37.     int gamespeed; //前进时间间隔
  38.     char direction; // 初始情况下,向右运动
  39.     int head,tail;
  40.     int score;
  41.     bool gameauto;
  42. public:
  43.     snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}
  44.     void initialize();   //初始化函数
  45.     void show_game();
  46.     int updata_game();
  47.     void setpoint();
  48.     void getgrade();
  49.     void display();
  50. };
  51. //定义初始化函数,将贪吃蛇的棋盘图进行初始化
  52. void snake_map::initialize()
  53. {
  54.     int i,j;
  55.     for(i=1;i<=3;i++)
  56.         s[1][i] = '*';
  57.     s[1][4] = '#';
  58.     for(i=1;i<=N-2;i++)
  59.         for(j=1;j<=N-2;j++)
  60.             s[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分
  61.     for(i=0;i<=N-1;i++)
  62.         s[0][i] = s[N-1][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
  63.     for(i=1;i<=N-2;i++)
  64.         s[i][0] = s[i][N-1] = '|'; //初始化贪吃蛇棋盘左右墙壁
  65. }
  66. //============================================
  67. //输出贪吃蛇棋盘信息
  68. void snake_map::show_game()
  69. {
  70.     system("cls"); // 清屏
  71.     int i,j;
  72.     cout << endl;
  73.     for(i=0;i<N;i++)
  74.     {
  75.         cout << '\t';
  76.         for(j=0;j<N;j++)
  77.             cout<<s[i][j]<<' '; // 输出贪吃蛇棋盘
  78.         if(i==2) cout << "\t等级:" << grade;
  79.         if(i==6) cout << "\t速度:" << gamespeed;
  80.         if(i==10) cout << "\t得分:" << score << "分" ;
  81.         if(i==14) cout << "\t暂停:按一下空格键" ;
  82.         if(i==18) cout << "\t继续:按两下空格键" ;
  83.         cout<<endl;
  84.     }
  85. }
  86. //输入选择等级
  87. void snake_map::getgrade()
  88. {
  89.     cin>>grade;
  90.     while( grade>7 || grade<1 )
  91.     {
  92.         cout << "请输入数字1-7选择等级,输入其他数字无效" << endl;
  93.         cin >> grade;
  94.     }
  95.     switch(grade)
  96.     {
  97.         case 1: gamespeed = 1000;gameauto = 0;break;
  98.         case 2: gamespeed = 800;gameauto = 0;break;
  99.         case 3: gamespeed = 600;gameauto = 0;break;
  100.         case 4: gamespeed = 400;gameauto = 0;break;
  101.         case 5: gamespeed = 200;gameauto = 0;break;
  102.         case 6: gamespeed = 100;gameauto = 0;break;
  103.         case 7: grade = 1;gamespeed = 1000;gameauto = 1;break;
  104.     }
  105. }
  106. //输出等级,得分情况以及称号
  107. void snake_map::display()
  108. {
  109.     cout << "\n\t\t\t\t等级:" << grade;
  110.     cout << "\n\n\n\t\t\t\t速度:" << gamespeed;
  111.     cout << "\n\n\n\t\t\t\t得分:" << score << "分" ;
  112. }
  113. //随机产生米
  114. void snake_map::setpoint()
  115. {
  116.     srand(time(0));
  117.     do
  118.     {
  119.         x1 = rand() % (N-2) + 1;
  120.         y1 = rand() % (N-2) + 1;
  121.     }while(s[x1][y1]!=' ');
  122.     s[x1][y1]='*';
  123. }
  124. char key;
  125. int snake_map::updata_game()
  126. {
  127.     gameover = 1;
  128.     key = direction;
  129.     start = clock();
  130.     while((gameover=(clock()-start<=gamespeed))&&!kbhit());
  131.     //如果有键按下或时间超过自动前进时间间隔则终止循环
  132.         if(gameover)
  133.         {
  134.             getch();
  135.             key = getch();
  136.         }
  137.         if(key == ' ')
  138.         {
  139.             while(getch()!=' '){};//这里实现的是按空格键暂停,按空格键继续的功能,但不知为何原因,需要按两下空格才能继续。这是个bug。
  140.         }
  141.         else
  142.             direction = key;
  143.         switch(direction)
  144.         {
  145.             case 72: x= position[head].x-1; y= position[head].y;break; // 向上
  146.             case 80: x= position[head].x+1; y= position[head].y;break; // 向下
  147.             case 75: x= position[head].x; y= position[head].y-1;break; // 向左
  148.             case 77: x= position[head].x; y= position[head].y+1; // 向右
  149.         }
  150.         if(!(direction==72||direction==80||direction==75 ||direction==77))   // 按键非方向键
  151.             gameover = 0;
  152.         else if(x==0 || x==N-1 ||y==0 || y==N-1)   // 碰到墙壁
  153.             gameover = 0;
  154.         else if(s[x][y]!=' '&&!(x==x1&&y==y1))    // 蛇头碰到蛇身
  155.             gameover = 0;
  156.         else if(x==x1 && y==y1)
  157.         { // 吃米,长度加1
  158.             length ++;
  159.             if(length>=8 && gameauto)
  160.             {
  161.                 length -= 8;
  162.                 grade ++;
  163.                 if(gamespeed>=200)
  164.                     gamespeed -= 200; // 改变贪吃蛇前进速度
  165.                 else
  166.                     gamespeed = 100;
  167.             }
  168.             s[x][y]= '#';  //更新蛇头
  169.             s[position[head].x][position[head].y] = '*'; //吃米后将原先蛇头变为蛇身
  170.             head = (head+1) % ( (N-2)*(N-2) );   //取蛇头坐标
  171.             position[head].x = x;
  172.             position[head].y = y;
  173.             show_game();
  174.             gameover = 1;
  175.             score += grade*20;  //加分
  176.             setpoint();   //产生米
  177.         }
  178.         else
  179.         { // 不吃米
  180.             s[position[tail].x][position[tail].y]=' ';//将蛇尾置空
  181.             tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾坐标
  182.             s[position[head].x][position[head].y]='*';  //将蛇头更为蛇身
  183.             head= (head+1) % ( (N-2) * (N-2) );
  184.             position[head].x = x;
  185.             position[head].y = y;
  186.             s[position[head].x][position[head].y]='#'; //更新蛇头
  187.             gameover = 1;
  188.         }
  189.     return gameover;
  190. }
  191. //====================================
  192. //主函数部分
  193. //====================================
  194. int main()
  195. {
  196.     char ctn = 'y';
  197.     int nodead;
  198.     cout<<"\n\n\n\n\n\t\t\t 欢迎进入贪吃蛇游戏!"<<endl;//欢迎界面;
  199.     cout<<"\n\n\n\t\t\t 按任意键马上开始。。。"<<endl;//准备开始;;
  200.     getch();
  201.     while( ctn=='y' )
  202.     {
  203.         system("cls"); // 清屏
  204.         snake_map snake;
  205.         snake.initialize();
  206.         cout << "\n\n请输入数字选择游戏等级:" << endl;
  207.         cout << "\n\n\n\t\t\t1.等级一:速度 1000 \n\n\t\t\t2.等级二:速度 800 \n\n\t\t\t3.等级三:速度 600 ";
  208.         cout << "\n\n\t\t\t4.等级四:速度 400 \n\n\t\t\t5.等级五:速度 200 \n\n\t\t\t6.等级六:速度 100 \n\n\t\t\t7.自动升级模式" << endl;
  209.         snake.getgrade();//获取等级
  210.         for(int i=1;i<=4;i++)
  211.         {
  212.             position[i].initialize(i);//初始化坐标
  213.         }
  214.         snake.setpoint();  // 产生第一个米
  215.         do
  216.         {
  217.             snake.show_game();
  218.             nodead = snake.updata_game();
  219.         }while(nodead);
  220.         system("cls"); //清屏
  221.         cout << "\n\n\n\t\t\t\tGameover!\n\n"<<endl;
  222.         snake.display();//输出等级/得分情况
  223.         cout << "\n\n\n\t\t    是否选择继续游戏?输入 y 继续,n 退出" << endl;
  224.         cin >> ctn;
  225.     }
  226.     return 0;
  227. }
复制代码
下载附件五子棋.zip

BGhAVrscx7aD  初级技师

发表于 2024-7-18 16:30:04

回帖奖励 +4 创造力

66666666666666666666666666666666
回复

使用道具 举报

代码满天  初级技师

发表于 2024-7-18 19:40:32

回帖奖励 +4 创造力

为什么压缩包的名字是五子棋?
回复

使用道具 举报

凉皮周  初级技匠

发表于 2024-7-18 21:49:02

回帖奖励 +4 创造力

BGhAVrscx7aD 发表于 2024-7-18 08:30
66666666666666666666666666666666

哇,贞德是泥洼!

c++贪吃蛇图1

回复

使用道具 举报

吴哲源  中级技师
 楼主|

发表于 2024-7-19 11:24:08

代码满天 发表于 2024-7-18 19:40
为什么压缩包的名字是五子棋?

马上改                                             
回复

使用道具 举报

吴哲源  中级技师
 楼主|

发表于 2024-7-19 11:27:38

代码满天 发表于 2024-7-18 19:40
为什么压缩包的名字是五子棋?

发错了,你直接复制代码
回复

使用道具 举报

代码满天  初级技师

发表于 2024-7-19 22:06:56

吴哲源 发表于 2024-7-19 11:27
发错了,你直接复制代码

哦,好的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail