Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // game.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <Windows.h>
- #include <iostream>
- #include <ctime>
- #include <random>
- #include <cstring>
- #include <string>
- using namespace std;
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- DWORD l;
- COORD c;
- int field[27][118];
- random_device rd;
- mt19937 gen(rd());
- int counter = 0;
- int difficulty = 0;
- int life = 3;
- DWORD WINAPI GenerateEnemies(CONST LPVOID lpParam)
- {
- CONST HANDLE hMutex = (CONST HANDLE)lpParam;
- COORD cEnemies;
- cEnemies.X = gen() % 118;
- cEnemies.Y = 1;
- FillConsoleOutputAttribute(hOut, 20, 1, cEnemies, &l);
- WORD color;
- while (cEnemies.Y != 28)
- {
- Sleep(500);
- ReadConsoleOutputAttribute(hOut, &color, 1, cEnemies, &l);
- if (color != 20)
- {
- counter++;
- break;
- }
- FillConsoleOutputAttribute(hOut, 10, 1, cEnemies, &l);
- cEnemies.Y++;
- FillConsoleOutputAttribute(hOut, 20, 1, cEnemies, &l);
- }
- if (cEnemies.Y == 28)
- {
- system("cls");
- life--;
- cout << "Lives: " << life << endl;
- }
- if (!life)
- {
- cout << "Your score: " << counter << endl << "KRASAVA" << endl;
- exit(1);
- }
- return 0;
- }
- DWORD WINAPI Shot(CONST LPVOID lpParam)
- {
- CONST HANDLE hMutex = (CONST HANDLE)lpParam;
- COORD cShoot;
- cShoot = c;
- bool flag = false;
- WORD color;
- while (cShoot.Y != -1)
- {
- WaitForSingleObject(hMutex, INFINITE);
- ReleaseMutex(hMutex);
- Sleep(100);
- if (flag)
- {
- FillConsoleOutputAttribute(hOut, 10, 1, cShoot, &l);
- }
- cShoot.Y--;
- ReadConsoleOutputAttribute(hOut, &color, 1, cShoot, &l);
- if (color == 20)
- {
- FillConsoleOutputAttribute(hOut, 10, 1, cShoot, &l);
- break;
- }
- FillConsoleOutputAttribute(hOut, 10 << 3, 1, cShoot, &l);
- flag = true;
- }
- return 0;
- }
- DWORD WINAPI Gent(CONST LPVOID lpParam)
- {
- srand(time(0));
- CONST HANDLE hMutex = (CONST HANDLE)lpParam;
- int kek = 0;
- while (true)
- {
- HANDLE tEnemies = CreateThread(NULL, 0, &GenerateEnemies, hMutex, 0, NULL);
- kek++;
- Sleep(rand() % difficulty);
- }
- return 0;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- const int kLeft = 37;
- const int kRight = 39;
- const int kSpace = 32;
- const int kEsc = 27;
- HANDLE hInp = GetStdHandle(STD_INPUT_HANDLE);
- PINPUT_RECORD input = new INPUT_RECORD;
- DWORD cb;
- c.X = 54;
- c.Y = 28;
- COORD cShoot;
- CONST HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
- int dif;
- cout << "Choose difficulty: " << endl << "1-very ease\n2-ease\n3-medium\n4-hard\n5-impossible\n";
- cin >> dif;
- switch (dif)
- {
- case 1:
- difficulty = 5000;
- break;
- case 2:
- difficulty = 4000;
- break;
- case 3:
- difficulty = 3000;
- break;
- case 4:
- difficulty = 2000;
- break;
- case 5:
- difficulty = 1000;
- break;
- default:
- break;
- }
- system("cls");
- cout << "Lives: " << life << endl;
- CreateThread(NULL, 0, &Gent, hMutex, 0, NULL);
- FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
- while (true)
- {
- ReadConsoleInput(hInp, input, 1, &cb);
- if (input->EventType == KEY_EVENT)
- {
- if (input->Event.KeyEvent.bKeyDown)
- {
- switch (input->Event.KeyEvent.wVirtualKeyCode)
- {
- case kLeft:
- {
- FillConsoleOutputAttribute(hOut, 10, 1, c, &l);
- c.X--;
- FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
- break;
- }
- case kRight:
- {
- FillConsoleOutputAttribute(hOut, 10, 1, c, &l);
- c.X++;
- FillConsoleOutputAttribute(hOut, 10 << 4, 1, c, &l);
- break;
- }
- case kSpace:
- {
- HANDLE tShot = CreateThread(NULL, 0, &Shot, hMutex, 0, NULL);
- break;
- }
- case kEsc: ExitProcess(1);
- default:
- break;
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement