Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <ctime>
- #include <iostream>
- #define WINDOW_X 1240
- #define WINDOW_Y 720
- #define MAP_X 80
- #define MAP_Y 100
- #define TIME_ID 1
- #define TIME_ID_2 2
- LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- const COLORREF backgroundColor = RGB(0, 0, 0);
- const COLORREF objectsColor = RGB(255, 255, 255);
- const COLORREF ballColor = RGB(0, 128, 0);
- /*enum Kierunek {
- GORA = 1,
- GORA_PRAWO = 2,
- PRAWO = 3,
- PRAWO_DOL = 4,
- DOL_LEWO = 5,
- LEWO = 6,
- LEWO_GORA = 7
- };*/
- HINSTANCE hInstance;
- RECT windowRect;
- HDC hdc;
- struct Paletka {
- int gora;
- int dol;
- int prawo;
- int lewo;
- };
- Paletka p1, p2, p3;
- bool RejestracjaKlasy(WNDCLASSEX &window)
- {
- window.cbSize = sizeof(WNDCLASSEX);
- window.style = CS_VREDRAW | CS_HREDRAW;
- window.lpfnWndProc = ProceduraOkna;
- window.cbClsExtra = 0;
- window.cbWndExtra = 0;
- window.hInstance = hInstance;
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hbrBackground = (HBRUSH)CreateSolidBrush(backgroundColor);
- window.lpszMenuName = NULL;
- window.lpszClassName = "StandardoweOkno";
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&window))
- {
- MessageBox(NULL, "Problem z rejestracją okna", "Rejestracja okna", MB_ICONERROR | MB_OK);
- return false;
- }
- return true;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCMD)
- {
- WNDCLASSEX window;
- if (!RejestracjaKlasy(window))
- return 1;
- HWND uchwytOkna = 0;
- uchwytOkna = CreateWindowEx(
- WS_EX_APPWINDOW | WS_EX_CLIENTEDGE,
- "StandardoweOkno",
- "Pierwsze okno",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- WINDOW_X,
- WINDOW_Y,
- 0,
- 0,
- hInstance,
- 0);
- ShowWindow(uchwytOkna, nShowCMD);
- UpdateWindow(uchwytOkna);
- MSG msg;
- for (; ;)
- {
- if (0 != GetMessage(&msg, 0, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- if (msg.message == WM_QUIT) break;
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static HPEN whitePen;
- static HPEN greenPen;
- static HBRUSH whiteBrush;
- static HBRUSH greenBrush;
- static PAINTSTRUCT paint;
- static int kierunek;
- static bool isStart;
- switch (message)
- {
- case WM_CLOSE:
- if (MessageBox(hWnd, "Czy chcesz zakonczyc dzialanie programu?", "Dzialanie programu", MB_YESNO | MB_ICONERROR) == IDNO)
- break;
- case WM_DESTROY:
- KillTimer(hWnd, TIME_ID);
- KillTimer(hWnd, TIME_ID_2);
- PostQuitMessage(0);
- break;
- case WM_CREATE:
- SetTimer(hWnd, TIME_ID, 100, NULL);
- SetTimer(hWnd, TIME_ID_2, 80, NULL);
- srand((unsigned)time(NULL));
- kierunek = 6;
- GetClientRect(hWnd, &windowRect);
- p1.lewo = (windowRect.left + 5);
- p1.gora = (windowRect.bottom / 2) - 40;
- p1.prawo = (windowRect.left + 20);
- p1.dol = (windowRect.bottom / 2) + 40;
- p2.lewo = (windowRect.right - 5);
- p2.prawo = (windowRect.right - 20);
- p2.gora = (windowRect.bottom / 2) - 40;
- p2.dol = (windowRect.bottom / 2) + 40;
- p3.lewo = (windowRect.right / 2) - 10;
- p3.prawo = (windowRect.right / 2) + 10;
- p3.gora = (windowRect.bottom / 2) - 10;
- p3.dol = (windowRect.bottom / 2) + 10;
- isStart = false;
- whiteBrush = CreateSolidBrush(objectsColor);
- greenBrush = CreateSolidBrush(ballColor);
- whitePen = CreatePen(PS_SOLID, 1, objectsColor);
- greenPen = CreatePen(PS_SOLID, 1, ballColor);
- break;
- case WM_PAINT:
- {
- hdc = BeginPaint(hWnd, &paint);
- SelectObject(hdc, whitePen);
- SelectObject(hdc, whiteBrush);
- MoveToEx(hdc, windowRect.right / 2, 0, NULL);
- LineTo(hdc, windowRect.right / 2, windowRect.bottom);
- /*SetMapMode(hdc, MM_ANISOTROPIC);
- SetViewportExtEx(hdc, windowRect.right, windowRect.bottom, NULL);
- SetViewportOrgEx(hdc, 0, 0, NULL);
- SetWindowExtEx(hdc, MAP_X, MAP_Y, NULL);
- SetWindowOrgEx(hdc, 0, 0, NULL);*/
- Rectangle(hdc, p1.lewo, p1.gora, p1.prawo, p1.dol);
- Rectangle(hdc, p2.lewo, p2.gora, p2.prawo, p2.dol);
- SelectObject(hdc, greenBrush);
- SelectObject(hdc, greenPen);
- Ellipse(hdc, p3.lewo, p3.gora, p3.prawo, p3.dol);
- EndPaint(hWnd, &paint);
- }
- break;
- case WM_SIZE:
- GetClientRect(hWnd, &windowRect);
- break;
- case WM_KEYDOWN:
- switch (wParam)
- {
- case 0x41:
- if (p1.gora > windowRect.top)
- {
- p1.gora -= 10;
- p1.dol -= 10;
- }
- break;
- case 0x5A:
- if (p1.dol < windowRect.bottom)
- {
- p1.dol += 10;
- p1.gora += 10;
- }
- break;
- case VK_UP:
- if (p2.gora > windowRect.top)
- {
- p2.gora -= 10;
- p2.dol -= 10;
- }
- break;
- case VK_DOWN:
- if (p2.dol < windowRect.bottom)
- {
- p2.dol += 10;
- p2.gora += 10;
- }
- break;
- case VK_SPACE:
- isStart = true;
- }
- break;
- case WM_TIMER:
- if (p3.prawo >= windowRect.right)
- {
- p1.lewo = (windowRect.left + 5);
- p1.gora = (windowRect.bottom / 2) - 40;
- p1.prawo = (windowRect.left + 20);
- p1.dol = (windowRect.bottom / 2) + 40;
- p2.lewo = (windowRect.right - 5);
- p2.prawo = (windowRect.right - 20);
- p2.gora = (windowRect.bottom / 2) - 40;
- p2.dol = (windowRect.bottom / 2) + 40;
- p3.lewo = (windowRect.right / 2) - 10;
- p3.prawo = (windowRect.right / 2) + 10;
- p3.gora = (windowRect.bottom / 2) - 10;
- p3.dol = (windowRect.bottom / 2) + 10;
- isStart = false;
- MessageBox(hWnd, "Wygral Player 1!", "WYGRANA", MB_OK);
- }
- if (p3.lewo <= windowRect.left)
- {
- p1.lewo = (windowRect.left + 5);
- p1.gora = (windowRect.bottom / 2) - 40;
- p1.prawo = (windowRect.left + 20);
- p1.dol = (windowRect.bottom / 2) + 40;
- p2.lewo = (windowRect.right - 5);
- p2.prawo = (windowRect.right - 20);
- p2.gora = (windowRect.bottom / 2) - 40;
- p2.dol = (windowRect.bottom / 2) + 40;
- p3.lewo = (windowRect.right / 2) - 10;
- p3.prawo = (windowRect.right / 2) + 10;
- p3.gora = (windowRect.bottom / 2) - 10;
- p3.dol = (windowRect.bottom / 2) + 10;
- isStart = false;
- MessageBox(hWnd, "Wygral Player 2!", "WYGRANA", MB_OK);
- }
- if (isStart)
- {
- 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)
- kierunek = rand() % 7;
- switch (kierunek)
- {
- case 0:
- p3.gora -= 5;
- p3.dol -= 5;
- break;
- case 1:
- p3.gora -= 5;
- p3.dol -= 5;
- p3.prawo += 5;
- p3.lewo += 5;
- break;
- case 2:
- p3.prawo += 5;
- p3.lewo += 5;
- break;
- case 3:
- p3.prawo += 5;
- p3.lewo += 5;
- p3.dol += 5;
- p3.gora += 5;
- break;
- case 4:
- p3.dol += 5;
- p3.gora += 5;
- break;
- case 5:
- p3.dol += 5;
- p3.gora += 5;
- p3.lewo -= 5;
- p3.prawo -= 5;
- break;
- case 6:
- p3.lewo -= 5;
- p3.prawo -= 5;
- break;
- case 7:
- p3.lewo -= 5;
- p3.prawo -= 5;
- p3.gora -= 5;
- p3.dol -= 5;
- break;
- }
- InvalidateRect(hWnd, &windowRect, 1);
- }
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement