bstacles; // 障碍物的位置 void CreateRandomPoints(std::vector<point>& points, int count) { while (points.size() < count) { point p; p.x = rand() % grid_size; p.y = rand() % grid_size; bool valid = true; for (const point& other : points) { if (p.x == other.x && p.y == other.y) { valid = false; break; } } if (valid) { points.push_back(p); } } } void DrawGrid(HWND hwnd, HDC hdc) { for (int x = 0; x < grid_size; x += grid_size / 10) { MoveToEx(hdc, x, 0, NULL); LineTo(hdc, x, grid_size); } for (int y = 0; y < grid_size; y += grid_size / 10) { MoveToEx(hdc, 0, y, NULL); LineTo(hdc, grid_size, y); } } void DrawBoxes(HWND hwnd, HDC hdc) { for (const point& p : boxes) { BitBlt(hdc, p.x, p.y, grid_size, grid_size, hdcBox, 0, 0, SRCCOPY); } } void DrawTargets(HWND hwnd, HDC hdc) { for (const point& p : targets) { SelectObject(hdc, htargetbrush); Ellipse(hdc, p.x, p.y, p.x + grid_size, p.y + grid_size); } } void DrawObstacles(HWND hwnd, HDC hdc) { for (const point& p : obstacles) { SelectObject(hdc, hobstaclebrush); Rectangle(hdc, p.x, p.y, p.x + grid_size, p.y + grid_size); } } LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch(Message) { case WM_CREATE: { // Load resources hboxbitmap = (HBITMAP)LoadImage(NULL, "box.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); htargetbrush = CreateSolidBrush(RGB(255, 0, 0)); hobstaclebrush = CreateSolidBrush(RGB(128, 128, 128)); srand(time(0)); CreateRandomPoints(boxes, box_count); CreateRandomPoints(targets, target_count); CreateRandomPoints(obstacles, obstacle_count); break; } case WM_PAINT: { hdc = BeginPaint(hwnd, &ps); DrawGrid(hwnd, hdc); DrawBoxes(hwnd, hdc); DrawTargets(hwnd, hdc); DrawObstacles(hwnd, hdc); EndPaint(hwnd, &ps); break; } case WM_DESTROY: { // Release resources DeleteObject(hboxbitmap); DeleteObject(htargetbrush); DeleteObject(hobstaclebrush); PostQuitMessage(0); break; } default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG msg; memset(&wc, 0, sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = "MyClass"; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "MyClass", "Win32 App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); if (hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } [2024-10-31 15:01:39 | AI写代码神器 | 2686点数解答]