Advertisement
TroubleMaker84

Untitled

Apr 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. #define LUNGIME 92
  10. #define LATIME 25
  11. #define SIMBOL_MARGINE1 '-'
  12. #define SIMBOL_MARGINE2 '|'
  13.  
  14. using namespace std;
  15.  
  16. struct Monstru{
  17.     int HP, poz, pozy;
  18.     char FORMA[5][7], LASER;
  19. };
  20. Monstru M1, M2, M3, M4, R;
  21.  
  22. int sh = 1;
  23. bool GAMEEND = false;
  24. char matTer[LATIME][LUNGIME];
  25. short int pozNava = 4;
  26.  
  27. int Is_Clear(int m, int n)
  28. {
  29.     for(int i=m;i<=m+5;i++)
  30.         for(int j=n-2;j<=n+2;j++)
  31.             if(matTer[i][j]!='\0' )
  32.                 return 0;
  33.     return 1;
  34. }
  35.  
  36. void m1 (){
  37.     M1.pozy = 88;
  38.     do
  39.         M1.poz = rand() % 24;
  40.     while (
  41.         M1.poz < 3 ||
  42.         Is_Clear(M1.poz,M1.pozy)==0
  43.     );
  44.     matTer[M1.poz][M1.pozy] = M1.FORMA[2][1];
  45.     matTer[M1.poz - 1][M1.pozy + 2] =
  46.     matTer[M1.poz][M1.pozy + 2] =
  47.     matTer[M1.poz + 1][M1.pozy + 2] =
  48.     matTer[M1.poz - 1][M1.pozy + 1] =
  49.     matTer[M1.poz][M1.pozy + 1] =
  50.     matTer[M1.poz + 1][M1.pozy + 1] =
  51.     M1.FORMA[2][2];
  52.  
  53. }
  54. void m2(){
  55.     M2.pozy = 88;
  56.     do
  57.         M2.poz = rand() % 23;
  58.     while (
  59.            M2.poz < 2 ||Is_Clear(M2.poz,M2.pozy)==0);
  60.     matTer[M2.poz][M2.pozy] =
  61.     matTer[M2.poz][M2.pozy + 1] =
  62.     matTer[M2.poz][M2.pozy + 2] =
  63.     M2.FORMA[1][1];
  64. }
  65.  
  66. void m3(){
  67.     M3.pozy = 88;
  68.     do
  69.         M3.poz = rand() % 23;
  70.     while (M3.poz < 3 ||
  71.          Is_Clear(M3.poz,M3.pozy)==0);
  72.     matTer[M3.poz][M3.pozy] =
  73.     matTer[M3.poz][M3.pozy + 1] =
  74.     matTer[M3.poz - 1][M3.pozy + 1] =
  75.     matTer[M3.poz - 1][M3.pozy + 2] =
  76.     matTer[M3.poz + 1][M3.pozy + 1] =
  77.     matTer[M3.poz + 1][M3.pozy + 2] =
  78.     M3.FORMA[2][1];
  79. }
  80.  
  81. void m4(){
  82.     M4.pozy = 88;
  83.     do
  84.         M4.poz = rand() %  21;
  85.     while (
  86.         M4.poz < 5 ||
  87.          Is_Clear(M4.poz,M4.pozy)==0
  88.     );
  89.  
  90.     matTer[M4.poz -1][M4.pozy-1]=M4.FORMA[1][2];
  91.     matTer[M4.poz][M4.pozy-1]=matTer[M4.poz][M4.pozy+2]=M4.FORMA[2][2];
  92.     matTer[M4.poz +1][M4.pozy-1]=M4.FORMA[4][1];
  93.     matTer[M4.poz-1][M4.pozy-2]=matTer[M4.poz +1][M4.pozy-2]=M4.FORMA[1][1];
  94.     matTer[M4.poz -1 ][M4.pozy]=matTer[M4.poz -1][M4.pozy+1]=matTer[M4.poz +1][M4.pozy]=matTer[M4.poz +1][M4.pozy+1]=M4.FORMA[1][3];
  95.     matTer[M4.poz -1][M4.pozy+2]=M4.FORMA[1][6];
  96.     matTer[M4.poz+1][M4.pozy+2]=M4.FORMA[4][6];
  97.     matTer[M4.poz][M4.pozy]=matTer[M4.poz][M4.pozy+1]=M4.FORMA[3][3];
  98. }
  99.  
  100. void r(){
  101.     R.pozy = 86;
  102.     do
  103.         R.poz = rand () % 22;
  104.     while (
  105.         R.poz<2 ||
  106.         matTer[R.poz][R.pozy] != '\0' ||
  107.         matTer[R.poz - 1][R.pozy + 1] != '\0' ||
  108.         matTer[R.poz - 1][R.pozy + 2] != '\0' ||
  109.         matTer[R.poz - 1][R.pozy + 3] != '\0' ||
  110.         matTer[R.poz][R.pozy + 1] != '\0' ||
  111.         matTer[R.poz][R.pozy + 2] != '\0' ||
  112.         matTer[R.poz][R.pozy + 3] != '\0'
  113.     );
  114.     matTer[R.poz][R.pozy + 4] = R.FORMA[2][4];
  115.     matTer[R.poz - 1][R.pozy + 1] = matTer[R.poz - 1][R.pozy + 2] = matTer[R.poz - 1][R.pozy + 3] = R.FORMA[1][2];
  116.     matTer[R.poz][R.pozy + 1] = matTer[R.poz][R.pozy + 2] = matTer[R.poz][R.pozy + 3] = R.FORMA[1][2];
  117.     matTer[R.poz][R.pozy] = R.FORMA[2][1];
  118. }
  119.  
  120. void show_cursor(bool show){ //vizibilitate curosor in consola
  121.     HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  122.     CONSOLE_CURSOR_INFO infCursor;
  123.     GetConsoleCursorInfo(out, &infCursor);
  124.     infCursor.bVisible = show;
  125.     SetConsoleCursorInfo(out, &infCursor);
  126. }
  127.  
  128. int check (char x){
  129.     int i,j;
  130.     for(i = 1; i <= 3; i++)
  131.         for(j = 1; j <= 3; j++)
  132.             if (x == M1.FORMA[i][j])
  133.                 return 1;
  134.     if (x == M2.FORMA[1][1])
  135.         return 2;
  136.     for(i = 1; i <= 3; i++)
  137.         for(j = 1;j <= 3; j++)
  138.             if (x == M3.FORMA[i][j])
  139.                 return 3;
  140.     for(i = 1; i <= 5; i++)
  141.         for(j = 1; j <= 4; j++)
  142.             if (x == M4.FORMA[i][j])
  143.                 return 4;
  144.     for(i = 1; i <= 2; i++)
  145.         for(j = 1; j <= 4; j++)
  146.             if(x == R.FORMA[i][j])
  147.                 return 5;
  148.     return 0;
  149. }
  150. int find_coordX(int n)
  151. {
  152.    
  153. }
  154.  
  155. void DEL_Monster(int m, int n){
  156.     for(int i = m - 1; i <= m + 3; i++)
  157.         for(int j = n; j<=n + 3; j++)
  158.                 if(matTer[i][j]!=char(219))
  159.                     matTer[i][j]='\0';
  160. }
  161.  
  162. void afisare(){
  163.     for (int i = 1; i <= LATIME - 1; i++){
  164.         for (int j = 1; j <= LUNGIME - 1; j++)
  165.                 cout << matTer[i][j];
  166.         cout << endl;
  167.         }
  168. }
  169.  
  170. void creare_teren(){
  171.     int i;
  172.     for (i = 1; i <= LATIME - 1; i++)
  173.         matTer[i][1] = matTer[i][LUNGIME - 1] = SIMBOL_MARGINE2;
  174.     for (i = 1; i <= LUNGIME - 1; i++)
  175.         matTer[1][i] = matTer[LATIME - 1][i] = SIMBOL_MARGINE1;
  176. }
  177.  
  178. void nava(){
  179.     matTer[2][2] = matTer[3][2] = matTer[4][2] = matTer[3][3] = (char)219;
  180. }
  181. void wave1()
  182. {
  183.     int i;
  184.     int Wave_Size=20;
  185.     Monstru  Monster[50];
  186.     int Monster_Type[50];
  187.     for(i=1;i<=Wave_Size;i++)
  188.         switch (rand()%4)
  189.         {
  190.             case 0: {Monster[i]=M1;
  191.                     Monster_Type[i]=1;}
  192.             case 1: {Monster[i]=M2;
  193.             Monster_Type[i]=2;}
  194.             case 2: {Monster[i]=M3;
  195.             Monster_Type[i]=3;}
  196.             case 3: {Monster[i]=M4;
  197.             Monster_Type[i]=4;}
  198.         }
  199.  
  200.  
  201. }
  202.  
  203. void Check_KeyPress ()
  204. {
  205.         char newKey;
  206.  
  207.     if (_kbhit())
  208.         newKey = getch();
  209.     if(newKey == 's' ||newKey == 'S')
  210.         if(pozNava < 18){
  211.             matTer[pozNava + 1][2] = matTer[pozNava][2];
  212.             matTer[pozNava ][3] = matTer[pozNava-1][3];
  213.             matTer[pozNava - 2][2] = matTer[pozNava-1][3] = '\0';
  214.             pozNava++;
  215.         }
  216.     if(newKey == 'w' || newKey == 'W')
  217.         if(pozNava > 4){
  218.             matTer[pozNava - 3][2] = matTer[pozNava][2];
  219.             matTer[pozNava - 2][3] = matTer[pozNava - 1][3];
  220.             matTer[pozNava][2] = matTer[pozNava - 1][3] = '\0';
  221.             pozNava--;
  222.         }
  223.     if(newKey == (char)32){
  224.         if (matTer[pozNava - 1][4] != '\0')
  225.             matTer[pozNava - 1][4] = '\0';
  226.         matTer[pozNava - 1][4] = char(248);
  227.     }
  228.  
  229. }
  230.  
  231. void Monster_Shoot()
  232. {
  233.     if(sh % 25 == 1 && M1.HP != 0)
  234.         matTer[M1.poz][M1.pozy - 1] = M1.LASER;
  235.     if(sh % 25 == 7 && M2.HP != 0)
  236.         matTer[M2.poz][M2.pozy - 1] = M2.LASER;
  237.     if(sh % 25 == 13 && M3.HP != 0)
  238.         matTer[M3.poz][M3.pozy - 1] = M3.LASER;
  239.     if(sh % 25 == 17 && M4.HP != 0)
  240.         matTer[M4.poz - 1][M4.pozy - 3] = matTer[M4.poz + 1][M4.pozy - 3] = M4.LASER;
  241.     sh++;
  242. }
  243. void miscare(){
  244.     Check_KeyPress();
  245.     int i,j;
  246.     Monster_Shoot();
  247.     for(i = 2; i <= LATIME - 2; i++)
  248.         for(j = 2 ; j <= LUNGIME - 2; j++)
  249.         {if(matTer[i][j] == (char)248)
  250.             {
  251.                 if(
  252.                     matTer[i][j + 2] == M1.LASER ||
  253.                     matTer[i][j + 2] == M2.LASER ||
  254.                     matTer[i][j + 2] == M3.LASER ||
  255.                     matTer[i][j + 2] == M4.LASER ||
  256.                     matTer[i][j + 1] == M1.LASER ||
  257.                     matTer[i][j + 1] == M2.LASER ||
  258.                     matTer[i][j + 1] == M3.LASER ||
  259.                     matTer[i][j + 1] == M4.LASER
  260.                 )
  261.                     matTer[i][j + 2] = matTer[i][j + 1] = '\0';
  262.                 else
  263.                     if(matTer[i][j + 2] == '\0'){
  264.                         if(j < LUNGIME - 3){
  265.                             matTer[i][j + 2] = (char)248;
  266.                             matTer[i][j] = '\0';
  267.                             j = j + 2;
  268.                         }
  269.                         else
  270.                             matTer[i][j]='\0';
  271.                     }
  272.                     else{
  273.                         switch (check(matTer[i][j + 2]))
  274.                         {                                   //de aici si pana la urmatorul, verific ce "monstru" a lovit
  275.                         case 1:{                            // printr-un switch care verifica acel caracter care este in 2 pozitii
  276.                             M1.HP -= 1;                     //unde ar trebui sa se mute glontele
  277.                             if(M1.HP == 0)
  278.                             DEL_M1();
  279.                             break;
  280.                         }
  281.                         case 2:{
  282.                             M2.HP = 0; //daca distrugi nava, nici sa nu mai traga
  283.                             DEL_M2();
  284.                             break;
  285.                         }
  286.                         case 3:{
  287.                             M3.HP--;
  288.                             if(M3.HP == 0)
  289.                                 DEL_M3();
  290.                             break;
  291.                         }
  292.                         case 4:{
  293.                             if(matTer[M4.poz][M4.pozy]=='\0')
  294.                                     {
  295.                                     Sleep(10000);}
  296.                             M4.HP--;
  297.                             if(M4.HP==0){
  298.                                 DEL_M4();}
  299.                             break;
  300.                         }
  301.                         default: break;
  302.                         }
  303.                         matTer[i][j]='\0';
  304.                     }        //daca s-a intrat pe acel else, se loveste de ceva, fac ce fac, si dupa sterg glontele
  305.             }
  306.             else
  307.                 if(matTer[i][j] == M1.LASER || matTer[i][j] == M2.LASER || matTer[i][j] == M3.LASER || matTer[i][j] == M4.LASER){
  308.                     if (matTer[i][j - 2] == char(219) || matTer[i][j - 1] == char(219))
  309.                         GAMEEND=TRUE;       //daca esti lovit, se termina jocul
  310.                     else
  311.                         if(j==3){
  312.                             matTer[i][j] = '\0';      //daca glontele lui a ajuns la coloana 3, dispare
  313.                         }
  314.                         else
  315.                             if(j > 3){
  316.                                 matTer[i][j-1]=matTer[i][j];
  317.                                 matTer[i][j]='\0';
  318.                             }         //il tot mut cu o pozitie la fiecare parcurgere
  319.                 }
  320.                 else{
  321.                     if(sh % 3 == 0){     //"sh" a devenit si rata la care se misca navele, ca nu vreau sa o faca mereu
  322.                         if(matTer[i][j] != '\0' && matTer[i][j] != char(219))
  323.                             if(check(matTer[i][j])==1){              //toate if/else verifica muta componenta unei nave
  324.                                 if(M1.pozy == 3){       //si in caz ca ajunge la final, o face sa dispara
  325.                                     if(matTer[i][j - 1] == char(219))
  326.                                         GAMEEND=TRUE;
  327.                                     else{
  328.                                         DEL_M1();
  329.                                         M1.HP=0;
  330.                                     }
  331.                                 }
  332.                                 matTer[i][j - 1] = matTer[i][j];
  333.                                 if(i == M1.poz && j == M1.pozy)
  334.                                     M1.pozy--;
  335.                             }
  336.                             else
  337.                                 if(check(matTer[i][j]) == 2){
  338.                                     if(M2.pozy == 3){
  339.                                         if(matTer[i][j - 1] == char(219))
  340.                                             GAMEEND = TRUE;
  341.                                         else{
  342.                                             DEL_M2();
  343.                                             M2.HP = 0;
  344.                                         }
  345.                                     }
  346.                                     matTer[i][j - 1] = matTer[i][j];
  347.                                     if(i == M2.poz && j == M2.pozy)
  348.                                         M2.pozy--;
  349.                                 }
  350.                                 else
  351.                                     if(check(matTer[i][j]) == 3){
  352.                                         if(M3.pozy == 3){
  353.                                             if(matTer[i][j - 1] == char(219))
  354.                                                 GAMEEND = TRUE;
  355.                                             else{
  356.                                                 DEL_M3();
  357.                                                 M3.HP = 0;
  358.                                             }
  359.                                         }
  360.                                         matTer[i][j - 1] = matTer[i][j];
  361.                                         if(i == M3.poz && j == M3.pozy)
  362.                                             M3.pozy--;
  363.                                     }
  364.                                     else
  365.                                         if(check(matTer[i][j]) == 4){
  366.                                             if(M4.pozy == 4){
  367.                                                 if(matTer[i][j - 1] == char(219))
  368.                                                     GAMEEND = TRUE;
  369.                                                 else{
  370.                                                     DEL_M4();
  371.                                                     M4.HP = 0;
  372.                                                 }
  373.                                             }
  374.                                             matTer[i][j - 1] = matTer[i][j];
  375.                                             if(i == M4.poz && j == M4.pozy)
  376.                                                 M4.pozy--;
  377.                                         }
  378.                         if(matTer[i][j] != char(219))
  379.                             matTer[i][j] = '\0';
  380.                     }
  381.                 }
  382.  
  383.  
  384.         }}
  385.  
  386. int main (){
  387.     char x;
  388.     cout << "_______________________________________________________________________" << endl;
  389.     cout << "|                                                                      |" << endl;
  390.     cout << "|          *       *   ***  *    ***   **   *     *  ***               |" << endl;
  391.     cout << "|           *  *  *    **   *    *    *  *  *  *  *  **                |" << endl;
  392.     cout << "|            *   *     ***  ***  ***   **   *     *  ***               |" << endl;
  393.     cout << "|                                                                      |" << endl;
  394.     cout << "|                                                                      |" << endl;
  395.     cout << "|            PRESS 1 TO START GAME                                     |" << endl;
  396.     cout << "|            PRESS 2 TO EXIT                                           |" << endl;
  397.     cout << "_______________________________________________________________________" << endl;
  398.     cin >> x;
  399.     system("cls");
  400.     if(x == '1'){
  401.         creare_teren();
  402.         show_cursor(false);
  403.         M1.HP = 3;
  404.         M2.HP = 1;
  405.         M3.HP = 4;
  406.         M4.HP = 6;
  407.         M1.FORMA[2][1] = (char)174;
  408.         M1.FORMA[1][2] = M1.FORMA[2][2] = M1.FORMA[3][2] = (char)254;
  409.         M2.FORMA[1][1] = M2.FORMA[1][2] = M2.FORMA[1][3] = char(178);
  410.         M3.FORMA[2][1] = M3.FORMA[2][2] = M3.FORMA[1][2] = M3.FORMA[1][3] = M3.FORMA[3][2] = M3.FORMA[3][3] = 'O';
  411.         R.FORMA[2][1] = '<';
  412.         R.FORMA[1][2] = R.FORMA[1][3] = R.FORMA[2][2] = R.FORMA[2][3] = '_';
  413.         R.FORMA[2][4] = '|';
  414.         M4.FORMA[1][1] = M4.FORMA[4][1] = '-';
  415.         M4.FORMA[1][2] = char(201);
  416.         M4.FORMA[1][6] = char(187);
  417.         M4.FORMA[2][2] = M4.FORMA[3][2] = char(186);
  418.         M4.FORMA[1][3] = M4.FORMA[1][4] = M4.FORMA[1][5] = M4.FORMA[4][3] = M4.FORMA[4][4] = M4.FORMA[4][5] = char(205);
  419.         M4.FORMA[2][6] = M4.FORMA[3][6] = char(186);
  420.         M4.FORMA[4][1] = char(200);
  421.         M4.FORMA[4][6] = char(188);
  422.         M4.FORMA[3][2] = M4.FORMA[3][3] = ' ';
  423.         srand(time(NULL));
  424.         m1();
  425.         m2();
  426.         m3();
  427.         m4();
  428.         r();
  429.         nava();
  430.         M1.LASER = char (223);
  431.         M2.LASER = '=';
  432.         M3.LASER = char(249);
  433.         M4.LASER = '~';
  434.         afisare();
  435.         wave1();
  436.  
  437.         system("cls");
  438.         while(GAMEEND==FALSE){
  439.                 miscare();
  440.                 Sleep(50);
  441.                 system("cls");
  442.                 afisare();
  443.  
  444.                 if(M1.HP == 0 && M2.HP == 0 && M3.HP == 0 && M4.HP == 0){
  445.                     GAMEEND=TRUE;
  446.                     cout << "CONGRATS";
  447.                 }
  448.         }
  449.     }
  450.     else
  451.         if(x == '2')
  452.           cout << "GAME OVER";
  453.           exit(0);
  454.     return 0;
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement