Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // snake.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.  
  25.  
  26. DWORD WINAPI GenerateEnemies(CONST LPVOID lpParam)
  27. {
  28.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  29.     COORD cEnemies;
  30.     cEnemies.X = gen() % 118;
  31.     cEnemies.Y = 1;
  32.     FillConsoleOutputAttribute(hOut, 20, 1, cEnemies, &l);
  33.     WORD color;
  34.     while (cEnemies.Y != 28)
  35.     {
  36.         Sleep(difficulty);
  37.         ReadConsoleOutputAttribute(hOut, &color, 1, cEnemies, &l);
  38.         if (color != 20)
  39.         {
  40.             counter++;
  41.             break;
  42.         }
  43.  
  44.         FillConsoleOutputAttribute(hOut, 10, 1, cEnemies, &l);
  45.         cEnemies.Y++;
  46.         FillConsoleOutputAttribute(hOut, 20, 1, cEnemies, &l);
  47.     }
  48.     if (cEnemies.Y == 28)
  49.     {
  50.         cout << "Your score: " << counter << endl << "KRASAVA" << endl;
  51.         exit(0);
  52.     }
  53.     return 0;
  54. }
  55.  
  56. DWORD WINAPI Shot(CONST LPVOID lpParam)
  57. {
  58.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  59.     COORD cShoot;
  60.     cShoot = c;
  61.     bool flag = false;
  62.     WORD color;
  63.     while (cShoot.Y != -1)
  64.     {
  65.         WaitForSingleObject(hMutex, INFINITE);
  66.         ReleaseMutex(hMutex);
  67.         Sleep(100);
  68.         if (flag)
  69.         {
  70.             FillConsoleOutputAttribute(hOut, 10, 1, cShoot, &l);
  71.         }
  72.         cShoot.Y--;
  73.         ReadConsoleOutputAttribute(hOut, &color, 1, cShoot, &l);
  74.         if (color == 20)
  75.         {
  76.             FillConsoleOutputAttribute(hOut, 10, 1, cShoot, &l);
  77.             break;
  78.         }
  79.         FillConsoleOutputAttribute(hOut, 10 << 3, 1, cShoot, &l);
  80.         flag = true;
  81.     }
  82.     return 0;
  83. }
  84.  
  85. DWORD WINAPI Gent(CONST LPVOID lpParam)
  86. {
  87.     srand(time(0));
  88.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  89.     int kek = 0;
  90.     while (true)
  91.     {
  92.         HANDLE tEnemies = CreateThread(NULL, 0, &GenerateEnemies, hMutex, 0, NULL);
  93.         kek++;
  94.         Sleep(1000 + rand() % 5000);
  95.     }
  96.     return 0;
  97. }
  98.  
  99. int main()
  100. {
  101.     const int kLeft = 37;
  102.     const int kRight = 39;
  103.     const int kSpace = 32;
  104.     const int kEsc = 27;
  105.  
  106.     HANDLE hInp = GetStdHandle(STD_INPUT_HANDLE);
  107.     PINPUT_RECORD input = new INPUT_RECORD;
  108.     DWORD cb;
  109.  
  110.     c.X = 54;
  111.     c.Y = 28;
  112.  
  113.     COORD cShoot;
  114.  
  115.     CONST HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
  116.  
  117.  
  118.  
  119.     int dif;
  120.     cout << "Choose difficulty: " << endl << "1-very ease\n2-ease\n3-medium\n4-hard\n5-very hard";
  121.     cin >> dif;
  122.     switch (dif)
  123.     {
  124.     case 1:
  125.         difficulty = 500;
  126.         break;
  127.     case 2:
  128.         difficulty = 400;
  129.         break;
  130.     case 3:
  131.         difficulty = 300;
  132.         break;
  133.     case 4:
  134.         difficulty = 200;
  135.         break;
  136.     case 5:
  137.         difficulty = 100;
  138.         break;
  139.     default:
  140.         break;
  141.     }
  142.     system("cls");
  143.     CreateThread(NULL, 0, &Gent, hMutex, 0, NULL);
  144.     FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
  145.  
  146.  
  147.     while (true)
  148.     {
  149.         ReadConsoleInput(hInp, input, 1, &cb);
  150.  
  151.         if (input->EventType == KEY_EVENT)
  152.         {
  153.             if (input->Event.KeyEvent.bKeyDown)
  154.             {
  155.                 switch (input->Event.KeyEvent.wVirtualKeyCode)
  156.                 {
  157.                 case kLeft:
  158.                 {
  159.                     FillConsoleOutputAttribute(hOut, 10, 1, c, &l);
  160.                     c.X--;
  161.                     FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
  162.                     break;
  163.                 }
  164.                 case kRight:
  165.                 {
  166.                     FillConsoleOutputAttribute(hOut, 10, 1, c, &l);
  167.                     c.X++;
  168.                     FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
  169.                     break;
  170.                 }
  171.                 case kSpace:
  172.                 {
  173.                     HANDLE tShot = CreateThread(NULL, 0, &Shot, hMutex, 0, NULL);
  174.                     break;
  175.                 }
  176.                 case kEsc: ExitProcess(1);
  177.                 default:
  178.                     break;
  179.                 }
  180.             }
  181.         }
  182.     }
  183.  
  184.  
  185.     return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement