简单的爱心效果

棋类

#include <windows.h>
#include <windowsx.h>
#include <ShObjIdl.h>
#define CELL_SIZE 100
#define BOARD_SIZE 3
HINSTANCE hInstance;
int board[BOARD_SIZE][BOARD_SIZE] = {0};
int currentPlayer = 1;
bool gameOver = false;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    hInstance = hInst;
    WNDCLASS wc = {0};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInst;
    wc.lpszClassName = "MainWindowClass";
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    RegisterClass(&wc);
    HWND hwnd = CreateWindow("MainWindowClass", "Tic Tac Toe", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CELL_SIZE * BOARD_SIZE + 70, CELL_SIZE * BOARD_SIZE + 70, NULL, NULL, hInst, NULL);
    SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
    DeleteMenu(GetSystemMenu(hwnd, FALSE), SC_SIZE, MF_BYCOMMAND);
    DeleteMenu(GetSystemMenu(hwnd, FALSE), SC_MAXIMIZE, MF_BYCOMMAND);
    DeleteMenu(GetSystemMenu(hwnd, FALSE), SC_MINIMIZE, MF_BYCOMMAND);
    DeleteMenu(GetSystemMenu(hwnd, FALSE), SC_RESTORE, MF_BYCOMMAND);
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); 
    
    // 从任务管理器获取图标
    HICON hIcon;
    ExtractIconEx("C:\\Windows\\System32\\taskmgr.exe", 0, NULL, &hIcon, 1);
    // 设置程序图标
    SendMessage(GetActiveWindow(), WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    SendMessage(GetActiveWindow(), WM_SETICON, ICON_BIG, (LPARAM)hIcon);

    
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_CREATE:
            break;
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            
            for(int i = 1; i <= 4; i++)
            {
                MoveToEx(hdc, CELL_SIZE * (i - 1) + 5, 5, NULL);
                LineTo(hdc, CELL_SIZE * (i - 1) + 5, CELL_SIZE * 3 + 5);
                MoveToEx(hdc, 5, CELL_SIZE * (i - 1) + 5, NULL);
                LineTo(hdc, CELL_SIZE * 3 + 5, CELL_SIZE * (i - 1) + 5);
            }
            
            for(int i = 0; i < BOARD_SIZE; i++)
            {
                for(int j = 0; j < BOARD_SIZE; j++)
                {
                    RECT cellRect = {i * CELL_SIZE, j * CELL_SIZE, (i + 1) * CELL_SIZE, (j + 1) * CELL_SIZE};
                    if(board[i][j] == 1)
                    {
                        Ellipse(hdc, cellRect.left + 15, cellRect.top + 15, cellRect.right - 5, cellRect.bottom - 5);
                    }
                    else if(board[i][j] == 2)
                    {
                        MoveToEx(hdc, cellRect.left + 15, cellRect.top + 15, NULL);
                        LineTo(hdc, cellRect.right - 5, cellRect.bottom - 5);
                        MoveToEx(hdc, cellRect.right - 5, cellRect.top + 15, NULL);
                        LineTo(hdc, cellRect.left + 15, cellRect.bottom - 5);
                    }
                }
            }
            EndPaint(hwnd, &ps);
            break;
        }
        case WM_LBUTTONDOWN:
        {
            if(gameOver)
            {
                break;
            }
            int x = GET_X_LPARAM(lParam) / CELL_SIZE;
            int y = GET_Y_LPARAM(lParam) / CELL_SIZE;
            if(board[x][y] == 0)
            {
                board[x][y] = currentPlayer;
                if(currentPlayer == 1)
                {
                    currentPlayer = 2;
                }
                else
                {
                    currentPlayer = 1;
                }
                InvalidateRect(hwnd, NULL, TRUE);
                // Check for game over
                for(int i = 0; i < BOARD_SIZE; i++)
                {
                    if(board[i][0] != 0 && board[i][0] == board[i][1] && board[i][1] == board[i][2])
                    {
                        gameOver = true;
                        MessageBox(hwnd, "Game over!", "Tic Tac Toe", MB_OK);
                        break;
                    }
                    if(board[0][i] != 0 && board[0][i] == board[1][i] && board[1][i] == board[2][i])
                    {
                        gameOver = true;
                        MessageBox(hwnd, "Game over!", "Tic Tac Toe", MB_OK);
                        break;
                    }
                }
                if(board[0][0] != 0 && board[0][0] == board[1][1] && board[1][1] == board[2][2])
                {
                    gameOver = true;
                    MessageBox(hwnd, "Game over!", "Tic Tac Toe", MB_OK);
                    break;
                }
                if(board[0][2] != 0 && board[0][2] == board[1][1] && board[1][1] == board[2][0])
                {
                    gameOver = true;
                    MessageBox(hwnd, "Game over!", "Tic Tac Toe", MB_OK);
                    break;
                }
                gameOver = true;
                for(int i = 0; i < BOARD_SIZE; i++)
                    if(board[i][0] == 0 || board[i][1] ==0 || board[i][2] == 0)
                        gameOver = false; 
                if(gameOver == true){
                    MessageBox(hwnd, "Game over!", "Tic Tac Toe", MB_OK);
                    break;
                }
            }
            break;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

 

posted @ 2023-06-07 13:45  Light-Chaser  阅读(7)  评论(0编辑  收藏  举报
简单的爱心效果