Win32实现迷宫
2024-10-08 05:01:08
跟着杨立祥老师的课程,为了完成扫雷的作业,打算先用DFS/BFS实现路径搜索的简单Demo。
生成迷宫:
/*
扫雷程序生成方砖
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <windowsx.h>
#include "resource.h" LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance,
PSTR szCmdLine, int iCmdshow)
{
static TCHAR szAppName[] = TEXT("Maze");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("registered error"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("Mazes"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hwnd, iCmdshow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBitmap;
static int cxClient, cyClient, cxSource, cySource, xClick, yClick;
static int leftPos, rightPos, topPos, bottomPos;
BITMAP bitmap;
HDC hdc, hdcMem;
PAINTSTRUCT ps;
int x, y;
HINSTANCE hInstance; switch (message)
{
case WM_CREATE:
PlaySound(TEXT("start.wav"), NULL, SND_FILENAME | SND_ASYNC);
srand((unsigned)time(NULL));
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
cxSource = bitmap.bmWidth;
cySource = bitmap.bmHeight / 3; return 0; case WM_SIZE:
cxClient = GET_X_LPARAM(lParam);
cyClient = GET_Y_LPARAM(lParam);
return 0; case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap); //方砖的范围(左上角)
topPos = cySource * 2;
leftPos = cxSource * 2; for (y = topPos; y <= cyClient - cySource * 3; y += cySource)
{
for (x = leftPos; x <= cxClient - cxSource * 3; x += cxSource)
{ if (rand() % 4 == 1)
{
BitBlt(hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY);
}
else
{
BitBlt(hdc, x, y, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY);
}
}
}
rightPos = x - cxSource;
bottomPos = y - cySource;
//左上:右下为出口
BitBlt(hdc, cxSource * 2, cySource * 2, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY);
BitBlt(hdc, rightPos, bottomPos, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY); DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
return 0;
case WM_LBUTTONDOWN:
xClick = GET_X_LPARAM(lParam);
yClick = GET_Y_LPARAM(lParam);
//无效区
if (xClick < leftPos || xClick > rightPos + cxSource || yClick < topPos || yClick > bottomPos + cySource)
{
break;
} for (y = topPos; y <= bottomPos; y += cySource)
{
if (yClick >= y && yClick <= y + cySource)
{
yClick = y;
break;
}
} for (x = leftPos; x <= rightPos; x += cxSource)
{
if (xClick >= x && xClick <= x + cxSource)
{
xClick = x;
break;
}
}
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap);
BitBlt(hdc, xClick, yClick, cxSource, cySource, hdcMem, 0, cySource * 2, SRCCOPY);
DeleteDC(hdcMem);
ReleaseDC(hwnd, hdc);
return 0;
case WM_DESTROY:
DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
效果图:
最新文章
- nginx+webpy配置
- iOS中修改头部tabBarButton 默认按钮的颜色和默认字体颜色
- CSS层叠样式表的层叠是什么意思(转自知乎)
- oracle通过plsql导入dmp数据文件
- Framework 类库的事件编程
- 基于visual Studio2013解决C语言竞赛题之1076放鞭炮
- TOGAF架构内容框架之内容元模型(上)
- 聊聊JAVA中 String类为什么不可变
- linux上搭建ftp
- wxWidgets 在 Linux 下开发环境配置
- [转]jquery异步ajax与服务器通信过程中如何通过then方法链式传递多层数据
- 为什么企业需要IT资产管理
- [HihoCoder1393]网络流三&#183;二分图多重匹配
- CXF框架入门(重点)
- 开发创建XMPP“发布订阅”扩展(xmpp pubsub extend)
- nodejs 爬虫模板 map&;array 数据模型
- ruby字符串连接
- 从零开始的Python学习Episode 8——深浅拷贝
- Jquery 欲绑定事件
- YBT 5.4 状态压缩动态规划