Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // game.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include <iostream>
  7. #include <ctime>
  8. #include <random>
  9. #include <cstring>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  17. DWORD l;
  18. COORD c;
  19. int field[27][118];
  20. random_device rd;
  21. mt19937 gen(rd());
  22. int counter = 0;
  23. int difficulty = 0;
  24. int life = 3;
  25.  
  26.  
  27. DWORD WINAPI GenerateEnemies(CONST LPVOID lpParam)
  28. {
  29.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  30.     COORD cEnemies;
  31.     cEnemies.X = gen() % 118;
  32.     cEnemies.Y = 1;
  33.     FillConsoleOutputAttribute(hOut, 20, 1, cEnemies, &l);
  34.     WORD color;
  35.     while (cEnemies.Y != 28)
  36.     {
  37.         Sleep(500);
  38.         ReadConsoleOutputAttribute(hOut, &color, 1, cEnemies, &l);
  39.         if (color != 20)
  40.         {
  41.             counter++;
  42.             break;
  43.         }
  44.  
  45.         FillConsoleOutputAttribute(hOut, 10, 1, cEnemies, &l);
  46.         cEnemies.Y++;
  47.         FillConsoleOutputAttribute(hOut, 20, 1, cEnemies, &l);
  48.     }
  49.     if (cEnemies.Y == 28)
  50.     {
  51.         system("cls");
  52.         life--;
  53.         cout << "Lives: " << life << endl;
  54.     }
  55.     if (!life)
  56.     {
  57.         cout << "Your score: " << counter << endl << "KRASAVA" << endl;
  58.         exit(1);
  59.     }
  60.     return 0;
  61. }
  62.  
  63. DWORD WINAPI Shot(CONST LPVOID lpParam)
  64. {
  65.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  66.     COORD cShoot;
  67.     cShoot = c;
  68.     bool flag = false;
  69.     WORD color;
  70.     while (cShoot.Y != -1)
  71.     {
  72.         WaitForSingleObject(hMutex, INFINITE);
  73.         ReleaseMutex(hMutex);
  74.         Sleep(100);
  75.         if (flag)
  76.         {
  77.             FillConsoleOutputAttribute(hOut, 10, 1, cShoot, &l);
  78.         }
  79.         cShoot.Y--;
  80.         ReadConsoleOutputAttribute(hOut, &color, 1, cShoot, &l);
  81.         if (color == 20)
  82.         {
  83.             FillConsoleOutputAttribute(hOut, 10, 1, cShoot, &l);
  84.             break;
  85.         }
  86.         FillConsoleOutputAttribute(hOut, 10 << 3, 1, cShoot, &l);
  87.         flag = true;
  88.     }
  89.     return 0;
  90. }
  91.  
  92. DWORD WINAPI Gent(CONST LPVOID lpParam)
  93. {
  94.     srand(time(0));
  95.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  96.     int kek = 0;
  97.     while (true)
  98.     {
  99.         HANDLE tEnemies = CreateThread(NULL, 0, &GenerateEnemies, hMutex, 0, NULL);
  100.         kek++;
  101.         Sleep(rand() % difficulty);
  102.     }
  103.     return 0;
  104. }
  105.  
  106. int main()
  107. {
  108.     setlocale(LC_ALL, "Russian");
  109.  
  110.     const int kLeft = 37;
  111.     const int kRight = 39;
  112.     const int kSpace = 32;
  113.     const int kEsc = 27;
  114.  
  115.     HANDLE hInp = GetStdHandle(STD_INPUT_HANDLE);
  116.     PINPUT_RECORD input = new INPUT_RECORD;
  117.     DWORD cb;
  118.  
  119.     c.X = 54;
  120.     c.Y = 28;
  121.  
  122.     COORD cShoot;
  123.  
  124.     CONST HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
  125.  
  126.  
  127.  
  128.     int dif;
  129.     cout << "Choose difficulty: " << endl << "1-very ease\n2-ease\n3-medium\n4-hard\n5-impossible\n";
  130.     cin >> dif;
  131.     switch (dif)
  132.     {
  133.     case 1:
  134.         difficulty = 5000;
  135.         break;
  136.     case 2:
  137.         difficulty = 4000;
  138.         break;
  139.     case 3:
  140.         difficulty = 3000;
  141.         break;
  142.     case 4:
  143.         difficulty = 2000;
  144.         break;
  145.     case 5:
  146.         difficulty = 1000;
  147.         break;
  148.     default:
  149.         break;
  150.     }
  151.     system("cls");
  152.     cout << "Lives: " << life << endl;
  153.     CreateThread(NULL, 0, &Gent, hMutex, 0, NULL);
  154.     FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
  155.  
  156.  
  157.     while (true)
  158.     {
  159.         ReadConsoleInput(hInp, input, 1, &cb);
  160.  
  161.         if (input->EventType == KEY_EVENT)
  162.         {
  163.             if (input->Event.KeyEvent.bKeyDown)
  164.             {
  165.                 switch (input->Event.KeyEvent.wVirtualKeyCode)
  166.                 {
  167.                 case kLeft:
  168.                 {
  169.                     FillConsoleOutputAttribute(hOut, 10, 1, c, &l);
  170.                     c.X--;
  171.                     FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
  172.                     break;
  173.                 }
  174.                 case kRight:
  175.                 {
  176.                     FillConsoleOutputAttribute(hOut, 10, 1, c, &l);
  177.                     c.X++;
  178.                     FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
  179.                     break;
  180.                 }
  181.                 case kSpace:
  182.                 {
  183.                     HANDLE tShot = CreateThread(NULL, 0, &Shot, hMutex, 0, NULL);
  184.                     break;
  185.                 }
  186.                 case kEsc: ExitProcess(1);
  187.                 default:
  188.                     break;
  189.                 }
  190.             }
  191.         }
  192.     }
  193.  
  194.  
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement