#include #include using namespace std; LPSTR className = TEXT("windowClass"); MSG msg; WORD timer; HDC hdc; HDC hdcMem; HBITMAP hbmMem; HANDLE hOld; //TEKSTURY HBITMAP bgTexture; HBITMAP playerTexture; HBITMAP enemyTexture; POINT bgPos; POINT playerPos; POINT enemyPos; static int enemyHP = 1; vector bulletsPos; vector enemyBullets; static bool enemyDirLeft = false; static bool registerClass(); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); void draw(HWND); void update(); POINT enemySize; long int before = GetTickCount(); long int after = GetTickCount(); int seconds; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { if (!registerClass()) { MessageBox(NULL, TEXT("Nie udało się zarejestrować klasy okna"), TEXT("Zjebałeś"), MB_OK | MB_ICONWARNING); return 1; } HWND hwnd = CreateWindowEx(0, className, TEXT("Okno"), WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); if (hwnd == NULL) { MessageBox(NULL, TEXT("Nie udało się utworzyć okna"), TEXT("Zjebałeś"), MB_OK | MB_ICONWARNING); return 1; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } void drawTexture(HDC hdc, HBITMAP texture, POINT position) { HDC hdcNew = CreateCompatibleDC(hdc); HBITMAP oldImage = (HBITMAP)SelectObject(hdcNew, texture); BITMAP textureInfo; GetObject(texture, sizeof(textureInfo), &textureInfo); BitBlt(hdc, position.x, position.y, 800, 600, hdcNew, 0, 0, SRCCOPY); SelectObject(hdcNew, oldImage); DeleteDC(hdcNew); DeleteObject(oldImage); } HBRUSH red = CreateSolidBrush(RGB(255, 0, 0)); void drawBullet(HDC hdc, POINT pos) { HBRUSH def = (HBRUSH) SelectObject(hdc, red); Rectangle(hdc, pos.x, pos.y, pos.x + 10, pos.y + 10); } bool checkColl(POINT bodyAPos, POINT bodyBPos, POINT bodyBSize) { if (bodyAPos.x > bodyBPos.x && bodyAPos.x < bodyBPos.x + bodyBSize.x && bodyAPos.y > bodyBPos.y && bodyAPos.y < bodyBPos.y + bodyBSize.y) { return true; } else { return false; } } void update() { if (enemyPos.x <= 0) { enemyDirLeft = false; } if (enemyPos.x >= 800 - 64) { enemyDirLeft = true; } if (enemyDirLeft) { enemyPos.x -= 15; } else { enemyPos.x += 15; } if (seconds >= 10) { seconds = 0; POINT bullet; bullet.x = enemyPos.x + 20; bullet.y = enemyPos.y + 20; enemyBullets.push_back(bullet); } else { seconds++; } for (int i = 0; i < bulletsPos.size(); i++) { bulletsPos[i].y -= 40; if (checkColl(bulletsPos[i], enemyPos, enemySize)) { enemyHP--; } if (bulletsPos[i].y <= 0) { bulletsPos.erase(bulletsPos.begin() + i); i--; } } for (int i = 0; i < enemyBullets.size(); i++) { enemyBullets[i].y += 40; } } void draw(HWND hwnd) { PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); hdcMem = CreateCompatibleDC(hdc); hbmMem = CreateCompatibleBitmap(hdc, 800, 600); hOld = SelectObject(hdcMem, hbmMem); drawTexture(hdc, bgTexture, bgPos); if (enemyHP > 0) { drawTexture(hdc, enemyTexture, enemyPos); } drawTexture(hdc, playerTexture, playerPos); for (int i = 0; i < bulletsPos.size(); i++) { drawBullet(hdc, bulletsPos[i]); } for (int i = 0; i < enemyBullets.size(); i++) { drawBullet(hdc, enemyBullets[i]); } SelectObject(hdcMem, hOld); DeleteObject(hbmMem); DeleteDC(hdcMem); EndPaint(hwnd, &ps); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static bool spacePressed = false; switch (msg) { case WM_CREATE: { SetTimer(hwnd, timer, 100, NULL); bgTexture = (HBITMAP)LoadImage(NULL, "space.bmp", IMAGE_BITMAP, 800, 600, LR_LOADFROMFILE); playerTexture = (HBITMAP)LoadImage(NULL, "shipPlayer.bmp", IMAGE_BITMAP, 64, 128, LR_LOADFROMFILE); enemyTexture = (HBITMAP)LoadImage(NULL, "shipEnemy.bmp", IMAGE_BITMAP, 64, 128, LR_LOADFROMFILE); bgPos.x = 0; bgPos.y = 0; playerPos.x = 800 / 2; playerPos.y = 600 - 128 - 40; enemyPos.x = 800 / 2; enemyPos.y = 0; enemySize.x = 64; enemySize.y = 128; }break; case WM_PAINT: { draw(hwnd); }break; case WM_KEYDOWN: { switch ((wParam)) { case VK_LEFT: { playerPos.x -= 15; }break; case VK_RIGHT: { playerPos.x += 15; }break; case VK_SPACE: { if (!spacePressed) { spacePressed = true; POINT bullet; bullet.x = playerPos.x + 20; bullet.y = playerPos.y - 20; bulletsPos.push_back(bullet); } }break; } }break; case WM_KEYUP: { switch (wParam) { case VK_SPACE: { spacePressed = false; }break; } }break; case WM_TIMER: { update(); InvalidateRect(hwnd, 0, 1); }break; case WM_CLOSE: { DestroyWindow(hwnd); }break; case WM_DESTROY: { PostQuitMessage(0); }break; default: { return DefWindowProc(hwnd, msg, wParam, lParam); } } return 0; } static bool registerClass() { WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpfnWndProc = WndProc; wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wc.hInstance = GetModuleHandle(NULL); wc.lpszMenuName = NULL; wc.lpszClassName = className; return (RegisterClassEx(&wc) != 0); }