Advertisement
mamamaria

Balls4Max

Apr 1st, 2021 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.98 KB | None | 0 0
  1. #include <cmath>
  2. #include <windows.h>
  3. #include <thread>
  4. #include <mutex>
  5. using namespace std;
  6.  
  7. std::mutex mtx;
  8. std::mutex newmtx;
  9.  
  10. LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
  11.  
  12. const int r = 30;
  13. int progress[2] = { r, r };
  14. int winSize[2] = { 500, 500 };
  15. int speed[2] = { 10, 5 };
  16. bool check[] = { true, true };
  17.  
  18. int phase[2];
  19. void Phase() {
  20.     while (true) {
  21.         phase[0] = 1;
  22.         phase[1] = 2;
  23.         Sleep(5000);
  24.         phase[0] = 0;
  25.         phase[1] = 0;
  26.         Sleep(5000);
  27.         phase[0] = 2;
  28.         phase[1] = 1;
  29.         Sleep(5000);
  30.         phase[0] = 0;
  31.         phase[1] = 0;
  32.         Sleep(5000);
  33.     }
  34. }
  35. void THfunction(HWND hwnd, WNDCLASS w, int k)
  36. {
  37.     int x = r;
  38.     bool locked = false;
  39.     bool unlocked = false;
  40.     bool flocked = false;
  41.     bool funlocked = false;
  42.     /*newmtx.lock();*/
  43.     while (check[0] || check[1]) {
  44.         /*newmtx.unlock();*/
  45.         x += speed[k];
  46.         progress[k] = x;
  47.         Sleep(20);
  48.         if (x >= winSize[k] + r) {
  49.             OutputDebugStringW(L"Check");
  50.             check[k] = false;
  51.             break;
  52.         }
  53.         if (locked == false && x >= winSize[k] / 2 - r * 2.5) {
  54.             if (phase[k] == 1)
  55.                 while (phase[k] == 1) Sleep(20);
  56.             mtx.lock();
  57.             locked = true;
  58.         }
  59.         if (unlocked == false && x >= winSize[k] / 2 + r * 2.5) {
  60.             mtx.unlock();
  61.             unlocked = true;
  62.         }
  63.         if (flocked == false && x >= winSize[k] / 2 - r * 4) {
  64.             speed[k] -= 2;
  65.  
  66.             flocked = true;
  67.         }
  68.         if (funlocked == false && x >= winSize[k] / 2 + r * 3) {
  69.             speed[k] += 2;
  70.  
  71.             funlocked = true;
  72.         }
  73.         /*newmtx.lock();*/
  74.     }
  75.     /*newmtx.unlock();*/
  76.     progress[k] = x;
  77.  
  78. }
  79.  
  80. void DrawBall(HDC hdc, RECT rectClient, int x, int y)
  81. {
  82.     int height, width;
  83.     HBRUSH hBrush;
  84.     hBrush = CreateSolidBrush(RGB(50, 100, 100));
  85.     SelectObject(hdc, hBrush);
  86.     Ellipse(hdc, x - r, y - r, x + r, y + r);
  87.     DeleteObject(hBrush);
  88. }
  89.  
  90.  
  91. void DrawTL(HDC hdc, RECT rectClient, int x, int y, int colour)
  92. {
  93.    
  94.     int height, width;
  95.     HBRUSH hBrush; hBrush = CreateSolidBrush(RGB(0,1,1));
  96.     switch (colour) {
  97.     case(1): { //красный
  98.         hBrush = CreateSolidBrush(RGB(200, 0, 0));
  99.         break;
  100.     }
  101.     case(2): { //зеленый
  102.         hBrush = CreateSolidBrush(RGB(0, 200, 0));
  103.         break;
  104.     }
  105.     case(0): { //синий
  106.         hBrush = CreateSolidBrush(RGB(200, 180, 100));
  107.         break;
  108.         }
  109.     }
  110.    
  111.     SelectObject(hdc, hBrush);
  112.     Ellipse(hdc, x - r, y - r, x + r, y + r);
  113.     DeleteObject(hBrush);
  114. }
  115.  
  116. int WINAPI WinMain(HINSTANCE hInstance,
  117.     HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  118. {
  119.     WNDCLASS w;
  120.     memset(&w, 0, sizeof(WNDCLASS));
  121.     w.style = CS_HREDRAW | CS_VREDRAW;
  122.     w.lpfnWndProc = WndProc;
  123.     w.hInstance = hInstance;
  124.     w.hbrBackground = CreateSolidBrush(0x00FFFFFF);
  125.     w.lpszClassName = L"My Class";
  126.     RegisterClass(&w);
  127.  
  128.     HWND hwnd;
  129.     hwnd = CreateWindow(L"My Class", L"Катится мячик",
  130.         WS_OVERLAPPEDWINDOW,
  131.         0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL,
  132.         hInstance, NULL);
  133.     std::thread TH2(Phase);
  134.     ShowWindow(hwnd, nCmdShow);
  135.     while (true) {
  136.         /*speed[0] = rand() % 10 + 5;
  137.         speed[1] = rand() % 10 + 5;*/
  138.         std::thread TH0(THfunction, hwnd, w, 0);
  139.         std::thread TH1(THfunction, hwnd, w, 1);
  140.        
  141.  
  142.         OutputDebugStringW(L"Start");
  143.         /*newmtx.lock();*/
  144.         while (check[0] || check[1]) {
  145.             /*newmtx.unlock();*/
  146.             InvalidateRect(hwnd, 0, 1);
  147.             UpdateWindow(hwnd);
  148.             Sleep(20);
  149.             /*newmtx.lock();*/
  150.         }
  151.         /*newmtx.unlock();*/
  152.         OutputDebugStringW(L"Start join");
  153.         TH0.join();
  154.         TH1.join();
  155.        
  156.  
  157.         check[0] = true; check[1] = true;
  158.  
  159.         OutputDebugStringW(L"End join");
  160.     }
  161.     /*DestroyWindow(hwnd);*/
  162. }
  163.  
  164. LONG WINAPI WndProc(HWND hwnd, UINT Message,
  165.     WPARAM wparam, LPARAM lparam)
  166. {
  167.     HDC hdc;
  168.     PAINTSTRUCT ps;
  169.     HPEN hpen;
  170.     switch (Message)
  171.     {
  172.     case WM_PAINT: {
  173.         hdc = BeginPaint(hwnd, &ps);
  174.  
  175.         int width = winSize[0] = ps.rcPaint.right - ps.rcPaint.left;
  176.         int height = winSize[1] = ps.rcPaint.bottom - ps.rcPaint.top;
  177.  
  178.         DrawBall(hdc, ps.rcPaint, progress[0], height / 2);
  179.         DrawBall(hdc, ps.rcPaint, width / 2, progress[1]);
  180.         DrawTL(hdc, ps.rcPaint, (width / 2) + 300, (height / 2) + 100, phase[0]);
  181.         OutputDebugStringW(L"HELP");
  182.         DrawTL(hdc, ps.rcPaint, (width / 2) - 100, (height / 2) + 300, phase[1]);
  183.         hpen = CreatePen(PS_SOLID, 5, 0);
  184.  
  185.         SelectObject(hdc, hpen);
  186.         int offset = r * 2;
  187.         MoveToEx(hdc, width / 2 - offset, 0, 0);
  188.         LineTo(hdc, width / 2 - offset, height / 2 - offset);
  189.         MoveToEx(hdc, width / 2 + offset, 0, 0);
  190.         LineTo(hdc, width / 2 + offset, height / 2 - offset);
  191.         MoveToEx(hdc, width / 2 - offset, height, 0);
  192.         LineTo(hdc, width / 2 - offset, height / 2 + offset);
  193.         MoveToEx(hdc, width / 2 + offset, height, 0);
  194.         LineTo(hdc, width / 2 + offset, height / 2 + offset);
  195.  
  196.         MoveToEx(hdc, 0, height / 2 - offset, 0);
  197.         LineTo(hdc, width / 2 - offset, height / 2 - offset);
  198.         MoveToEx(hdc, 0, height / 2 + offset, 0);
  199.         LineTo(hdc, width / 2 - offset, height / 2 + offset);
  200.         MoveToEx(hdc, width, height / 2 - offset, 0);
  201.         LineTo(hdc, width / 2 + offset, height / 2 - offset);
  202.         MoveToEx(hdc, width, height / 2 + offset, 0);
  203.         LineTo(hdc, width / 2 + offset, height / 2 + offset);
  204.  
  205.         EndPaint(hwnd, &ps);
  206.         break;
  207.     }
  208.     case WM_DESTROY:
  209.         PostQuitMessage(0);
  210.         break;
  211.     default:
  212.         return DefWindowProc(hwnd, Message, wparam, lparam);
  213.     }
  214.  
  215.     return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement