Advertisement
Cieslin

PIU_Pong_by_Cieslin

Dec 5th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.09 KB | None | 0 0
  1. #include <windows.h>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. #define WINDOW_X 1240
  6. #define WINDOW_Y 720
  7. #define MAP_X 80
  8. #define MAP_Y 100
  9. #define TIME_ID 1
  10. #define TIME_ID_2 2
  11.  
  12. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  13.  
  14.  
  15. const COLORREF backgroundColor = RGB(0, 0, 0);
  16. const COLORREF objectsColor = RGB(255, 255, 255);
  17. const COLORREF ballColor = RGB(0, 128, 0);
  18.  
  19. /*enum Kierunek {
  20.     GORA = 1,
  21.     GORA_PRAWO = 2,
  22.     PRAWO = 3,
  23.     PRAWO_DOL = 4,
  24.     DOL_LEWO = 5,
  25.     LEWO = 6,
  26.     LEWO_GORA = 7
  27. };*/
  28.  
  29. HINSTANCE hInstance;
  30. RECT windowRect;
  31. HDC hdc;
  32.  
  33. struct Paletka {
  34.     int gora;
  35.     int dol;
  36.     int prawo;
  37.     int lewo;
  38. };
  39.  
  40. Paletka p1, p2, p3;
  41.  
  42. bool RejestracjaKlasy(WNDCLASSEX &window)
  43. {
  44.     window.cbSize = sizeof(WNDCLASSEX);
  45.     window.style = CS_VREDRAW | CS_HREDRAW;
  46.     window.lpfnWndProc = ProceduraOkna;
  47.     window.cbClsExtra = 0;
  48.     window.cbWndExtra = 0;
  49.     window.hInstance = hInstance;
  50.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  51.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  52.     window.hbrBackground = (HBRUSH)CreateSolidBrush(backgroundColor);
  53.     window.lpszMenuName = NULL;
  54.     window.lpszClassName = "StandardoweOkno";
  55.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  56.  
  57.     if (!RegisterClassEx(&window))
  58.     {
  59.         MessageBox(NULL, "Problem z rejestracją okna", "Rejestracja okna", MB_ICONERROR | MB_OK);
  60.         return false;
  61.     }
  62.     return true;
  63. }
  64.  
  65. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCMD)
  66. {
  67.     WNDCLASSEX window;
  68.     if (!RejestracjaKlasy(window))
  69.         return 1;
  70.  
  71.  
  72.     HWND uchwytOkna = 0;
  73.  
  74.     uchwytOkna = CreateWindowEx(
  75.         WS_EX_APPWINDOW | WS_EX_CLIENTEDGE,
  76.         "StandardoweOkno",
  77.         "Pierwsze okno",
  78.         WS_OVERLAPPEDWINDOW,
  79.         CW_USEDEFAULT,
  80.         CW_USEDEFAULT,
  81.         WINDOW_X,
  82.         WINDOW_Y,
  83.         0,
  84.         0,
  85.         hInstance,
  86.         0);
  87.  
  88.     ShowWindow(uchwytOkna, nShowCMD);
  89.     UpdateWindow(uchwytOkna);
  90.  
  91.     MSG msg;
  92.  
  93.     for (; ;)
  94.     {
  95.         if (0 != GetMessage(&msg, 0, 0, 0))
  96.         {
  97.             TranslateMessage(&msg);
  98.             DispatchMessage(&msg);
  99.         }
  100.         if (msg.message == WM_QUIT) break;
  101.     }
  102.  
  103.     return (int)msg.wParam;
  104. }
  105.  
  106. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  107. {
  108.     static HPEN whitePen;
  109.     static HPEN greenPen;
  110.     static HBRUSH whiteBrush;
  111.     static HBRUSH greenBrush;
  112.     static PAINTSTRUCT paint;
  113.     static int kierunek;
  114.     static bool isStart;
  115.     switch (message)
  116.     {
  117.     case WM_CLOSE:
  118.         if (MessageBox(hWnd, "Czy chcesz zakonczyc dzialanie programu?", "Dzialanie programu", MB_YESNO | MB_ICONERROR) == IDNO)
  119.             break;
  120.     case WM_DESTROY:
  121.         KillTimer(hWnd, TIME_ID);
  122.         KillTimer(hWnd, TIME_ID_2);
  123.         PostQuitMessage(0);
  124.         break;
  125.     case WM_CREATE:
  126.         SetTimer(hWnd, TIME_ID, 100, NULL);
  127.         SetTimer(hWnd, TIME_ID_2, 80, NULL);
  128.         srand((unsigned)time(NULL));
  129.         kierunek = 6;
  130.         GetClientRect(hWnd, &windowRect);
  131.         p1.lewo = (windowRect.left + 5);
  132.         p1.gora = (windowRect.bottom / 2) - 40;
  133.         p1.prawo = (windowRect.left + 20);
  134.         p1.dol = (windowRect.bottom / 2) + 40;
  135.         p2.lewo = (windowRect.right - 5);
  136.         p2.prawo = (windowRect.right - 20);
  137.         p2.gora = (windowRect.bottom / 2) - 40;
  138.         p2.dol = (windowRect.bottom / 2) + 40;
  139.         p3.lewo = (windowRect.right / 2) - 10;
  140.         p3.prawo = (windowRect.right / 2) + 10;
  141.         p3.gora = (windowRect.bottom / 2) - 10;
  142.         p3.dol = (windowRect.bottom / 2) + 10;
  143.         isStart = false;
  144.         whiteBrush = CreateSolidBrush(objectsColor);
  145.         greenBrush = CreateSolidBrush(ballColor);
  146.         whitePen = CreatePen(PS_SOLID, 1, objectsColor);
  147.         greenPen = CreatePen(PS_SOLID, 1, ballColor);
  148.         break;
  149.     case WM_PAINT:
  150.     {
  151.         hdc = BeginPaint(hWnd, &paint);
  152.         SelectObject(hdc, whitePen);
  153.         SelectObject(hdc, whiteBrush);
  154.         MoveToEx(hdc, windowRect.right / 2, 0, NULL);
  155.         LineTo(hdc, windowRect.right / 2, windowRect.bottom);
  156.        
  157.         /*SetMapMode(hdc, MM_ANISOTROPIC);
  158.         SetViewportExtEx(hdc, windowRect.right, windowRect.bottom, NULL);
  159.         SetViewportOrgEx(hdc, 0, 0, NULL);
  160.         SetWindowExtEx(hdc, MAP_X, MAP_Y, NULL);
  161.         SetWindowOrgEx(hdc, 0, 0, NULL);*/
  162.  
  163.         Rectangle(hdc, p1.lewo, p1.gora, p1.prawo, p1.dol);
  164.         Rectangle(hdc, p2.lewo, p2.gora, p2.prawo, p2.dol);
  165.  
  166.         SelectObject(hdc, greenBrush);
  167.         SelectObject(hdc, greenPen);
  168.  
  169.         Ellipse(hdc, p3.lewo, p3.gora, p3.prawo, p3.dol);
  170.  
  171.  
  172.         EndPaint(hWnd, &paint);
  173.     }
  174.         break;
  175.     case WM_SIZE:
  176.         GetClientRect(hWnd, &windowRect);
  177.         break;
  178.     case WM_KEYDOWN:
  179.         switch (wParam)
  180.         {
  181.         case 0x41:
  182.             if (p1.gora > windowRect.top)
  183.             {
  184.                 p1.gora -= 10;
  185.                 p1.dol -= 10;
  186.             }
  187.             break;
  188.         case 0x5A:
  189.             if (p1.dol < windowRect.bottom)
  190.             {
  191.                 p1.dol += 10;
  192.                 p1.gora += 10;
  193.             }
  194.             break;
  195.         case VK_UP:
  196.             if (p2.gora > windowRect.top)
  197.             {
  198.                 p2.gora -= 10;
  199.                 p2.dol -= 10;
  200.             }
  201.             break;
  202.         case VK_DOWN:
  203.             if (p2.dol < windowRect.bottom)
  204.             {
  205.                 p2.dol += 10;
  206.                 p2.gora += 10;
  207.             }
  208.             break;
  209.         case VK_SPACE:
  210.             isStart = true;
  211.         }
  212.         break;
  213.     case WM_TIMER:
  214.         if (p3.prawo >= windowRect.right)
  215.         {
  216.             p1.lewo = (windowRect.left + 5);
  217.             p1.gora = (windowRect.bottom / 2) - 40;
  218.             p1.prawo = (windowRect.left + 20);
  219.             p1.dol = (windowRect.bottom / 2) + 40;
  220.             p2.lewo = (windowRect.right - 5);
  221.             p2.prawo = (windowRect.right - 20);
  222.             p2.gora = (windowRect.bottom / 2) - 40;
  223.             p2.dol = (windowRect.bottom / 2) + 40;
  224.             p3.lewo = (windowRect.right / 2) - 10;
  225.             p3.prawo = (windowRect.right / 2) + 10;
  226.             p3.gora = (windowRect.bottom / 2) - 10;
  227.             p3.dol = (windowRect.bottom / 2) + 10;
  228.             isStart = false;
  229.             MessageBox(hWnd, "Wygral Player 1!", "WYGRANA", MB_OK);
  230.         }
  231.         if (p3.lewo <= windowRect.left)
  232.         {
  233.             p1.lewo = (windowRect.left + 5);
  234.             p1.gora = (windowRect.bottom / 2) - 40;
  235.             p1.prawo = (windowRect.left + 20);
  236.             p1.dol = (windowRect.bottom / 2) + 40;
  237.             p2.lewo = (windowRect.right - 5);
  238.             p2.prawo = (windowRect.right - 20);
  239.             p2.gora = (windowRect.bottom / 2) - 40;
  240.             p2.dol = (windowRect.bottom / 2) + 40;
  241.             p3.lewo = (windowRect.right / 2) - 10;
  242.             p3.prawo = (windowRect.right / 2) + 10;
  243.             p3.gora = (windowRect.bottom / 2) - 10;
  244.             p3.dol = (windowRect.bottom / 2) + 10;
  245.             isStart = false;
  246.             MessageBox(hWnd, "Wygral Player 2!", "WYGRANA", MB_OK);
  247.         }
  248.         if (isStart)
  249.         {
  250.             if ((p3.dol >= p1.gora && p3.lewo == p1.prawo && p3.gora <= p1.dol) || (p3.dol >= p2.gora && p3.lewo == p2.prawo && p3.gora <= p2.dol) || p3.gora <= windowRect.top || p3.dol >= windowRect.bottom)
  251.                 kierunek = rand() % 7;
  252.             switch (kierunek)
  253.             {
  254.             case 0:
  255.                 p3.gora -= 5;
  256.                 p3.dol -= 5;
  257.                 break;
  258.             case 1:
  259.                 p3.gora -= 5;
  260.                 p3.dol -= 5;
  261.                 p3.prawo += 5;
  262.                 p3.lewo += 5;
  263.                 break;
  264.             case 2:
  265.                 p3.prawo += 5;
  266.                 p3.lewo += 5;
  267.                 break;
  268.             case 3:
  269.                 p3.prawo += 5;
  270.                 p3.lewo += 5;
  271.                 p3.dol += 5;
  272.                 p3.gora += 5;
  273.                 break;
  274.             case 4:
  275.                 p3.dol += 5;
  276.                 p3.gora += 5;
  277.                 break;
  278.             case 5:
  279.                 p3.dol += 5;
  280.                 p3.gora += 5;
  281.                 p3.lewo -= 5;
  282.                 p3.prawo -= 5;
  283.                 break;
  284.             case 6:
  285.                 p3.lewo -= 5;
  286.                 p3.prawo -= 5;
  287.                 break;
  288.             case 7:
  289.                 p3.lewo -= 5;
  290.                 p3.prawo -= 5;
  291.                 p3.gora -= 5;
  292.                 p3.dol -= 5;
  293.                 break;
  294.             }
  295.             InvalidateRect(hWnd, &windowRect, 1);
  296.         }
  297.         break;
  298.     default:
  299.         return DefWindowProc(hWnd, message, wParam, lParam);
  300.     }
  301.     return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement