Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <windows.h>
  2. #include <list>
  3. #include <math.h>
  4. #define LEWO -1
  5. #define PRAWO 1
  6.  
  7. using namespace std;
  8.  
  9. HBRUSH Niebieski, Czerwony;
  10.  
  11. class Statek {
  12.     int PozX, PozY, Zwr;
  13. public:
  14.     Statek(int PozycjaX, int PozycjaY, int Zwrot) : PozX(PozycjaX), PozY(PozycjaY), Zwr(Zwrot) {}
  15.     POINT Pozycja() const;
  16.     void Rysuj(const HDC DC) const;
  17.     void Przesun(const int dx, const int dy) { PozX += dx*Zwr; PozY += dy; }
  18.     bool Kolizja(const Statek&) const;
  19. };
  20.  
  21. POINT Statek::Pozycja() const {
  22.     POINT Pozycja;
  23.     Pozycja.x = PozX; Pozycja.y = PozY;
  24.     return Pozycja;
  25. }
  26.  
  27. void Statek::Rysuj(const HDC DC) const {
  28.     Rectangle(DC, PozX-(31*Zwr), PozY-10, PozX+(9*Zwr), PozY+10);
  29.     Ellipse(DC, PozX-(11*Zwr), PozY-25, PozX+(39*Zwr), PozY+25);
  30.     POINT P[4];
  31.     P[0].x = PozX-(21*Zwr); P[0].y = PozY-5; P[1].x = PozX-(26*Zwr); P[1].y = PozY-5; P[2].x = PozX-(31*Zwr); P[2].y = PozY-25; P[3].x = PozX-(26*Zwr); P[3].y = PozY-25;
  32.     Polygon(DC, P, 4);
  33.     P[0].y = PozY+5; P[1].y = PozY+5; P[2].y = PozY+25; P[3].y = PozY+25;
  34.     Polygon(DC, P, 4);
  35.     Rectangle(DC, PozX-(36*Zwr), PozY-25, PozX-(16*Zwr), PozY-20);
  36.     Rectangle(DC, PozX-(36*Zwr), PozY+25, PozX-(16*Zwr), PozY+20);
  37.     int prevdc = SaveDC(DC);
  38.     SelectObject(DC, Niebieski);
  39.     Rectangle(DC, PozX-(15*Zwr), PozY-25, PozX-(12*Zwr), PozY-20);
  40.     Rectangle(DC, PozX-(15*Zwr), PozY+25, PozX-(12*Zwr), PozY+20);
  41.     SelectObject(DC, Czerwony);
  42.     Rectangle(DC, PozX-(39*Zwr), PozY-25, PozX-(35*Zwr), PozY-20);
  43.     Rectangle(DC, PozX-(39*Zwr), PozY+25, PozX-(35*Zwr), PozY+20);
  44.     RestoreDC(DC, prevdc);
  45. }
  46.  
  47. bool Statek::Kolizja(const Statek& S) const {
  48.     if (abs(PozX-S.Pozycja().x) <= 78 && abs(PozY-S.Pozycja().y) <= 50) return true;
  49.     return false;
  50. }
  51.  
  52. HDC dc;
  53. PAINTSTRUCT ps;
  54. RECT r;
  55. list<Statek> lista;
  56. list<Statek>::iterator it, it2;
  57. bool k;
  58.  
  59. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  60.     switch(msg) {
  61.     case WM_PAINT:
  62.         dc = BeginPaint(hWnd, &ps);
  63.         GetClientRect(hWnd, &r);
  64.         SetMapMode(dc, MM_ISOTROPIC);
  65.         SetWindowExtEx(dc, 1000, 1000, NULL);
  66.         SetViewportExtEx(dc, r.right, r.bottom, NULL);
  67.         for (it = lista.begin(); it != lista.end(); ++it) {
  68.             it->Rysuj(dc);
  69.         }
  70.         EndPaint(hWnd, &ps);
  71.     break;
  72.     case WM_LBUTTONDOWN:
  73.         for (it = lista.begin(); it != lista.end(); ++it) {
  74.             k = false;
  75.             for (it2 = lista.begin(); it2 != lista.end(); ++it2) {
  76.                 if (it2 != it) {
  77.                     if (it->Kolizja(*it2)) k = true;
  78.                 }
  79.             }
  80.             if (!k) it->Przesun(25, 0);
  81.         }
  82.         GetClientRect(hWnd, &r);
  83.         InvalidateRect(hWnd, &r, true);
  84.     break;
  85.     case WM_DESTROY:
  86.         PostQuitMessage(0);
  87.     break;
  88.     default:
  89.         return DefWindowProc(hWnd, msg, wParam, lParam);
  90.     break;
  91.     }
  92.     return 0;
  93. }
  94.  
  95. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
  96.     HWND hWnd;
  97.     MSG msg;
  98.     WNDCLASSEX wc;
  99.     wc.cbClsExtra = 0;
  100.     wc.cbSize = sizeof(WNDCLASSEX);
  101.     wc.cbWndExtra = 0;
  102.     wc.hbrBackground = (HBRUSH)(8);
  103.     wc.hCursor = 0;
  104.     wc.hIcon = 0;
  105.     wc.hIconSm = 0;
  106.     wc.hInstance = hInstance;
  107.     wc.lpfnWndProc = WndProc;
  108.     wc.lpszClassName = L"ENTERPRISE";
  109.     wc.lpszMenuName = 0;
  110.     wc.style = CS_VREDRAW | CS_HREDRAW;
  111.     if(!RegisterClassEx(&wc)) return 0;
  112.     lista.push_back(Statek(180, 55, PRAWO));
  113.     lista.push_back(Statek(100, 200, PRAWO));
  114.     lista.push_back(Statek(110, 260, PRAWO));
  115.     lista.push_back(Statek(50, 470, PRAWO));
  116.     lista.push_back(Statek(250, 780, PRAWO));
  117.     lista.push_back(Statek(900, 220, LEWO));
  118.     lista.push_back(Statek(950, 370, LEWO));
  119.     lista.push_back(Statek(870, 450, LEWO));
  120.     lista.push_back(Statek(790, 540, LEWO));
  121.     lista.push_back(Statek(920, 800, LEWO));
  122.     hWnd = CreateWindowEx(0, L"ENTERPRISE", L"Enterprajz", WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, 0, 0, hInstance, 0);
  123.     Niebieski = CreateSolidBrush(RGB(50,50,255));
  124.     Czerwony = CreateSolidBrush(RGB(255,50,50));
  125.     ShowWindow(hWnd, SW_SHOW);
  126.     UpdateWindow(hWnd);
  127.     while (GetMessage(&msg, NULL, 0, 0) != 0) {
  128.         TranslateMessage(&msg);
  129.         DispatchMessage(&msg);
  130.     }
  131.     UnregisterClass(L"ENTERPRISE", hInstance);
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement