[Plain Text] 俄罗斯方块进阶版 →→→→→进入此内容的聊天室

来自 霍才华, 2024-03-15, 写在 Plain Text, 查看 13 次.
URL http://www.code666.cn/view/882c31e5
  1. #include<iostream>
  2. #include<math.h>
  3. #include<Windows.h>
  4. #include<conio.h>
  5. #include<ctime>
  6. using namespace std;
  7.  
  8. enum DIR
  9. {
  10.         UP,
  11.         RIGHT,
  12.         DOWN,
  13.         LEFT
  14. };
  15.  
  16. time_t start = 0, finish = 0;
  17.  
  18. int _x = 6, _y = 1;//图形生成位置
  19.  
  20. int map[30][16] = { 0 };
  21.  
  22. int sharp[20][8] = {
  23.         {0,0,0,0,0,0,0,0},
  24.         //I形
  25.         {0,0,0,1,0,2,0,3},
  26.         {0,0,1,0,2,0,3,0},
  27.         //■形
  28.         {0,0,1,0,0,1,1,1},
  29.         //L形
  30.         {0,0,0,1,0,2,1,2},
  31.         {0,0,0,1,1,0,2,0},
  32.         {0,0,1,0,1,1,1,2},
  33.         {0,1,1,1,2,0,2,1},
  34.         //J形
  35.         {0,2,1,0,1,1,1,2},
  36.         {0,0,0,1,1,1,2,1},
  37.         {0,0,0,1,0,2,1,0},
  38.         {0,0,1,0,2,0,2,1},
  39.         //Z形
  40.         {0,0,1,0,1,1,2,1},
  41.         {0,1,0,2,1,0,1,1},
  42.         //S形
  43.         {0,1,1,0,1,1,2,0},
  44.         {0,0,0,1,1,1,1,2},
  45.         //T形
  46.         {0,1,1,0,1,1,2,1},
  47.         {0,0,0,1,0,2,1,1},
  48.         {0,0,1,0,1,1,2,0},
  49.         {0,1,1,0,1,1,1,2}
  50. };
  51.  
  52.  
  53. class Game
  54. {
  55. public:
  56.         int score;//游戏分数
  57.         int _id;//图形编号
  58.         int top;//最高点高度
  59.         int speed;//下落速度
  60.  
  61.         Game();
  62.         void showMenu();//显示菜单
  63.         void showGround();//显示游戏界面
  64.         void gameOver();//游戏结束界面
  65.         void Run();//运行游戏
  66.         void sharpDraw(int id, bool show = false);//绘制图形
  67.         void keyControl();//键盘控制
  68.         bool move(int dir, int id);//移动判断
  69.         bool downSet(int id);//下落
  70.         void Turn(int id);//旋转
  71.         void clean();//消行
  72. };
  73.  
  74. void SetPos(int i, int j)//控制光标位置, 列, 行
  75. {
  76.         COORD pos = { i,j };
  77.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  78. }
  79.  
  80. int main()
  81. {
  82.         CONSOLE_CURSOR_INFO cursor;
  83.         GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
  84.         cursor.bVisible = 0;    //这四行用来设置光标不显示
  85.         SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
  86.  
  87.         srand((unsigned)time(NULL));
  88.  
  89.         Game game;
  90.         game.showMenu();
  91.         return 0;
  92. }
  93.  
  94. Game::Game()
  95. {
  96.         score = 0;
  97.         _id = 0;
  98.         top = 58;
  99.         speed = 1000;
  100. }
  101.  
  102. void Game::showMenu()
  103. {
  104.         for (int i = 0; i < 30; i++)
  105.         {
  106.                 for (int j = 0; j < 26; j++)
  107.                 {
  108.                         if ((i == 0 || i == 29) || (j == 0 || j == 25))
  109.                         {
  110.                                 cout << "■";
  111.                         }
  112.                         else
  113.                         {
  114.                                 cout << "  ";
  115.                         }
  116.                 }
  117.                 cout << endl;
  118.         }
  119.  
  120.         SetPos(17, 8);
  121.         cout << "俄 罗 斯 方 块" << endl;
  122.         SetPos(13, 12);
  123.         cout << "↑旋转方块  ↓加速下滑" << endl;
  124.         SetPos(12, 14);
  125.         cout << "← →左右移动  空格  暂停" << endl;
  126.         SetPos(15, 20);
  127.         cout << "0 退出  Enter 开始" << endl;
  128.  
  129.         while (1)
  130.         {
  131.                 int select = _getch();
  132.                 if (select == 13)
  133.                 {
  134.                         system("cls");
  135.                         this->Run();
  136.                 }
  137.                 else if (select = 48)
  138.                 {
  139.                         system("cls");
  140.                         exit(0);
  141.                 }
  142.         }
  143. }
  144.  
  145. void Game::showGround()
  146. {
  147.         for (int i = 0; i < 30; i++)
  148.         {
  149.                 for (int j = 0; j < 26; j++)
  150.                 {
  151.                         if ((i == 0 || i == 29) || (j == 0 || j == 25 || j == 15))
  152.                         {
  153.                                 cout << "■";
  154.                         }
  155.                         else if (i == 15 && j > 15)
  156.                         {
  157.                                 cout << "■";
  158.                         }
  159.                         else
  160.                         {
  161.                                 cout << "  ";
  162.                         }
  163.                 }
  164.                 cout << endl;
  165.         }
  166.  
  167.         SetPos(31, 2);
  168.         cout << "下 个图形" << endl;
  169.         SetPos(31, 17);
  170.         cout << "当 前得分" << endl;
  171.  
  172.         for (int i = 0; i < 30; i++)
  173.         {
  174.                 for (int j = 0; j < 16; j++)
  175.                 {
  176.                         if ((i == 0 || i == 29) || (j == 0 || j == 15))
  177.                         {
  178.                                 map[i][j] = 1;
  179.                         }
  180.                         else
  181.                         {
  182.                                 map[i][j] = 0;
  183.                         }
  184.                 }
  185.         }
  186. }
  187.  
  188. void Game::gameOver()
  189. {
  190.         for (int i = 5; i < 15; i++)
  191.         {
  192.                 SetPos(1, i);
  193.                 cout << "                            " << endl;
  194.         }
  195.  
  196.         SetPos(8, 7);
  197.         cout << "G a m e   O v e r" << endl;
  198.  
  199.         SetPos(3, 10);
  200.         cout << "0 退出   Enter 重新开始" << endl;
  201.  
  202.         while (1)
  203.         {
  204.                 int select = _getch();
  205.                 if (select == 13)
  206.                 {
  207.                         system("cls");
  208.                         this->Run();
  209.                 }
  210.                 else if (select == 48)
  211.                 {
  212.                         system("cls");
  213.                         exit(0);
  214.                 }
  215.         }
  216.  
  217. }
  218.  
  219. void Game::Run()
  220. {
  221.         score = 0;
  222.         _id = 0;
  223.         top = 58;
  224.         _x = 6;
  225.         _y = 1;
  226.         showGround();
  227.         start = clock();
  228.         int new_id = rand() % 19 + 1;
  229.  
  230.         while (1)
  231.         {
  232.                 sharpDraw(_id);
  233.                 keyControl();
  234.  
  235.                 if (downSet(_id))
  236.                 {
  237.                         sharpDraw(-new_id, 1);
  238.                         _id = new_id;
  239.                         new_id = rand() % 19 + 1;
  240.                         sharpDraw(new_id, 1);
  241.                         clean();
  242.                 }
  243.  
  244.                 SetPos(34, 20);
  245.                 cout << score << endl;
  246.         }
  247. }
  248.  
  249. void Game::sharpDraw(int id, bool show)
  250. {
  251.         int x, y;
  252.  
  253.         if (show == true)
  254.         {
  255.                 if (id > 0)
  256.                 {
  257.                         for (int i = 0; i < 4; i++)
  258.                         {
  259.                                 x = 19 + sharp[id][2 * i];
  260.                                 y = 6 + sharp[id][2 * i + 1];
  261.                                 SetPos(2 * x, y);
  262.                                 cout << "■";
  263.                         }
  264.                 }
  265.                 else
  266.                 {
  267.                         for (int i = 0; i < 4; i++)
  268.                         {
  269.                                 x = 19 + sharp[-id][2 * i];
  270.                                 y = 6 + sharp[-id][2 * i + 1];
  271.                                 SetPos(2 * x, y);
  272.                                 cout << "  ";
  273.                         }
  274.                 }
  275.                 return;
  276.         }
  277.  
  278.  
  279.         if (id > 0)
  280.         {
  281.                 for (int i = 0; i < 4; i++)
  282.                 {
  283.                         x = _x + sharp[id][2 * i];
  284.                         y = _y + sharp[id][2 * i + 1];
  285.                         SetPos(2 * x, y);
  286.                         cout << "■";
  287.                 }
  288.         }
  289.         else
  290.         {
  291.                 for (int i = 0; i < 4; i++)
  292.                 {
  293.                         x = _x + sharp[-id][2 * i];
  294.                         y = _y + sharp[-id][2 * i + 1];
  295.                         SetPos(2 * x, y);
  296.                         cout << "  ";
  297.                 }
  298.         }
  299.         return;
  300.  
  301. }
  302.  
  303. bool Game::downSet(int id)
  304. {
  305.         if (id == 0)
  306.                 return true;
  307.  
  308.         finish = clock();
  309.  
  310.         if (finish - start < speed)
  311.         {
  312.                 return false;
  313.         }
  314.  
  315.         start = clock();
  316.  
  317.         if (!move(DOWN, _id))
  318.         {
  319.                 int x, y;
  320.                 for (int i = 0; i < 4; i++)
  321.                 {
  322.                         x = _x + sharp[id][2 * i];
  323.                         y = _y + sharp[id][2 * i + 1];
  324.                         map[y][x] = 1;
  325.  
  326.                         if (y < top)
  327.                         {
  328.                                 top = y;
  329.                         }
  330.                         if (top <= 1)
  331.                         {
  332.                                 gameOver();
  333.                         }
  334.                 }
  335.                 _x = 6;
  336.                 _y = 1;
  337.                 return true;
  338.         }
  339.  
  340.         sharpDraw(-id);
  341.         _y++;
  342.         sharpDraw(id);
  343.         return false;
  344. }
  345.  
  346. bool Game::move(int dir, int id)
  347. {
  348.         int x, y;
  349.         switch (dir)
  350.         {
  351.         case UP:
  352.                 for (int i = 0; i < 4; i++)
  353.                 {
  354.                         x = _x + sharp[id][2 * i];
  355.                         y = _y + sharp[id][2 * i + 1];
  356.                         if (map[y][x] == 1)
  357.                         {
  358.                                 return false;
  359.                         }
  360.                 }
  361.                 break;
  362.         case DOWN:
  363.         {
  364.                 for (int i = 0; i < 4; i++)
  365.                 {
  366.                         x = _x + sharp[id][2 * i];
  367.                         y = _y + sharp[id][2 * i + 1];
  368.                         if (map[y + 1][x] == 1)
  369.                         {
  370.                                 return false;
  371.                         }
  372.                 }
  373.         }
  374.         break;
  375.         case RIGHT:
  376.         {
  377.                 for (int i = 0; i < 4; i++)
  378.                 {
  379.                         x = _x + sharp[id][2 * i];
  380.                         y = _y + sharp[id][2 * i + 1];
  381.                         if (map[y][x + 1] == 1)
  382.                         {
  383.                                 return false;
  384.                         }
  385.                 }
  386.         }
  387.         break;
  388.         case LEFT:
  389.         {
  390.                 for (int i = 0; i < 4; i++)
  391.                 {
  392.                         x = _x + sharp[id][2 * i];
  393.                         y = _y + sharp[id][2 * i + 1];
  394.                         if (map[y][x - 1] == 1)
  395.                         {
  396.                                 return false;
  397.                         }
  398.                 }
  399.         }
  400.         break;
  401.         default:
  402.                 break;
  403.         }
  404.         return true;
  405. }
  406.  
  407. void Game::Turn(int id)
  408. {
  409.         switch (id)
  410.         {
  411.         case 1:id++; break;
  412.         case 2:id--; break;
  413.  
  414.         case 3: break;
  415.  
  416.         case 4:id++; break;
  417.         case 5:id++; break;
  418.         case 6:id++; break;
  419.         case 7:id -= 3; break;
  420.  
  421.         case 8:id++; break;
  422.         case 9:id++; break;
  423.         case 10:id++; break;
  424.         case 11:id -= 3; break;
  425.  
  426.         case 12:id++; break;
  427.         case 13:id--; break;
  428.  
  429.         case 14:id++; break;
  430.         case 15:id--; break;
  431.  
  432.         case 16:id++; break;
  433.         case 17:id++; break;
  434.         case 18:id++; break;
  435.         case 19:id -= 3; break;
  436.  
  437.         default:
  438.                 break;
  439.         }
  440.  
  441.         if (!move(UP, id))
  442.         {
  443.                 return;
  444.         }
  445.  
  446.         sharpDraw(-_id);
  447.         _id = id;
  448. }
  449.  
  450. void Game::keyControl()
  451. {
  452.         if (!_kbhit())
  453.                 return;
  454.  
  455.         int key = _getch();
  456.  
  457.         switch (key)
  458.         {
  459.         case 72:
  460.                 Turn(_id);
  461.                 break;
  462.         case 80:
  463.                 if (move(DOWN, _id))
  464.                 {
  465.                         sharpDraw(-_id);
  466.                         _y++;
  467.                 }
  468.                 break;
  469.         case 75:
  470.                 if (move(LEFT, _id))
  471.                 {
  472.                         sharpDraw(-_id);
  473.                         _x--;
  474.                 }
  475.                 break;
  476.         case 77:
  477.                 if (move(RIGHT, _id))
  478.                 {
  479.                         sharpDraw(-_id);
  480.                         _x++;
  481.                 }
  482.                 break;
  483.         case 32:
  484.         {
  485.                 for (int i = 5; i < 15; i++)
  486.                 {
  487.                         SetPos(1, i);
  488.                         cout << "                            " << endl;
  489.                 }
  490.  
  491.                 SetPos(10, 7);
  492.                 cout << "游 戏 暂 停" << endl;
  493.  
  494.                 SetPos(3, 10);
  495.                 cout << "0 返回菜单  回车 继续游戏" << endl;
  496.  
  497.                 while (1)
  498.                 {
  499.                         int select = _getch();
  500.  
  501.                         if (select == 13)
  502.                         {
  503.                                 for (int i = 5; i < 15; i++)
  504.                                 {
  505.                                         SetPos(1, i);
  506.                                         cout << "                            " << endl;
  507.                                 }
  508.                                 break;
  509.                         }
  510.                         else if (select == 48)
  511.                         {
  512.                                 system("cls");
  513.                                 showMenu();
  514.                         }
  515.                 }
  516.  
  517.         }
  518.         default:
  519.                 break;
  520.         }
  521. }
  522.  
  523. void Game::clean()
  524. {
  525.         int n = -1;
  526.         int line = -1;
  527.         while (1)
  528.         {
  529.                 for (int i = 28; i > 0; i--)
  530.                 {
  531.                         for (int j = 1; j < 15; j++)
  532.                         {
  533.                                 line = i;
  534.                                 if (map[i][j] == 0)
  535.                                 {
  536.                                         line = -1;
  537.                                         break;
  538.                                 }
  539.                         }
  540.                         if (line != -1)
  541.                                 break;
  542.                 }
  543.  
  544.                 if (line == -1)
  545.                         break;
  546.  
  547.                 for (int i = line; i > 0; i--)
  548.                 {
  549.                         for (int j = 1; j < 15; j++)
  550.                         {
  551.                                 if (i == 1)
  552.                                         map[i][j] = 0;
  553.                                 else
  554.                                 {
  555.                                         map[i][j] = map[i - 1][j];
  556.                                         SetPos(2 * j, i);
  557.                                         if (map[i][j] == 1)
  558.                                                 cout << "■";
  559.                                         else
  560.                                                 cout << "  ";
  561.                                 }
  562.                         }
  563.                 }
  564.                 top++;
  565.                 n++;
  566.         }
  567.  
  568.         if (n >= 0)
  569.         {
  570.                 score += n * n * 100 + 100;
  571.                 if (speed > 100)
  572.                         speed = 1000 - score / 10;
  573.         }
  574. }

回复 俄罗斯方块进阶版 rss

标题 提交人 语言 时间
Re: 俄罗斯方块进阶版 霍才华 text 2 月 前.

回复 "俄罗斯方块进阶版"

这儿你可以回复上面这条便签

captcha