Tony041010

打磚塊

Jun 24th, 2021 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | None | 0 0
  1. /*小時候我玩過一種簡單的小遊戲,忘記名字了,感覺是叫做"乒乓球"?
  2. 一顆球會一直彈到牆壁,並且撞擊空間內的磚塊使它被敲破,我們要操控一個可移動的擋板擋住球,
  3. 讓球撞擊越多磚塊越好,撞一次得一分,並且會隨著分數越多,球會愈來越快*/
  4. #include <conio.h>
  5. #include <iostream>
  6. #include <time.h>
  7. #include <windows.h>
  8. using namespace std;
  9. int map[600]={0};
  10. int board_idx; //板子最左方那一格的座標
  11. int ball; //球所在的座標
  12. int vx = 30;
  13. int vy = 1;
  14. int score = 0;
  15. int SleepTime = 150;
  16.  
  17. void starting(){
  18.     cout<<"\n提醒 : 因為本遊戲的物體移動採用重繪方式呈現,故常會有小lag發生,請不要卯起來按a和d\n";
  19.     cout<<"如果已經讀完注意事項,請按下z開始遊戲,並確保輸入法保持在英文,caps lock不要打開";
  20.     while(1){
  21.         if(kbhit()){
  22.             char k = _getch();
  23.             if (k == 'z'){
  24.                 cout<<endl <<endl;
  25.                 cout<<"The game is about to start in 3 secs\n";
  26.                 Sleep(1000);
  27.                 cout << "3\n";
  28.                 Sleep(1000);
  29.                 cout << "2\n";
  30.                 Sleep(1000);
  31.                 cout << "1\n";
  32.                 Sleep(1000);
  33.                 cout << "Start!\n";
  34.                 Sleep(1000);
  35.                 break;
  36.             }
  37.             else{}
  38.         }
  39.     }
  40. }
  41.  
  42. //■□●
  43. void draw_map(){
  44.     system("cls");
  45.     cout << "Score :  " << score <<endl;
  46.     for(int i = 1 ; i <= 600 ; i++){
  47.         if(map[i] == 0){
  48.             cout << "  ";
  49.         }
  50.         else if(map[i] == 1 || map[i] == 4){
  51.             cout << "■";
  52.         }
  53.         else if(map[i] == 2){
  54.             cout << "●";
  55.         }
  56.         else if(map[i] == 3){
  57.             cout << "□";
  58.         }
  59.         if(i % 30 == 0){
  60.             cout << endl;
  61.         }
  62.     }
  63. }
  64.  
  65. // 1 for wall, 2 for ball, 3 for the board, 4 for the blocks
  66. void init_map(){
  67.     for(int i = 1 ; i <= 600 ; i++){
  68.         if(i<=30||i%30 ==1 ||i%30 ==0){
  69.             map[i]=1;
  70.         }
  71.     }
  72.     for(int i = 32 ; i <= 150 ; i++){
  73.         if(map[i]!=1){
  74.             map[i]=4;
  75.         }
  76.     }
  77.     ball = 160;
  78.     map[ball]=2; //球的起始點
  79.     for(int i = 573 ; i<=577 ; i++){
  80.         map[i]=3;
  81.     }
  82.      board_idx = 573;
  83. }
  84.  
  85. void turn_wall(){
  86.     //彈到左上方牆壁
  87.     if(map[ball-30]==1 && map[ball-1]==1){
  88.         vx = -vx;   //-31 -> +31
  89.         vy = -vy;
  90.     }
  91.     //彈到右上方牆壁
  92.     else if(map[ball-30]==1 && map[ball+1] ==1){
  93.         vx = -vx ;  //-29 -> +29
  94.         vy = -vy;
  95.         score += 1;
  96.     }
  97.     //彈到上方牆壁
  98.     else if(map[ball-30] ==1){
  99.         vx = -vx;  //-31 -> +29    -29  ->  +31
  100.     }
  101.     //彈到左方牆壁
  102.     else if(map[ball-1]==1){
  103.         vy = -vy;  //-31 -> -29    +29  ->  +31
  104.     }
  105.     //彈到右方牆壁
  106.     else if(map[ball+1]==1){
  107.         vy = -vy;  //+31 -> +29    -29  ->  -31
  108.     }
  109.     //彈到板子上
  110.     else if(map[ball+30] ==3){
  111.         vx = -vx;  //+31 -> -29    +29  ->  -31
  112.     }
  113. }
  114.  
  115. void turn_block(){
  116.     /*//敲到左上磚塊的尖端,消去
  117.     if(map[ball-30]==0 && map[ball-1]==0 && map[ball-31]==4){
  118.         map[ball-31]==0;
  119.         vx = -vx;   //-31 -> +31
  120.         vy = -vy;
  121.         score += 1;
  122.     }
  123.     //敲到右上磚塊的尖端,消去
  124.     else if(map[ball-30]==0 && map[ball+1]==0 &&map[ball-29]==4){
  125.         map[ball-29]=0;
  126.         vx = -vx ;  //-29 -> +29
  127.         vy = -vy;
  128.         score += 1;
  129.     }*/
  130.     //彈到左上方磚塊,消去左方磚塊
  131.     if(map[ball-30]==4 && map[ball-1]==4){
  132.         map[ball-1]==0;
  133.         vx = -vx;   //-31 -> +31
  134.         vy = -vy;
  135.         score += 1;
  136.     }
  137.     //彈到右上方磚塊,消去左方磚塊
  138.     else if(map[ball-30]==4 && map[ball+1] ==4){
  139.         map[ball+1]=0;
  140.         vx = -vx ;  //-29 -> +29
  141.         vy = -vy;
  142.         score += 1;
  143.     }
  144.     //彈到上方磚塊,消去
  145.     else if(map[ball-30] ==4){
  146.         map[ball-30]=0;
  147.         vx = -vx;  //-31 -> +29    -29  ->  +31
  148.         score += 1;
  149.     }
  150.     //彈到左方磚塊,消去
  151.     else if(map[ball-1]==4){
  152.         map[ball-1]==0;
  153.         vy = -vy;  //-31 -> -29    +29  ->  +31
  154.         score += 1;
  155.     }
  156.     //彈到右方磚塊,消去
  157.     else if(map[ball+1]==4){
  158.         map[ball+1] =0;
  159.         vy = -vy;  //+31 -> +29    -29  ->  -31
  160.         score += 1;
  161.     }
  162. }
  163. void move_ball(){
  164.     map[ball]=0;
  165.     map[ball+vx+vy]=2;
  166.     ball = ball+vx+vy;
  167.     turn_wall();
  168.     turn_block();
  169. }
  170.  
  171. int main(){
  172.     starting();
  173.     init_map();
  174.     draw_map();
  175.     while(1){
  176.         Sleep(SleepTime);
  177.         move_ball();
  178.         if(kbhit()){
  179.             char k =_getch();
  180.             if(k =='a'){
  181.                 if(board_idx !=572){
  182.                     if(board_idx == 573){
  183.                         board_idx -=1;
  184.                         for(int i = 0 ; i<5 ; i++){
  185.                             map[board_idx+i]=3;
  186.                         }
  187.                         map[board_idx+5] =0;
  188.                     }
  189.                     else{
  190.                         board_idx -=2;
  191.                         for(int i = 0 ; i<5 ; i++){
  192.                             map[board_idx+i]=3;
  193.                         }
  194.                         map[board_idx+5]=0;
  195.                         map[board_idx+6]=0;
  196.                     }
  197.                 }
  198.             }
  199.             else if(k =='d'){
  200.                 if(board_idx !=595){
  201.                     if(board_idx ==594){
  202.                         board_idx += 1;
  203.                         for(int i = 0 ; i<5 ; i++){
  204.                             map[board_idx+i]=3;
  205.                         }
  206.                         map[board_idx-1]=0;
  207.                     }
  208.                     else{
  209.                         board_idx +=2;
  210.                         for(int i = 0 ; i<5 ; i++){
  211.                             map[board_idx+i]=3;
  212.                         }
  213.                         map[board_idx-1]=0;
  214.                         map[board_idx-2]=0;
  215.                     }
  216.                 }
  217.             }
  218.             else if(k =='q'){
  219.                 break;
  220.             }
  221.         }
  222.         if(score >= 10){
  223.             SleepTime = 50;
  224.         }
  225.         else if(score >=5){
  226.             SleepTime = 100;
  227.         }
  228.         if(ball > 570){
  229.             draw_map();
  230.             cout << "Game over";
  231.             break;
  232.         }
  233.         draw_map();
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment