Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <Windows.h>
- #include "stdio.h"
- TCHAR Class_Name[] = TEXT("OKNO_TEST");
- MSG Komunikat;
- int zmienna = 0;
- char ilosc[128];
- HINSTANCE *hInst;
- HWND okno1;
- const WORD ID_TIMER = 1;
- RECT Kula, Old, OldA,OldB;
- WORD KulkaX = 100, KulkaY = 100;
- POINT wps;
- RECT WymiaryOkna;
- RECT A, B;
- bool LeciPrawo = true;
- bool wDol = true;
- HBRUSH brush_gracz, brush_kulka;
- HPEN pen_gracz, pen_pilka;
- RECT start;
- void RysujKulke()
- {
- // wylicz nowe parametry kulki
- // if (KulkaX <= 0 || KulkaX >= ptOkno.x) SpeedX = -SpeedX;
- //if (KulkaY <= 0 || KulkaY >= ptOkno.y) SpeedY = -SpeedY;
- //KulkaX += SpeedX;
- // KulkaY += SpeedY;
- // narysuj na nowej pozycji
- HDC hdc = GetDC(okno1);
- // BitBlt(hdc, KulkaX, KulkaY, bmKulka.bmWidth, bmKulka.bmHeight, hdcMem, 0, 0, SRCCOPY);
- //Ellipse(hdc, Kula.left, 60, 40, 40);
- FillRect(hdc, &WymiaryOkna, (HBRUSH)GetStockObject(COLOR_WINDOW + 1));
- FillRect(hdc, &OldA, (HBRUSH)GetStockObject(COLOR_WINDOW + 1));
- FillRect(hdc, &Old, (HBRUSH)GetStockObject(COLOR_WINDOW + 1));
- SelectObject(hdc, brush_kulka);
- SelectObject(hdc, pen_pilka);
- Ellipse(hdc, Kula.left, Kula.top, Kula.right, Kula.bottom);
- SelectObject(hdc, brush_gracz);
- SelectObject(hdc, pen_gracz);
- Rectangle(hdc, A.left, A.top, A.right, A.bottom);
- Rectangle(hdc, B.left, B.top, B.right, B.bottom);
- //Ellipse(hdc, 100, 100, 120, 120);
- // Ellipse(hdc, 120, 60, 40, 40);
- ReleaseDC(okno1, hdc);
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- GetClientRect(hwnd, &WymiaryOkna);
- switch (msg) {
- case WM_CREATE:
- {
- SetTimer(hwnd, 1, 16, NULL);
- Kula.left = 490;//30
- Kula.top = 200;//30
- Kula.right = 510;//10
- Kula.bottom = 220;//10
- wps.x = 50;
- wps.y = 60;
- A.left = 10;
- A.top = 210;
- A.right = 30;
- A.bottom = 290;
- B.left = 950;
- B.top = 210;
- B.right = 970;
- B.bottom = 290;
- start = Kula;
- brush_gracz = CreateSolidBrush(RGB(255, 128, 36));
- brush_kulka = CreateSolidBrush(RGB(255, 0, 0));
- pen_gracz = CreatePen(PS_SOLID, 3, RGB(0, 20, 222));
- pen_pilka = CreatePen(PS_SOLID, 3, RGB(0, 20, 222));
- }
- break;
- case WM_TIMER:
- Old = Kula;
- /////////////////////////////////////////////////
- if (wDol == true && LeciPrawo == true)
- {
- Kula.left += 8;
- Kula.top -= 4;
- Kula.right += 8;
- Kula.bottom -= 4;
- if (Kula.top < WymiaryOkna.top+50)
- {
- wDol = false;
- }
- if (Kula.right > B.left)
- {
- if (!(Kula.top > B.top && Kula.bottom < B.bottom) )
- {
- LeciPrawo = false;
- wDol = rand() % 2;
- Kula = start;
- }
- else
- {
- LeciPrawo = false;
- wDol = rand() % 2;
- }
- }
- }
- ////////////////////////////////////////////////
- if (wDol == false && LeciPrawo == true)
- {
- Kula.left += 8;
- Kula.top += 4;
- Kula.right += 8;
- Kula.bottom += 4;
- if (Kula.bottom > WymiaryOkna.bottom -30)
- {
- wDol = true;
- }
- if (Kula.right > B.left)
- {
- if (!(Kula.top > B.top && Kula.bottom < B.bottom))
- {
- LeciPrawo = false;
- wDol = rand() % 2;
- Kula = start;
- }
- else
- {
- LeciPrawo = false;
- wDol = rand() % 2;
- }
- }
- }
- ///////////////////////////////////////////////
- if (wDol == true && LeciPrawo == false)
- {
- Kula.left -= 8;
- Kula.top += 4;
- Kula.right -= 8;
- Kula.bottom += 4;
- if (Kula.bottom > WymiaryOkna.bottom-50)
- {
- wDol = false;
- }
- if (Kula.right < A.right)
- {
- if (!(Kula.top > A.top && Kula.bottom < A.bottom))
- {
- LeciPrawo = true;
- wDol = rand() % 2;
- Kula = start;
- }
- else
- {
- LeciPrawo = true;
- wDol = rand() % 2;
- }
- /*
- static char buff[50];
- wsprintfA(buff, "Przeciwnik ma %d żyć", przeciwnik.hp);
- SetWindowTextA(hwnd, buff);
- LeciPrawo = true;
- wDol = rand() % 2;*/
- }
- }
- /////////////////////////////////////////////
- if (wDol == false && LeciPrawo == false)
- {
- Kula.left -= 8;
- Kula.top -= 4;
- Kula.right -= 8;
- Kula.bottom -= 4;
- if (Kula.top < WymiaryOkna.top + 50)
- {
- wDol = true;
- }
- if (Kula.right < A.right)
- {
- if (!(Kula.top > A.top && Kula.bottom < A.bottom))
- {
- LeciPrawo = true;
- wDol = rand() % 2;
- Kula = start;
- }
- else
- {
- LeciPrawo = true;
- wDol = rand() % 2;
- }
- /*
- LeciPrawo = true;
- wDol = rand() % 2;*/
- }
- }
- RysujKulke();
- if (GetAsyncKeyState(VK_NUMPAD4)) { // gracz a do gory
- if (A.top > 0)
- {
- OldA = A;
- A.top -= 8;
- A.bottom -= 8;
- }
- }
- if (GetAsyncKeyState(VK_NUMPAD1)) { // gracz a do dół
- if (A.bottom < 465)
- {
- OldA = A;
- A.top += 8;
- A.bottom += 8;
- }
- }
- if (GetAsyncKeyState(VK_NUMPAD6)) { // gracz B do gory
- if (B.top > 0)
- {
- OldB = B;
- B.top -= 8;
- B.bottom -= 8;
- }
- }
- if (GetAsyncKeyState(VK_NUMPAD3)) { // gracz B do dół
- if (B.bottom < 465)
- {
- OldB = B;
- B.top += 8;
- B.bottom += 8;
- }
- }
- break;
- case WM_LBUTTONDOWN:
- zmienna++;
- sprintf_s(ilosc, "%d", zmienna);
- SetWindowTextA(hwnd, ilosc);
- break;
- case WM_CLOSE:
- if ((MessageBox(hwnd, TEXT("Czy chcesz wyłączyć okno ?"), TEXT("Pytanie"), MB_OKCANCEL)) == IDOK)
- DestroyWindow(hwnd);
- else break;
- case WM_DESTROY:
- KillTimer(hwnd, ID_TIMER);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- hInst = &hInstance;
- WNDCLASSEX window;
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- window.hCursor = LoadCursor(*hInst, IDC_ARROW);
- window.hIcon = LoadIcon(*hInst, IDI_APPLICATION);
- window.hIconSm = NULL;
- window.hInstance = *hInst;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = Class_Name;
- window.lpszMenuName = 0;
- window.style = NULL;
- if (!RegisterClassEx(&window)) {
- MessageBox(NULL, TEXT("Zarejestrowanie klasy nieudane! =("), TEXT("WARNING"), MB_OK | MB_ICONWARNING);
- return 1;
- }
- okno1 = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, TEXT("Okno 1"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1000, 500, NULL, NULL, *hInst, NULL);
- if (okno1 == NULL) {
- MessageBox(NULL, TEXT("Stworzenie okna nieudane! =("), TEXT("WARNING"), MB_OK | MB_ICONWARNING);
- return 2;
- }
- ShowWindow(okno1, nCmdShow);
- UpdateWindow(okno1);
- while (GetMessage(&Komunikat, NULL, 0, 0)) {
- TranslateMessage(&Komunikat);
- DispatchMessage(&Komunikat);
- }
- UnregisterClass(Class_Name, *hInst);
- return Komunikat.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement