HPEN pen;

HBRUSH brush;

HBRUSH bulletBrush;


POINT playerPos = { 400, 400 };

POINT playerSize = { 50, 50 };


POINT bulletPos = {};

POINT bulletSize = { 10, 10 };

bool shot = false;


POINT playerDirection = {};

POINT bulletDirection = {};


int playerSpeed = 10;

int bulletSpeed = 30;


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    switch (message)

    {

    case WM_CREATE:

    {

        pen = CreatePen(PS_SOLID, 10, RGB(255, 0, 255));

        brush = CreateSolidBrush(RGB(255, 100, 100));

        bulletBrush = CreateSolidBrush(RGB(0, 0, 0));


        SetTimer(hWnd, 0, 100, nullptr);

    }

    break;

    case WM_TIMER:

    {

        bulletPos.x += bulletDirection.x * bulletSpeed;

        bulletPos.y += bulletDirection.y * bulletSpeed;

        bulletPos.x += bulletDirection.x * bulletSpeed;

        bulletPos.y += bulletDirection.y * bulletSpeed;


        InvalidateRect(hWnd, nullptr, true);

    }

    break;

    case WM_COMMAND:

    {

        int wmId = LOWORD(wParam);

        switch (wmId)

        {

        case IDM_ABOUT:

            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

            break;

        case IDM_EXIT:

            DestroyWindow(hWnd);

            break;

        default:

            return DefWindowProc(hWnd, message, wParam, lParam);

        }

    }

    break;

    case WM_PAINT:

    {

        PAINTSTRUCT ps;

        HDC hdc = BeginPaint(hWnd, &ps);



        SelectObject(hdc, pen);

        SelectObject(hdc, brush);


        int left = playerPos.x - (playerSize.x >> 1);

        int right = playerPos.x + (playerSize.x >> 1);

        int top = playerPos.y - (playerSize.y >> 1);

        int bottom = playerPos.y + (playerSize.y >> 1);

        Rectangle(hdc, left, top, right, bottom);


        if (shot == true)

        {

            SelectObject(hdc, bulletBrush);

            

            int bulletLeft = bulletPos.x - (bulletSize.x >> 1);

            int bulletRight = bulletPos.x + (bulletSize.x >> 1);

            int bulletTop = bulletPos.y - (bulletSize.y >> 1);

            int bulletBottom = bulletPos.y + (bulletSize.y >> 1);


            Ellipse(hdc, bulletLeft, bulletTop, bulletRight, bulletBottom);

        }

        

        EndPaint(hWnd, &ps);

    }

    break;


    case WM_KEYDOWN:

    {

        switch (wParam)

        {

        case VK_UP:

            playerDirection = { 0, -1 };

            playerPos.y -= playerSpeed;

            break;

        case VK_DOWN:

            playerDirection = { 0, +1 };

            playerPos.y += playerSpeed;

            break;

        case VK_RIGHT:

            playerDirection = { +1, 0 };

            playerPos.x += playerSpeed;

            break;

        case VK_LEFT:

            playerDirection = { -1, 0 };

            playerPos.x -= playerSpeed;

            break;

        case ' ':

        {

            bulletPos = { playerPos.x, playerPos.y };

            if (playerDirection.x == 0 && playerDirection.y == 0)

            {

                bulletDirection = { 1, 0 };

            }

            else

            {

                bulletDirection = { playerDirection.x, playerDirection.y };

            }

            shot = true;

        }

        default:

            break;

        }

        InvalidateRect(hWnd, nullptr, true);

    }

    break;

    case WM_DESTROY:

        DeleteObject(pen);

        DeleteObject(brush);


        PostQuitMessage(0);

        break;

    default:

        return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

}