Advertisement
penguin88428

circle_game

Mar 19th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <thread>//-std=c++11
  7. #define play_second 60 //自訂遊玩時間
  8. using namespace std;
  9. char game[5][6] = {"     ","     ","     ","     ","     "};
  10. int point = 0;
  11. int time1 = play_second;
  12. bool stop = 0;//停止countdown中之無限迴圈
  13. int countdown()//計時
  14. {
  15.     while(true)
  16.     {
  17.         if(stop)
  18.             break;
  19.         if(time1 == 0)
  20.             continue;
  21.         Sleep(1000);
  22.         time1--;   
  23.     }
  24. }
  25. int main()
  26. {
  27.     thread T1(countdown);
  28.     char key;
  29.     int a;
  30.     srand(time(NULL));
  31.     for(int i = 1;i<5;i++)//設定初始版面
  32.         game[i][(rand()%3)*2] = 'O';
  33.     while(1)
  34.     {
  35.         switch(point)//依分數變換顏色
  36.         {
  37.             case 0:
  38.             {
  39.                 system("color 0d");
  40.                 break;
  41.             }
  42.             case 100:
  43.             {
  44.                 system("color 0c");
  45.                 break;
  46.             }
  47.             case 200:
  48.             {
  49.                 system("color 0a");
  50.                 break;
  51.             }
  52.             case 300:
  53.             {
  54.                 system("color 0e");
  55.                 break;
  56.             }      
  57.         }
  58.         a = rand()%3;
  59.         if(a == 0)
  60.             game[0][0] = 'O';
  61.         else if(a == 1)
  62.             game[0][2] = 'O';
  63.         else
  64.             game[0][4] = 'O';
  65.         for(int i = 0;i<5;i++)
  66.         {
  67.             for(int j = 0;j<5;j++)
  68.             {
  69.                 cout<<game[i][j];
  70.             }
  71.             cout<<endl;
  72.         }
  73.         cout<<"---------"<<endl;   
  74.         cout<<"point:"<<point<<endl;
  75.         cout<<"time:"<<time1<<endl;
  76.         while(1)
  77.         {
  78.             if(time1 == 0)
  79.             {
  80.                 system("cls");
  81.                 cout<<"遊戲結束! 得分:"<<point<<endl;
  82.                 cout<<"輸入y繼續/n結束"<<endl;
  83.                 while(1)
  84.                 {
  85.                     char c = getch();
  86.                     if(c == 'n')
  87.                     {
  88.                         stop = 1;
  89.                         T1.join();
  90.                         return 0;
  91.                     }
  92.                     else if(c == 'y')
  93.                     {
  94.                     time1 = play_second;
  95.                     point = 0;
  96.                     break; 
  97.                     }              
  98.                 }
  99.                 break;
  100.             }
  101.             key = getch();
  102.             if(key == 'a')
  103.             {
  104.                 if(game[4][0] == 'O')//打擊正確
  105.                 {
  106.                     point++;
  107.                     break;
  108.                 }
  109.                 else//打錯顏色變為白色,暫停一秒
  110.                 {          
  111.                 system("color 0f");
  112.                 Sleep(1000);
  113.                 while(kbhit()) getch();//清除鍵盤緩衝區
  114.                 if(point <=100)
  115.                     system("color 0d");
  116.                 else if(point>100 && point<=200)
  117.                     system("color 0c");
  118.                 else if(point>200 && point<=300)
  119.                     system("color 0a");
  120.                 else
  121.                     system("color 0e");
  122.                 }
  123.             }
  124.            
  125.             else if(key == 's')
  126.             {
  127.                 if(game[4][2] == 'O')
  128.                 {
  129.                     point++;
  130.                     break;
  131.                 }
  132.                 else
  133.                 {          
  134.                 system("color 0f");
  135.                 Sleep(1000);
  136.                 while(kbhit()) getch();
  137.                 if(point <=100)
  138.                     system("color 0d");
  139.                 else if(point>100 && point<=200)
  140.                     system("color 0c");
  141.                 else if(point>200 && point<=300)
  142.                     system("color 0a");
  143.                 else
  144.                     system("color 0e");
  145.                 }
  146.             }
  147.            
  148.             else if(key == 'd')
  149.             {
  150.                 if(game[4][4] == 'O')
  151.                 {
  152.                     point++;
  153.                     break;
  154.                 }
  155.                 else
  156.                 {          
  157.                 system("color 0f");
  158.                 Sleep(1000);
  159.                 while(kbhit()) getch();
  160.                 if(point <=100)
  161.                     system("color 0d");
  162.                 else if(point>100 && point<=200)
  163.                     system("color 0c");
  164.                 else if(point>200 && point<=300)
  165.                     system("color 0a");
  166.                 else
  167.                     system("color 0e");
  168.                 }
  169.             }      
  170.         }
  171.         for(int i = 3;i>=0;i--)
  172.             for(int j = 0;j<5;j++)
  173.                 game[i+1][j] = game[i][j];
  174.         for(int i = 0;i<5;i++)
  175.             game[0][i] = ' ';
  176.         system("cls");
  177.     }
  178.     return 0;  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement