Advertisement
peterzig

[PIU] Statki

Dec 5th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.10 KB | None | 0 0
  1. #include <windows.h>
  2. #include <deque>
  3. #include <time.h>
  4.  
  5. #define SCREEN_WIDTH 1000
  6. #define SCREEN_HEIGHT 800
  7. #define TIMER_ID1 1
  8. #define TIMER_ID2 2
  9. #define BULLET_SIZE 6
  10. #define EXPOLE_SQUARE_SIZE 20
  11.  
  12. TCHAR className[] = TEXT("Class");
  13. TCHAR appName[] = TEXT("[PIU] Statki");
  14.  
  15. int playerX = 100;
  16. int playerY = 100;
  17. UINT playerSizeX = 63;
  18. UINT playerSizeY = 107;
  19.  
  20. HWND hwnd;
  21. MSG msg;
  22.  
  23. struct Container {
  24.     float x, y;
  25.     float xVel, yVel;
  26. };
  27.  
  28. std::deque<Container> bullets;
  29. std::deque<Container> explode;
  30.  
  31. Container enemy;
  32.  
  33. HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
  34. {
  35.     HDC hdcMem, hdcMem2;
  36.     HBITMAP hbmMask, hbmOld, hbmOld2;
  37.     BITMAP bm;
  38.  
  39.     GetObject(hbmColour, sizeof(BITMAP), &bm);
  40.     hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
  41.  
  42.     hdcMem = CreateCompatibleDC(NULL);
  43.     hdcMem2 = CreateCompatibleDC(NULL);
  44.  
  45.     hbmOld = (HBITMAP)SelectObject(hdcMem, hbmColour);
  46.     hbmOld2 = (HBITMAP)SelectObject(hdcMem2, hbmMask);
  47.  
  48.     SetBkColor(hdcMem, crTransparent);
  49.  
  50.     BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  51.     BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
  52.  
  53.     SelectObject(hdcMem, hbmOld);
  54.     SelectObject(hdcMem2, hbmOld2);
  55.     DeleteDC(hdcMem);
  56.     DeleteDC(hdcMem2);
  57.  
  58.     return hbmMask;
  59. }
  60.  
  61. void addBullet() {
  62.     Container container;
  63.     container.x = playerX + (playerSizeX / 2) + (BULLET_SIZE / 2);
  64.     container.y = playerY;
  65.     container.xVel = 0/*2 - static_cast <float> (rand()) / static_cast <float> (RAND_MAX / 4)*/;
  66.     container.yVel = 5;
  67.  
  68.     bullets.push_back(container);
  69. }
  70.  
  71. void initExplode() {
  72.     size_t random = (rand() % 4) + 2;
  73.  
  74.     for (size_t i = 0; i < random; i++) {
  75.         Container container;
  76.         container.x = enemy.x + (playerSizeX / 2);
  77.         container.y = enemy.y - (playerSizeY / 2);
  78.         container.xVel = 5 - static_cast <float> (rand()) / static_cast <float> (RAND_MAX / 10);
  79.         container.yVel = 5 - static_cast <float> (rand()) / static_cast <float> (RAND_MAX / 10);
  80.  
  81.         explode.push_back(container);
  82.     }
  83. }
  84.  
  85. int randomEnemyVelocity() {
  86.     return (rand() % 20) - 10;
  87. }
  88.  
  89. bool checkCollision(Container A, Container B)
  90. {
  91.     RECT objectA, objectB;
  92.  
  93.     objectA.left = A.x;
  94.     objectA.right = A.x + BULLET_SIZE;
  95.     objectA.top = A.y;
  96.     objectA.bottom = A.y + BULLET_SIZE;
  97.  
  98.     objectB.left = B.x;
  99.     objectB.right = B.x + playerSizeX;
  100.     objectB.top = B.y;
  101.     objectB.bottom = B.y + playerSizeY;
  102.  
  103.     if (objectA.bottom <= objectB.top)
  104.     {
  105.         return false;
  106.     }
  107.  
  108.     if (objectA.top >= objectB.bottom)
  109.     {
  110.         return false;
  111.     }
  112.  
  113.     if (objectA.right <= objectB.left)
  114.     {
  115.         return false;
  116.     }
  117.  
  118.     if (objectA.left >= objectB.right)
  119.     {
  120.         return false;
  121.     }
  122.  
  123.     return true;
  124. }
  125.  
  126. void addEnemy(RECT clientRect) {
  127.     Container container;
  128.     container.x = rand() % (clientRect.right - playerSizeX);
  129.     container.y = 100;
  130.     container.xVel = randomEnemyVelocity();
  131.     container.yVel = 0;
  132.  
  133.     enemy = container;
  134. }
  135.  
  136. void enemyUpdate(RECT clientRect) {
  137.     enemy.x += enemy.xVel;
  138.  
  139.     if (enemy.x + enemy.xVel < 0) {
  140.         enemy.x = enemy.xVel;
  141.         enemy.xVel = -enemy.xVel;
  142.     }
  143.  
  144.     else if (enemy.x + enemy.xVel > clientRect.right - playerSizeX) {
  145.         enemy.x = clientRect.right - playerSizeX - enemy.xVel;
  146.         enemy.xVel = -enemy.xVel;
  147.     }
  148. }
  149.  
  150. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  151.     static RECT clientRect;
  152.     static HBITMAP backgroundBitMap, playerBitmap, playerMask, enemyBitmap, enemyMask;
  153.     static PAINTSTRUCT ps;
  154.     static BITMAP bgInfo, playerInfo, enemyInfo;
  155.     static HBRUSH whiteColor;
  156.  
  157.     switch (msg) {
  158.     case WM_CREATE: {
  159.         srand(time(NULL));
  160.         GetClientRect(hwnd, &clientRect);
  161.         SetTimer(hwnd, TIMER_ID1, 20, NULL);
  162.         SetTimer(hwnd, TIMER_ID2, 3000, NULL);
  163.         addEnemy(clientRect);
  164.  
  165.         whiteColor = (HBRUSH)CreateSolidBrush(RGB(255, 255, 255));
  166.  
  167.         backgroundBitMap = (HBITMAP)LoadImage(NULL, TEXT("C:\\Users\\zapop\\OneDrive\\Dokumenty\\Visual Studio 2015\\Projects\\[PIU] Statki\\[PIU] Statki\\background.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  168.         playerBitmap = (HBITMAP)LoadImage(NULL, TEXT("C:\\Users\\zapop\\OneDrive\\Dokumenty\\Visual Studio 2015\\Projects\\[PIU] Statki\\[PIU] Statki\\player.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  169.         playerMask = CreateBitmapMask(playerBitmap, RGB(0, 0, 0));
  170.         enemyBitmap = (HBITMAP)LoadImage(NULL, TEXT("C:\\Users\\zapop\\OneDrive\\Dokumenty\\Visual Studio 2015\\Projects\\[PIU] Statki\\[PIU] Statki\\enemy.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  171.         enemyMask = CreateBitmapMask(enemyBitmap, RGB(0, 0, 0));
  172.  
  173.         GetObject(backgroundBitMap, sizeof(bgInfo), &bgInfo);
  174.         GetObject(enemyBitmap, sizeof(enemyInfo), &enemyInfo);
  175.         GetObject(playerBitmap, sizeof(playerInfo), &playerInfo);
  176.  
  177.         playerSizeX = playerInfo.bmWidth;
  178.         playerSizeY = playerInfo.bmHeight;
  179.         playerX = (clientRect.right / 2) - (playerSizeX / 2);
  180.         playerY = clientRect.bottom - (clientRect.bottom / 4);
  181.     }break;
  182.  
  183.     case WM_PAINT: {
  184.         HDC hdc = BeginPaint(hwnd, &ps);
  185.  
  186.         /////////////////////////////////////////////////////////////////////
  187.  
  188.         HDC hdcNowy = CreateCompatibleDC(hdc);
  189.         HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, backgroundBitMap);
  190.         BitBlt(hdc, 0, 0, bgInfo.bmWidth, bgInfo.bmHeight, hdcNowy, 0, 0, SRCCOPY);
  191.  
  192.         SelectObject(hdcNowy, hbmOld);
  193.  
  194.         /////////////////////////////////////////////////////////////////////
  195.  
  196.         SelectObject(hdc, whiteColor);
  197.  
  198.         for (std::deque<Container>::iterator it = bullets.begin(); it != bullets.end(); ++it)
  199.             Ellipse(hdc, it->x, it->y, it->x + BULLET_SIZE, it->y + BULLET_SIZE);
  200.  
  201.         /////////////////////////////////////////////////////////////////////
  202.  
  203.         for (std::deque<Container>::iterator it = explode.begin(); it != explode.end(); ++it)
  204.             Rectangle(hdc, it->x, it->y, it->x + EXPOLE_SQUARE_SIZE, it->y + EXPOLE_SQUARE_SIZE);
  205.  
  206.         /////////////////////////////////////////////////////////////////////
  207.  
  208.         hbmOld = (HBITMAP)SelectObject(hdcNowy, playerMask);
  209.  
  210.         BitBlt(hdc, playerX, playerY, playerSizeX, playerSizeY, hdcNowy, 0, 0, SRCAND);
  211.         SelectObject(hdcNowy, playerBitmap);
  212.         BitBlt(hdc, playerX, playerY, playerSizeX, playerSizeY, hdcNowy, 0, 0, SRCPAINT);
  213.  
  214.         SelectObject(hdcNowy, hbmOld);
  215.  
  216.         /////////////////////////////////////////////////////////////////////
  217.  
  218.         hbmOld = (HBITMAP)SelectObject(hdcNowy, enemyMask);
  219.  
  220.         BitBlt(hdc, enemy.x, enemy.y, playerSizeX, playerSizeY, hdcNowy, 0, 0, SRCAND);
  221.         SelectObject(hdcNowy, enemyBitmap);
  222.         BitBlt(hdc, enemy.x, enemy.y, playerSizeX, playerSizeY, hdcNowy, 0, 0, SRCPAINT);
  223.  
  224.         SelectObject(hdcNowy, hbmOld);
  225.  
  226.         DeleteDC(hdcNowy);
  227.         EndPaint(hwnd, &ps);
  228.     }break;
  229.  
  230.     case WM_TIMER: {
  231.         switch (wParam) {
  232.         case TIMER_ID1: {
  233.             for (std::deque<Container>::iterator it = bullets.begin(); it != bullets.end(); ++it) {
  234.                 it->y -= it->yVel;
  235.                 it->x += it->xVel;
  236.  
  237.                 if (it->y < 0) {
  238.                     bullets.erase(it);
  239.                     break;
  240.                 }
  241.             }
  242.  
  243.             enemyUpdate(clientRect);
  244.  
  245.             for (std::deque<Container>::iterator b = bullets.begin(); b != bullets.end(); ++b)
  246.                 if (checkCollision(*b, enemy)) {
  247.                     initExplode();
  248.                     addEnemy(clientRect);
  249.                 }
  250.  
  251.             for (std::deque<Container>::iterator it = explode.begin(); it != explode.end(); ++it) {
  252.                 it->x += it->xVel;
  253.                 it->y += it->yVel;
  254.  
  255.                 if (it->x < 0 || it->x > clientRect.right || it->y < 0 || it->y > clientRect.bottom) {
  256.                     explode.erase(it);
  257.                     break;
  258.                 }
  259.             }
  260.  
  261.             InvalidateRect(hwnd, NULL, 0);
  262.         }break;
  263.  
  264.         case TIMER_ID2:
  265.             enemy.xVel = randomEnemyVelocity();
  266.             break;
  267.         }
  268.     }break;
  269.  
  270.     case WM_KEYDOWN:
  271.         switch ((int)wParam)
  272.         {
  273.         case VK_RIGHT:
  274.             if (playerX + 5 < clientRect.right - playerSizeX)
  275.                 playerX += 5;
  276.             break;
  277.  
  278.         case VK_LEFT:
  279.             if (playerX - 5 > 0)
  280.                 playerX -= 5;
  281.             break;
  282.  
  283.         case VK_SPACE:
  284.             addBullet();
  285.             break;
  286.  
  287.         default:
  288.             break;
  289.         }
  290.         break;
  291.  
  292.     case WM_CLOSE:
  293.         DestroyWindow(hwnd);
  294.         break;
  295.  
  296.     case WM_DESTROY: {
  297.         KillTimer(hwnd, TIMER_ID1);
  298.         KillTimer(hwnd, TIMER_ID2);
  299.         PostQuitMessage(0);
  300.     }break;
  301.  
  302.     default:
  303.         return DefWindowProc(hwnd, msg, wParam, lParam);
  304.     }
  305.  
  306.     return 0;
  307. }
  308.  
  309. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  310. {
  311.     WNDCLASSEX wc;
  312.     wc.cbSize = sizeof(WNDCLASSEX);
  313.     wc.style = 0;
  314.     wc.cbClsExtra = 0;
  315.     wc.cbWndExtra = 0;
  316.     wc.hInstance = hInstance;
  317.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  318.     wc.lpfnWndProc = WndProc;
  319.     wc.lpszClassName = className;
  320.     wc.lpszMenuName = NULL;
  321.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  322.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  323.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  324.  
  325.     if (RegisterClassEx(&wc) == 0) {
  326.         MessageBox(NULL, TEXT("Some problem with RegisterClassEx"), className, MB_OK);
  327.         return 1;
  328.     }
  329.  
  330.     hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_CLIENTEDGE, className, appName, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, hInstance, 0);
  331.     if (hwnd == NULL) {
  332.         MessageBox(NULL, TEXT("Some problem with CreateWindowEx by hwnd"), className, MB_OK);
  333.         return 1;
  334.     }
  335.  
  336.     ShowWindow(hwnd, nCmdShow);
  337.     UpdateWindow(hwnd);
  338.  
  339.     while (GetMessage(&msg, NULL, 0, 0) > 0)
  340.     {
  341.         TranslateMessage(&msg);
  342.         DispatchMessage(&msg);
  343.     }
  344.  
  345.     UnregisterClass(className, hInstance);
  346.  
  347.     return msg.wParam;
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement