Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(lib ,"Msimg32.lib");
- #include <Windows.h>
- #include <math.h>
- #include <time.h>
- #include <vector>
- #include <string>
- int SCORE = 0;
- PAINTSTRUCT PS;
- HDC hdc, hdcNowy;
- HBITMAP hbmOld;
- RECT WndRect;
- int Max = 0;
- HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent) {
- HDC hdcMem, hdcMem2;
- HBITMAP hbmMask, hbmOld, hbmOld2;
- BITMAP bm;
- GetObject(hbmColour, sizeof(BITMAP), &bm);
- hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
- hdcMem = CreateCompatibleDC(NULL);
- hdcMem2 = CreateCompatibleDC(NULL);
- hbmOld = (HBITMAP)SelectObject(hdcMem, hbmColour);
- hbmOld2 = (HBITMAP)SelectObject(hdcMem2, hbmMask);
- SetBkColor(hdcMem, crTransparent);
- BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
- BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
- SelectObject(hdcMem, hbmOld);
- SelectObject(hdcMem2, hbmOld2);
- DeleteDC(hdcMem);
- DeleteDC(hdcMem2);
- return hbmMask;
- }
- class Egg {
- public:
- POINT Direction;
- POINT Position;
- HBITMAP Bitmap;
- BITMAP BitmapInfo;
- HBITMAP Maska;
- int Speed = 10;
- int Y_MAX;
- bool STOP = false;
- Egg(int X, int Y, int Y_max) {
- Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\eggs.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- GetObject(Bitmap, sizeof(BitmapInfo), &BitmapInfo);
- Position.x = X;
- Position.y = Y;
- Direction.x = 0;
- Direction.y = 1;
- Y_MAX = Y_max;
- Maska = CreateBitmapMask(Bitmap, RGB(0, 0, 0));
- }
- bool MoveBall() {
- if (!STOP) {
- Position.x += (Direction.x*Speed);
- Position.y += (Direction.y*Speed);
- }
- if (Position.y >= Y_MAX) {
- STOP = true;
- return true;
- }
- return false;
- }
- void PaintMasked(HDC &hdc, HDC &bmhdc) {
- Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\eggs.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Maska);
- BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCAND);
- SelectObject(hdcNowy, Bitmap);
- BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCPAINT);
- SelectObject(hdcNowy, hbmOld);
- }
- };
- class Basket {
- POINT Position;
- HBITMAP Bitmap;
- BITMAP BitmapInfo;
- HBITMAP Maska;
- public:
- Basket() {
- Position.x = 300;
- Position.y = 400;
- Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\ba.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- GetObject(Bitmap, sizeof(BitmapInfo), &BitmapInfo);
- Maska = CreateBitmapMask(Bitmap, RGB(0, 0, 0));
- }
- void SetPos(int x) {
- Position.x = x;
- }
- void PaintMasked(HDC &hdc, HDC &bmhdc) {
- Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\ba.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Maska);
- BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCAND);
- SelectObject(hdcNowy, Bitmap);
- BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCPAINT);
- SelectObject(hdcNowy, hbmOld);
- }
- void Catch(std::vector<Egg> &egg) {
- for (int i = 0; i < egg.size(); i++) {
- if (Position.x<=egg[i].Position.x &&
- Position.x+ BitmapInfo.bmWidth >= egg[i].Position.x+egg[i].BitmapInfo.bmWidth) {
- if (egg[i].Position.y > Position.y) {
- egg.erase(egg.begin() + i);
- SCORE++;
- }
- }
- }
- }
- };
- std::vector<Egg> eggs;
- Basket *basket = new Basket();
- float MousePos_x;
- float MousePos_y;
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- //MousePos_x = LOWORD(lParam);
- //MousePos_y = HIWORD(lParam);
- switch (msg) {
- case WM_RBUTTONDOWN: {
- eggs.clear();
- }break;
- case WM_PAINT: {
- hdc = BeginPaint(hwnd, &PS);
- hdcNowy = CreateCompatibleDC(hdc);
- char buff[255];
- wsprintf(buff, "Wynik: %d", SCORE);
- TextOut(hdc, 5, 5, buff, std::string(buff).length());
- for (int i = 0; i < eggs.size(); i++) {
- eggs[i].PaintMasked(hdc, hdcNowy);
- }
- basket->PaintMasked(hdc, hdcNowy);
- DeleteDC(hdcNowy);
- EndPaint(hwnd, &PS);
- }break;
- case WM_TIMER: {
- GetClientRect(hwnd, &WndRect);
- if (rand() % 100 > 95)
- eggs.push_back(Egg((int)(rand() % WndRect.right - 100) + 50, 50, WndRect.bottom - 50));
- POINT p;
- GetCursorPos(&p);
- ScreenToClient(hwnd, &p);
- basket->SetPos(p.x -100);
- basket->Catch(eggs);
- for (int i = 0; i < eggs.size(); i++) {
- if (eggs[i].MoveBall()) {
- //SCORE--;
- eggs.erase(eggs.begin() + i);
- }
- }
- InvalidateRect(hwnd, &WndRect, true);
- }break;
- case WM_CLOSE: DestroyWindow(hwnd); break;
- case WM_DESTROY: PostQuitMessage(0); break;
- default: return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
- srand(time(NULL));
- WNDCLASSEX window;
- MSG msg;
- TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Pilki");
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 10);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- window.hInstance = hInstance;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = Class_Name;
- window.lpszMenuName = 0;
- window.style = CS_VREDRAW | CS_HREDRAW;
- RegisterClassEx(&window);
- HWND hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- SetTimer(hwnd, 1000, 20, NULL);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(Class_Name, hInstance);
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement