Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```cpp
- #include <windows.h>
- #include <vector>
- #include <cmath>
- #include <ctime>
- #define ID_TIMER 1
- #define BALL_RADIUS 10
- #define TABLE_WIDTH 800
- #define TABLE_HEIGHT 400
- #define BALL_COUNT 16
- HINSTANCE hInst;
- HWND hwnd;
- HDC hdcMem, hdcBall;
- HBITMAP hbmMem, hbmOld;
- HPEN hPen;
- HBRUSH hBrush;
- RECT table = { 50, 50, 850, 450 };
- POINT cueStart, cueEnd;
- bool isDragging = false;
- float power = 0.0f;
- int playerColors[2] = { 0, 0 };
- int currentPlayer = 0;
- struct Ball {
- float x, y;
- float vx, vy;
- COLORREF color;
- bool isPocketed;
- bool isStriped;
- };
- std::vector<Ball> balls;
- Ball cueBall;
- bool playerTurn = true;
- bool gameOver = false;
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void InitBalls();
- void DrawTable(HDC);
- void DrawBalls(HDC);
- void DrawCue(HDC);
- void DrawPowerMeter(HDC);
- void UpdateBalls();
- void CheckCollisions();
- void CheckPockets();
- void AIMove();
- void ApplyEnglish(Ball&, float, float);
- bool CheckGameOver();
- void AssignPlayerColor(COLORREF);
- void HandleFoul();
- int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow) {
- hInst = hInstance;
- WNDCLASS wc = {};
- wc.lpfnWndProc = WndProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = L"8BallPool";
- RegisterClass(&wc);
- hwnd = CreateWindowEx(0, L"8BallPool", L"8-Ball Pool", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 900, 600, nullptr, nullptr, hInstance, nullptr);
- ShowWindow(hwnd, nCmdShow);
- MSG msg = {};
- while (GetMessage(&msg, nullptr, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
- switch (message) {
- case WM_CREATE:
- srand((unsigned int)time(NULL));
- InitBalls();
- SetTimer(hwnd, ID_TIMER, 16, NULL);
- break;
- case WM_PAINT: {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hwnd, &ps);
- if (!hdcMem) {
- hdcMem = CreateCompatibleDC(hdc);
- hbmMem = CreateCompatibleBitmap(hdc, 900, 600);
- hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
- }
- FillRect(hdcMem, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
- DrawTable(hdcMem);
- DrawBalls(hdcMem);
- if (!gameOver && playerTurn) {
- DrawCue(hdcMem);
- DrawPowerMeter(hdcMem);
- }
- BitBlt(hdc, 0, 0, 900, 600, hdcMem, 0, 0, SRCCOPY);
- EndPaint(hwnd, &ps);
- break;
- }
- case WM_LBUTTONDOWN:
- if (gameOver || !playerTurn) break;
- cueStart.x = LOWORD(lParam);
- cueStart.y = HIWORD(lParam);
- isDragging = true;
- break;
- case WM_MOUSEMOVE:
- if (isDragging) {
- cueEnd.x = LOWORD(lParam);
- cueEnd.y = HIWORD(lParam);
- power = min(sqrt(pow(cueEnd.x - cueStart.x, 2) + pow(cueEnd.y - cueStart.y, 2)) / 100.0f, 1.0f);
- InvalidateRect(hwnd, NULL, FALSE);
- }
- break;
- case WM_LBUTTONUP:
- if (isDragging) {
- isDragging = false;
- float dx = cueEnd.x - cueStart.x;
- float dy = cueEnd.y - cueStart.y;
- cueBall.vx = dx * power * 0.5f;
- cueBall.vy = dy * power * 0.5f;
- ApplyEnglish(cueBall, dx * power * 0.1f, dy * power * 0.1f);
- playerTurn = false;
- InvalidateRect(hwnd, NULL, FALSE);
- }
- break;
- case WM_TIMER:
- if (!gameOver) {
- UpdateBalls();
- CheckCollisions();
- CheckPockets();
- if (!playerTurn) AIMove();
- if (CheckGameOver()) {
- gameOver = true;
- MessageBox(hwnd, L"Game Over! All balls are pocketed.", L"Game Over", MB_OK);
- }
- InvalidateRect(hwnd, NULL, FALSE);
- }
- break;
- case WM_DESTROY:
- KillTimer(hwnd, ID_TIMER);
- SelectObject(hdcMem, hbmOld);
- DeleteObject(hbmMem);
- DeleteDC(hdcMem);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
- return 0;
- }
- void InitBalls() {
- balls.clear();
- cueBall = { 200.0f, 250.0f, 0.0f, 0.0f, RGB(255, 255, 255), false, false };
- balls.push_back(cueBall);
- std::vector<COLORREF> colors = {
- RGB(255, 255, 0), RGB(255, 0, 0), RGB(255, 255, 0), RGB(255, 0, 0),
- RGB(255, 255, 0), RGB(255, 0, 0), RGB(255, 255, 0), RGB(255, 0, 0),
- RGB(255, 255, 0), RGB(255, 0, 0), RGB(255, 255, 0), RGB(255, 0, 0),
- RGB(255, 255, 0), RGB(255, 0, 0), RGB(0, 0, 0)
- };
- float startX = 600.0f;
- float startY = 250.0f;
- int colorIndex = 0;
- for (int row = 0; row < 5; ++row) {
- for (int col = 0; col <= row; ++col) {
- float x = startX + row * 2 * BALL_RADIUS * cosf(M_PI / 3);
- float y = startY + (col - row / 2.0f) * 2 * BALL_RADIUS * sinf(M_PI / 3);
- bool isStriped = colorIndex < 7 || (colorIndex >= 7 && colorIndex < 14 && colors[colorIndex] == RGB(255, 0, 0));
- balls.push_back({ x, y, 0.0f, 0.0f, colors[colorIndex++], false, isStriped });
- }
- }
- }
- void DrawTable(HDC hdc) {
- HBRUSH hBrush = CreateSolidBrush(RGB(0, 128, 0));
- FillRect(hdc, &table, hBrush);
- DeleteObject(hBrush);
- for (int i = 0; i < 2; ++i) {
- for (int j = 0; j < 3; ++j) {
- Ellipse(hdc, table.left + j * TABLE_WIDTH / 2 - BALL_RADIUS, table.top + i * TABLE_HEIGHT - BALL_RADIUS, table.left + j * TABLE_WIDTH / 2 + BALL_RADIUS, table.top + i * TABLE_HEIGHT + BALL_RADIUS);
- }
- }
- }
- void DrawBalls(HDC hdc) {
- for (const auto& ball : balls) {
- if (!ball.isPocketed) {
- hBrush = CreateSolidBrush(ball.color);
- SelectObject(hdc, hBrush);
- Ellipse(hdc, (int)(ball.x - BALL_RADIUS), (int)(ball.y - BALL_RADIUS), (int)(ball.x + BALL_RADIUS), (int)(ball.y + BALL_RADIUS));
- DeleteObject(hBrush);
- }
- }
- }
- void DrawCue(HDC hdc) {
- MoveToEx(hdc, (int)cueBall.x, (int)cueBall.y, NULL);
- LineTo(hdc, cueEnd.x, cueEnd.y);
- }
- void DrawPowerMeter(HDC hdc) {
- HBRUSH hBrush = CreateSolidBrush(RGB(0, 255, 0));
- RECT rect = { 10, 50, 30, 450 };
- FillRect(hdc, &rect, hBrush);
- DeleteObject(hBrush);
- hBrush = CreateSolidBrush(RGB(255, 255, 0));
- rect.bottom = 450 - (int)(400 * power);
- FillRect(hdc, &rect, hBrush);
- DeleteObject(hBrush);
- hBrush = CreateSolidBrush(RGB(255, 0, 0));
- rect.bottom = 450 - (int)(400 * power / 2);
- FillRect(hdc, &rect, hBrush);
- DeleteObject(hBrush);
- }
- void UpdateBalls() {
- for (auto& ball : balls) {
- if (!ball.isPocketed) {
- ball.x += ball.vx;
- ball.y += ball.vy;
- ball.vx *= 0.99f;
- ball.vy *= 0.99f;
- if (fabs(ball.vx) < 0.01f) ball.vx = 0.0f;
- if (fabs(ball.vy) < 0.01f) ball.vy = 0.0f;
- }
- }
- }
- void CheckCollisions() {
- for (size_t i = 0; i < balls.size(); ++i) {
- Ball& ball1 = balls[i];
- if (ball1.isPocketed) continue;
- for (size_t j = i + 1; j < balls.size(); ++j) {
- Ball& ball2 = balls[j];
- if (ball2.isPocketed) continue;
- float dx = ball2.x - ball1.x;
- float dy = ball2.y - ball1.y;
- float distance = sqrtf(dx * dx + dy * dy);
- if (distance < 2 * BALL_RADIUS) {
- float angle = atan2f(dy, dx);
- float totalVx = ball1.vx + ball2.vx;
- float totalVy = ball1.vy + ball2.vy;
- ball1.vx = totalVx * cosf(angle);
- ball1.vy = totalVy * sinf(angle);
- ball2.vx = totalVx * cosf(angle + M_PI);
- ball2.vy = totalVy * sinf(angle + M_PI);
- ball1.x += ball1.vx;
- ball1.y += ball1.vy;
- ball2.x += ball2.vx;
- ball2.y += ball2.vy;
- }
- }
- }
- }
- void CheckPockets() {
- for (auto& ball : balls) {
- if (ball.isPocketed) continue;
- for (int i = 0; i < 2; ++i) {
- for (int j = 0; j < 3; ++j) {
- float dx = table.left + j * TABLE_WIDTH / 2 - ball.x;
- float dy = table.top + i * TABLE_HEIGHT - ball.y;
- if (sqrtf(dx * dx + dy * dy) < BALL_RADIUS) {
- ball.isPocketed = true;
- if (&ball == &cueBall) {
- HandleFoul();
- return;
- } else {
- playerTurn = true;
- return;
- }
- }
- }
- }
- }
- }
- void AIMove() {
- if (!playerTurn) {
- cueBall.vx = (rand() % 200 - 100) / 100.0f;
- cueBall.vy = (rand() % 200 - 100) / 100.0f;
- ApplyEnglish(cueBall, cueBall.vx * 0.1f, cueBall.vy * 0.1f);
- playerTurn = true;
- }
- }
- void ApplyEnglish(Ball& ball, float ex, float ey) {
- ball.vx += ex;
- ball.vy += ey;
- }
- bool CheckGameOver() {
- for (const auto& ball : balls) {
- if (!ball.isPocketed) return false;
- }
- return true;
- }
- void AssignPlayerColor(COLORREF color) {
- if (playerColors[0] == 0) playerColors[0] = color;
- else if (playerColors[1] == 0 && playerColors[0] != color) playerColors[1] = color;
- }
- void HandleFoul() {
- cueBall.isPocketed = false;
- cueBall.x = 200.0f;
- cueBall.y = 250.0f;
- cueBall.vx = 0.0f;
- cueBall.vy = 0.0f;
- playerTurn = true;
- }
- ```
- ++=====================++
- ++=====================++
- ```cpp
- RECT foulRect = { 50, 50, 250, 150 };
- InvalidateRect(hwnd, &foulRect, TRUE);
- }
- void DrawFoul(HDC hdc) {
- RECT rect = { 50, 50, 250, 150 };
- SetBkMode(hdc, TRANSPARENT);
- SetTextColor(hdc, RGB(255, 0, 0));
- DrawText(hdc, L"Foul!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
- }
- void DrawCurrentPlayerIndicator(HDC hdc) {
- RECT rect = { 400, 10, 500, 40 };
- wchar_t text[50];
- wsprintf(text, L"Player %d's Turn", currentPlayer + 1);
- SetBkMode(hdc, TRANSPARENT);
- SetTextColor(hdc, RGB(0, 0, 255));
- DrawText(hdc, text, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
- }
- void DrawPocketedBalls(HDC hdc) {
- RECT rect = { 50, 500, 850, 550 };
- FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
- int x = 60;
- for (const auto& ball : balls) {
- if (ball.isPocketed) {
- hBrush = CreateSolidBrush(ball.color);
- SelectObject(hdc, hBrush);
- Ellipse(hdc, x, 510, x + BALL_RADIUS * 2, 510 + BALL_RADIUS * 2);
- DeleteObject(hBrush);
- x += BALL_RADIUS * 3;
- }
- }
- }
- void DrawTargetingLine(HDC hdc) {
- if (isDragging) {
- MoveToEx(hdc, (int)cueBall.x, (int)cueBall.y, NULL);
- LineTo(hdc, cueEnd.x, cueEnd.y);
- float dx = cueEnd.x - cueBall.x;
- float dy = cueEnd.y - cueBall.y;
- float len = sqrt(dx * dx + dy * dy);
- dx /= len;
- dy /= len;
- int tx = (int)(cueBall.x + dx * (len + BALL_RADIUS));
- int ty = (int)(cueBall.y + dy * (len + BALL_RADIUS));
- Ellipse(hdc, tx - 2, ty - 2, tx + 2, ty + 2);
- }
- }
- void DrawCueBallEnglish(HDC hdc) {
- RECT rect = { 50, 450, 100, 500 };
- FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
- Ellipse(hdc, 50, 450, 100, 500);
- Ellipse(hdc, 72, 472, 78, 478);
- }
- void DrawCueBallLimitLine(HDC hdc) {
- HPEN hPenOld = (HPEN)SelectObject(hdc, CreatePen(PS_DOT, 1, RGB(255, 0, 0)));
- MoveToEx(hdc, table.left + 0.3 * TABLE_WIDTH, table.top, NULL);
- LineTo(hdc, table.left + 0.3 * TABLE_WIDTH, table.bottom);
- SelectObject(hdc, hPenOld);
- DeleteObject(hPen);
- }
- void DrawUIElements(HDC hdc) {
- DrawCurrentPlayerIndicator(hdc);
- DrawPocketedBalls(hdc);
- DrawCueBallLimitLine(hdc);
- DrawCueBallEnglish(hdc);
- if (!playerTurn) {
- RECT rect = { 400, 500, 500, 550 };
- wchar_t text[50];
- wsprintf(text, L"Player %d's Turn", currentPlayer + 1);
- SetBkMode(hdc, TRANSPARENT);
- SetTextColor(hdc, RGB(0, 0, 255));
- DrawText(hdc, text, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
- }
- }
- void UpdateGame() {
- if (!gameOver) {
- UpdateBalls();
- CheckCollisions();
- CheckPockets();
- if (!playerTurn) AIMove();
- if (CheckGameOver()) {
- gameOver = true;
- InvalidateRect(hwnd, NULL, TRUE);
- }
- }
- }
- ```
- ++=====================++
- ++=====================++
- ```cpp
- void Paint(HWND hwnd) {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hwnd, &ps);
- HDC hdcMem = CreateCompatibleDC(hdc);
- HBITMAP hbmMem = CreateCompatibleBitmap(hdc, TABLE_WIDTH, TABLE_HEIGHT);
- HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
- HBRUSH hbrBkGnd = CreateSolidBrush(RGB(0, 128, 0));
- FillRect(hdcMem, &table, hbrBkGnd);
- DeleteObject(hbrBkGnd);
- DrawTable(hdcMem);
- DrawBalls(hdcMem);
- DrawCueStick(hdcMem);
- DrawUIElements(hdcMem);
- if (foul) DrawFoul(hdcMem);
- BitBlt(hdc, 0, 0, TABLE_WIDTH, TABLE_HEIGHT, hdcMem, 0, 0, SRCCOPY);
- SelectObject(hdcMem, hbmOld);
- DeleteObject(hbmMem);
- DeleteDC(hdcMem);
- EndPaint(hwnd, &ps);
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
- switch (uMsg) {
- case WM_CREATE:
- SetTimer(hwnd, 1, 16, NULL);
- break;
- case WM_PAINT:
- Paint(hwnd);
- break;
- case WM_TIMER:
- UpdateGame();
- InvalidateRect(hwnd, NULL, FALSE);
- break;
- case WM_LBUTTONDOWN:
- OnLButtonDown(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
- break;
- case WM_MOUSEMOVE:
- OnMouseMove(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
- break;
- case WM_LBUTTONUP:
- OnLButtonUp(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) {
- const wchar_t CLASS_NAME[] = L"8BallPoolClass";
- WNDCLASS wc = {};
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = CLASS_NAME;
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- RegisterClass(&wc);
- HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"8-Ball Pool", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, TABLE_WIDTH, TABLE_HEIGHT, NULL, NULL, hInstance, NULL);
- ShowWindow(hwnd, nCmdShow);
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- ```
- ++=====================++
- ++=====================++ Superfluous?
- ```cpp
- void DrawUIElements(HDC hdc) {
- // Draw player turn indicator
- if (currentPlayer == 0) {
- DrawText(hdc, L"Player 1's Turn", -1, &turnIndicatorRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- } else {
- DrawText(hdc, L"Player 2's Turn", -1, &turnIndicatorRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- }
- // Draw pocketed balls
- DrawPocketedBalls(hdc);
- }
- void DrawFoul(HDC hdc) {
- HFONT hFont = CreateFont(72, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");
- HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
- SetTextColor(hdc, RGB(255, 0, 0));
- SetBkMode(hdc, TRANSPARENT);
- RECT rect = { 50, 50, TABLE_WIDTH - 50, TABLE_HEIGHT - 50 };
- DrawText(hdc, L"Foul!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- SelectObject(hdc, hOldFont);
- DeleteObject(hFont);
- }
- void DrawPocketedBalls(HDC hdc) {
- for (int i = 0; i < pocketedBallsPlayer1.size(); i++) {
- DrawBall(hdc, pocketedBallsPlayer1[i], 50 + i * BALL_RADIUS * 2, TABLE_HEIGHT - 30);
- }
- for (int i = 0; i < pocketedBallsPlayer2.size(); i++) {
- DrawBall(hdc, pocketedBallsPlayer2[i], 50 + i * BALL_RADIUS * 2, TABLE_HEIGHT - 60);
- }
- }
- void OnLButtonDown(int x, int y) {
- if (isCueBallInHand) {
- SetCueBallPosition(x, y);
- } else {
- aiming = true;
- aimStartX = x;
- aimStartY = y;
- }
- }
- void OnMouseMove(int x, int y) {
- if (aiming) {
- aimEndX = x;
- aimEndY = y;
- }
- }
- void OnLButtonUp(int x, int y) {
- if (aiming) {
- aiming = false;
- ShootCueBall(aimEndX - aimStartX, aimEndY - aimStartY);
- }
- }
- void SetCueBallPosition(int x, int y) {
- if (x >= CUE_BALL_MIN_X && x <= CUE_BALL_MAX_X && y >= CUE_BALL_MIN_Y && y <= CUE_BALL_MAX_Y) {
- balls[0].x = x;
- balls[0].y = y;
- }
- }
- void ShootCueBall(int dx, int dy) {
- balls[0].vx = dx * SHOT_STRENGTH;
- balls[0].vy = dy * SHOT_STRENGTH;
- isCueBallInHand = false;
- }
- void DrawBall(HDC hdc, Ball& ball, int x, int y) {
- HBRUSH hbr = CreateSolidBrush(ball.color);
- HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hbr);
- Ellipse(hdc, x - BALL_RADIUS, y - BALL_RADIUS, x + BALL_RADIUS, y + BALL_RADIUS);
- SelectObject(hdc, hOldBrush);
- DeleteObject(hbr);
- if (ball.type == SOLID || ball.type == STRIPE) {
- // Draw ball number or stripe here if necessary
- }
- }
- void DrawCueStick(HDC hdc) {
- if (aiming) {
- HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
- HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
- MoveToEx(hdc, balls[0].x, balls[0].y, NULL);
- LineTo(hdc, aimEndX, aimEndY);
- SelectObject(hdc, hOldPen);
- DeleteObject(hPen);
- }
- }
- void DrawTable(HDC hdc) {
- HBRUSH hbrTable = CreateSolidBrush(RGB(0, 128, 0));
- HBRUSH hbrEdge = CreateSolidBrush(RGB(255, 0, 0));
- SelectObject(hdc, hbrTable);
- Rectangle(hdc, 0, 0, TABLE_WIDTH, TABLE_HEIGHT);
- SelectObject(hdc, hbrEdge);
- Rectangle(hdc, 0, 0, TABLE_WIDTH, EDGE_WIDTH);
- Rectangle(hdc, 0, 0, EDGE_WIDTH, TABLE_HEIGHT);
- Rectangle(hdc, TABLE_WIDTH - EDGE_WIDTH, 0, TABLE_WIDTH, TABLE_HEIGHT);
- Rectangle(hdc, 0, TABLE_HEIGHT - EDGE_WIDTH, TABLE_WIDTH, TABLE_HEIGHT);
- DeleteObject(hbrTable);
- DeleteObject(hbrEdge);
- DrawPockets(hdc);
- }
- void DrawPockets(HDC hdc) {
- HBRUSH hbrPocket = CreateSolidBrush(RGB(0, 0, 0));
- HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hbrPocket);
- for (int i = 0; i < 6; i++) {
- Ellipse(hdc, pockets[i].x - POCKET_RADIUS, pockets[i].y - POCKET_RADIUS, pockets[i].x + POCKET_RADIUS, pockets[i].y + POCKET_RADIUS);
- }
- SelectObject(hdc, hOldBrush);
- DeleteObject(hbrPocket);
- }
- ```
- ++=====================++
- ++=====================++
- #Can you repeat the source code provided above between "void HandleFoul() {" and "void DrawFoul(HDC hdc) {" because " RECT foulRect = { 50, 50, 250, 150 };
- InvalidateRect(hwnd, &foulRect, TRUE);
- }" seems out of place after continuing the code! Don't show the full entire code, only between those two points and why " RECT foulRect = { 50, 50, 250, 150 };
- InvalidateRect(hwnd, &foulRect, TRUE);
- }" is shown when it's out of the scope of "void HandleFoul() {" Thank you!
- Sure, here is the code between `void HandleFoul() {` and `void DrawFoul(HDC hdc) {` with the `RECT foulRect = { 50, 50, 250, 150 }; InvalidateRect(hwnd, &foulRect, TRUE);` properly placed within the `HandleFoul` function:
- ```cpp
- void HandleFoul() {
- isCueBallInHand = true;
- RECT foulRect = { 50, 50, 250, 150 };
- InvalidateRect(hwnd, &foulRect, TRUE);
- currentPlayer = (currentPlayer + 1) % 2;
- }
- void DrawFoul(HDC hdc) {
- HFONT hFont = CreateFont(72, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");
- HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
- SetTextColor(hdc, RGB(255, 0, 0));
- SetBkMode(hdc, TRANSPARENT);
- RECT rect = { 50, 50, TABLE_WIDTH - 50, TABLE_HEIGHT - 50 };
- DrawText(hdc, L"Foul!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- SelectObject(hdc, hOldFont);
- DeleteObject(hFont);
- }
- ```
- In this revised version, the `RECT foulRect = { 50, 50, 250, 150 }; InvalidateRect(hwnd, &foulRect, TRUE);` line is placed inside the `HandleFoul` function where it belongs, ensuring that the invalidation of the rectangle occurs when a foul is handled. This triggers a redraw of the specified rectangle, which is needed to display the "Foul!" message properly.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement