Advertisement
KirriLL

test1(boll)

Feb 13th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <ctime>
  6.  
  7. #define M 24
  8. #define N 79
  9.  
  10. using namespace std;
  11.  
  12. struct Car
  13. {
  14.     int x, y, widht;
  15. };
  16.  
  17. struct Ball
  18. {
  19.     int x, y, dx, dy;
  20.     bool active;
  21.     void move()
  22.     {
  23.         x += dx; y += dy;
  24.     }
  25. };
  26.  
  27. struct Block
  28. {
  29.     int x, y, wid;
  30.     bool act;
  31. };
  32.  
  33. Car car;
  34. Ball ball;
  35. Block blocks[M / 4][N / 4];
  36. const char* map[M][N];
  37.  
  38. void ShowMap()
  39. {
  40.     for (int i = 0; i<M; i++)
  41.     {
  42.         for (int j = 0; j<N; j++)
  43.             cout << map[i][j];
  44.         cout << "\n";
  45.     }
  46.  
  47. }
  48.  
  49. void SetCaret(const char* symb)
  50. {
  51.     for (int i = 0; i<car.widht; i++)
  52.         map[car.x][car.y + i] = symb;
  53. }
  54.  
  55. int isColision(Ball &b, Block &block)
  56. {
  57.     if (block.act)
  58.     {
  59.         if ((b.x == block.x + 1) && (b.y >= block.y) && (b.y <= block.y + block.wid))
  60.             return 1;
  61.         if ((b.x == block.x) && (b.y == block.y + block.wid + 1))
  62.             return 2;
  63.         if ((b.x == block.x - 1) && (b.y >= block.y) && (b.y <= block.y + block.wid))
  64.             return 1;
  65.         if ((b.x == block.x) && (b.y == block.y))
  66.             return 2;
  67.     }
  68.     return 0;
  69. }
  70.  
  71. void SetBlocks()
  72. {
  73.     for (int i = 0; i<M / 4; i++)
  74.     {
  75.         int step = 1;
  76.         for (int j = 0; j<M / 4; j++)
  77.         {
  78.             Block &b = blocks[i][j];
  79.             b.wid = 3;
  80.             b.x = i;
  81.             b.y = (j)*b.wid + step;
  82.             if (b.act){
  83.                 for (int h = 0; h<b.wid; h++)
  84.                     map[b.x][b.y + h] = " * ";
  85.             }
  86.             else
  87.             {
  88.                 for (int h = 0; h<b.wid; h++)
  89.                     map[b.x][b.y + h] = "  ";
  90.                 map[b.x][b.y + b.wid] = "  ";
  91.                 step++;
  92.             }
  93.         }
  94.  
  95.     }
  96.  
  97. }
  98.  
  99. int main()
  100. {
  101.     srand(time(NULL));
  102.     for (int i = 0; i<M; i++)
  103.         for (int j = 0; j<N; j++)
  104.             map[i][j] = "  ";
  105.     car.x = 23;
  106.     car.y = 25;
  107.     car.widht = 10;
  108.  
  109.     SetCaret("#");
  110.  
  111.     ball.x = car.x - 1;
  112.     ball.y = car.y + car.widht / 2;
  113.     ball.dx = -1;
  114.     ball.dy = 1;
  115.     ball.active = true;
  116.     map[ball.x][ball.y] = "O";
  117.  
  118.     for (int i = 0; i<M / 4; i++)
  119.         for (int j = 0; j<N / 4; j++)
  120.             blocks[i][j].act = true;
  121.  
  122.     SetBlocks();
  123.  
  124.     ShowMap();
  125.  
  126.     int end = 0;
  127.     char st;
  128.  
  129.     while (end != 0)
  130.     {
  131.         if (ball.x < 8)
  132.         {
  133.             for (int i = 0; i<M / 4; i++)
  134.             {
  135.                 for (int j = 0; j<N / 4; j++)
  136.                 {
  137.                     Block &b = blocks[i][j];
  138.                     if (isColision(ball, b) == 1) { ball.dx *= -1; b.act = true; }
  139.                     else if (isColision(ball, b) == 2) { ball.dy *= -1; b.act = true; }
  140.                     else if (isColision(ball, b) == 3) { ball.dx *= -1; ball.active = true; }
  141.                     SetBlocks();
  142.                 }
  143.             }
  144.  
  145.         }
  146.         map[ball.x][ball.y] = " ";
  147.         if (ball.active) ball.move();
  148.         map[ball.x][ball.y] = "O";
  149.         if (ball.y < 1 || ball.y > N - 1) ball.dy *= -1;
  150.         if (ball.x < 1 || ball.x > M - 1) ball.dx *= -1;
  151.         if ((ball.x == car.x - 1) && (ball.y >= car.y) && (ball.y <= car.y + car.widht))
  152.             end = 0;
  153.         if (ball.x > M - 1) end = 0;
  154.  
  155.        
  156.         if (GetAsyncKeyState('a'))
  157.         {
  158.             SetCaret("  ");
  159.             car.y -= 3;
  160.             SetCaret("#");
  161.         }
  162.         if (GetAsyncKeyState('d'))
  163.             {
  164.                 SetCaret("  ");
  165.                 car.y += 3;
  166.                 SetCaret("#");
  167.             }
  168.         if (GetAsyncKeyState(VK_SPACE))
  169.             end = 1;
  170.         if (GetAsyncKeyState('P'))
  171.         {
  172.             system("cls");
  173.             cout << "\n\n\n\n\n\t\t For continue press any key....";
  174.             _getch();
  175.         }
  176.             Sleep(10);
  177.             system("cls");
  178.             ShowMap();
  179.     }
  180.     system("cls");
  181.     cout << "\n\n\n\n\t\t\t\tGAME OVER!";
  182.     _getch();
  183.     return 0;
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement