Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <list>
- #include <math.h>
- #define LEWO -1
- #define PRAWO 1
- using namespace std;
- HBRUSH Niebieski, Czerwony;
- class Statek {
- int PozX, PozY, Zwr;
- public:
- Statek(int PozycjaX, int PozycjaY, int Zwrot) : PozX(PozycjaX), PozY(PozycjaY), Zwr(Zwrot) {}
- POINT Pozycja() const;
- void Rysuj(const HDC DC) const;
- void Przesun(const int dx, const int dy) { PozX += dx*Zwr; PozY += dy; }
- bool Kolizja(const Statek&) const;
- };
- POINT Statek::Pozycja() const {
- POINT Pozycja;
- Pozycja.x = PozX; Pozycja.y = PozY;
- return Pozycja;
- }
- void Statek::Rysuj(const HDC DC) const {
- Rectangle(DC, PozX-(31*Zwr), PozY-10, PozX+(9*Zwr), PozY+10);
- Ellipse(DC, PozX-(11*Zwr), PozY-25, PozX+(39*Zwr), PozY+25);
- POINT P[4];
- 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;
- Polygon(DC, P, 4);
- P[0].y = PozY+5; P[1].y = PozY+5; P[2].y = PozY+25; P[3].y = PozY+25;
- Polygon(DC, P, 4);
- Rectangle(DC, PozX-(36*Zwr), PozY-25, PozX-(16*Zwr), PozY-20);
- Rectangle(DC, PozX-(36*Zwr), PozY+25, PozX-(16*Zwr), PozY+20);
- int prevdc = SaveDC(DC);
- SelectObject(DC, Niebieski);
- Rectangle(DC, PozX-(15*Zwr), PozY-25, PozX-(12*Zwr), PozY-20);
- Rectangle(DC, PozX-(15*Zwr), PozY+25, PozX-(12*Zwr), PozY+20);
- SelectObject(DC, Czerwony);
- Rectangle(DC, PozX-(39*Zwr), PozY-25, PozX-(35*Zwr), PozY-20);
- Rectangle(DC, PozX-(39*Zwr), PozY+25, PozX-(35*Zwr), PozY+20);
- RestoreDC(DC, prevdc);
- }
- bool Statek::Kolizja(const Statek& S) const {
- if (abs(PozX-S.Pozycja().x) <= 78 && abs(PozY-S.Pozycja().y) <= 50) return true;
- return false;
- }
- HDC dc;
- PAINTSTRUCT ps;
- RECT r;
- list<Statek> lista;
- list<Statek>::iterator it, it2;
- bool k;
- LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch(msg) {
- case WM_PAINT:
- dc = BeginPaint(hWnd, &ps);
- GetClientRect(hWnd, &r);
- SetMapMode(dc, MM_ISOTROPIC);
- SetWindowExtEx(dc, 1000, 1000, NULL);
- SetViewportExtEx(dc, r.right, r.bottom, NULL);
- for (it = lista.begin(); it != lista.end(); ++it) {
- it->Rysuj(dc);
- }
- EndPaint(hWnd, &ps);
- break;
- case WM_LBUTTONDOWN:
- for (it = lista.begin(); it != lista.end(); ++it) {
- k = false;
- for (it2 = lista.begin(); it2 != lista.end(); ++it2) {
- if (it2 != it) {
- if (it->Kolizja(*it2)) k = true;
- }
- }
- if (!k) it->Przesun(25, 0);
- }
- GetClientRect(hWnd, &r);
- InvalidateRect(hWnd, &r, true);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, msg, wParam, lParam);
- break;
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
- HWND hWnd;
- MSG msg;
- WNDCLASSEX wc;
- wc.cbClsExtra = 0;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.cbWndExtra = 0;
- wc.hbrBackground = (HBRUSH)(8);
- wc.hCursor = 0;
- wc.hIcon = 0;
- wc.hIconSm = 0;
- wc.hInstance = hInstance;
- wc.lpfnWndProc = WndProc;
- wc.lpszClassName = L"ENTERPRISE";
- wc.lpszMenuName = 0;
- wc.style = CS_VREDRAW | CS_HREDRAW;
- if(!RegisterClassEx(&wc)) return 0;
- lista.push_back(Statek(180, 55, PRAWO));
- lista.push_back(Statek(100, 200, PRAWO));
- lista.push_back(Statek(110, 260, PRAWO));
- lista.push_back(Statek(50, 470, PRAWO));
- lista.push_back(Statek(250, 780, PRAWO));
- lista.push_back(Statek(900, 220, LEWO));
- lista.push_back(Statek(950, 370, LEWO));
- lista.push_back(Statek(870, 450, LEWO));
- lista.push_back(Statek(790, 540, LEWO));
- lista.push_back(Statek(920, 800, LEWO));
- hWnd = CreateWindowEx(0, L"ENTERPRISE", L"Enterprajz", WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, 0, 0, hInstance, 0);
- Niebieski = CreateSolidBrush(RGB(50,50,255));
- Czerwony = CreateSolidBrush(RGB(255,50,50));
- ShowWindow(hWnd, SW_SHOW);
- UpdateWindow(hWnd);
- while (GetMessage(&msg, NULL, 0, 0) != 0) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(L"ENTERPRISE", hInstance);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement