Win32实现迷宫
跟着杨立祥老师的课程,为了完成扫雷的作业,打算先用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);
}
效果图:
Win32实现迷宫的更多相关文章
- [游戏模版17] Win32 推箱子 迷宫
>_<:Here introduce a simple game: >_<:resource >_<:only can push a box and finally ...
- C语言动态走迷宫
曾经用C语言做过的动态走迷宫程序,先分享代码如下: 代码如下: //头文件 #include<stdio.h> #include<windows.h>//Sleep(500)函 ...
- C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper
前言 在开发应用程序时,通常只让程序运行一个实例.所以,就要判断程序是否已经运行. 下面是我自己在项目中使用到,封装好的帮助类.有 普通的 C# 应用程序 和 Windows CE 和 Windows ...
- java.lang.UnsatisfiedLinkError: %1 不是有效的 Win32 应用程序。
JNA 调用 dll 库时,保错: ///////////////// 通过 JNA 引入 DLL 库 //////////// /** * ID_FprCap.dll 负责指纹的采集, 指纹仪的初始 ...
- 初次认识 C# win32 api
第一次接触win32api,刚开始的时候有点迷迷糊糊的. Windows API 就是windows应用程序接口. win api向上就是windows应用程序,向下就是windows操作系统核心. ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- [老文章搬家] [翻译] 深入解析win32 crt 调试堆
09 年翻译的东西. 原文见: http://www.nobugs.org/developer/win32/debug_crt_heap.html 在DeviceStudio的Debug编译模式下, ...
- Virus.Win32.Virlock.b分析
0x00 样本说明 分析样本是被0b500d25f645c0b25532c1e3c9741667的样本感染得到.感染前的文件是Tcpview.exe,一款windows网络连接查看工具. 感染前后文件 ...
- BFS_Maze_求解迷宫最短路径
/* 10 10 #.######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .## ...
随机推荐
- 超详细Node安装教程
今天周末休息,我制定了我的2020年度规划,其中包含编写50篇养成写博文的习惯.算下来平均每周一篇,感觉也不是很难,但我的写作能力不是很好,争取一次比一次好!希望自己能够坚持下去.2020为自己而活, ...
- [gitHub实践] git基础:远程仓库的使用
[gitHub实践] git基础:远程仓库的使用 版权2019.6.2更新 git 基础 远程仓库的使用 git remote # 查看远程仓库 $ git remote # 克隆的仓库服务器默认名字 ...
- 如何让接口文档自动生成,SpringBoot中Swagger的使用
目录 一.在SpringBoot项目中配置Swagger2 1.pom.xml中对Swagger2的依赖 2.编写配置类启用Swagger 3.配置实体类的文档 4.配置接口的文档 5.访问文档 二. ...
- Persistence.beans
SF_USERS user = new SF_USERS(); user.setCTIME("20170103"); String ids = "fish,water&q ...
- es5中数组的遍历方法
//for循环 const arr = [1,2,3,4,5] for(let i = 0; i < arr.length; i++){ if(arr[i] === 2){ //break // ...
- axios全局引用
在vue项目开发中,我们使用axios进行ajax请求,很多人一开始使用axios的方式,会当成vue-resoure的使用方式来用,即在主入口文件引入import VueResource from ...
- 读取Core下的appsettings.json的值的时候中文乱码
这个百度一下一大堆,我就用的这个:然后重新生成一次就好了. 2.有的是更改VS的什么高级保存之类的,我记得之气设置过, 然后就是:这篇文章
- Scrapy解析器xpath
一.使用xpath 不在scrapy框架中通过response from scrapy.http import HtmlResponse HtmlResponse->TextResponse-& ...
- 「 扫盲 」Web服务器和应用服务器的区别
我们经常使用apache,tomcat,nginx,jetty等服务器,但并不清楚它们间的区别,它们中,哪些是Web服务器,哪些是应用服务器?今天就来告诉你 Web服务器 理解WEB服务器,首先你要理 ...
- ninject 的 实现 的 理解
mvc 用ninject 好像 有 的. 加上 ClassDiagram .ClassDiagram1.rar Represents a site on a type where a value c ...