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 #.######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .## ...
随机推荐
- 1086 就不告诉你 (15分)C语言
做作业的时候,邻座的小盆友问你:"五乘以七等于多少?"你应该不失礼貌地微笑着告诉他:"五十三."本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积. 输入 ...
- red note8 pro谷歌套件
谷歌核心Apps(即Google官方应用“全家桶”),包括YouTube,Google Now,Google Play store,Google Play Games,Google Maps等: 基于 ...
- SpringCloud入门系列0-Nacos的安装与配置
背景 工作有一些年头了,自从19年初彻底转了java(这又是另一篇心酸的故事),突然感觉自己荒废了好几年(不是说.net不好,而是回顾自己这几年做的很多东西都浮于表面,有时候弄成很忙的样子,回头看看自 ...
- nginx 负载均衡的配置
首先搭建好三台nginx,我是用VM搭建的 nginx搭建,https://www.cnblogs.com/liubaoqing/p/10507962.html 这里的三台nginx ,ip分别是 1 ...
- 基于Arduino的按键控制LED实验
I/O 口的意思即为INPUT 接口和OUTPUT 接口,到目前为止我们设计的小灯实验都还只是应用到Arduino 的I/O 口的输出功能,这个实验我们来尝试一下使用Arduino的I/O 口的输入功 ...
- es lucene搜索及聚合流程源码分析
本文以TermQuery,GlobalOrdinalsStringTermsAggregator为例,通过代码,分析es,lucene搜索及聚合流程.1:协调节点收到请求后,将search任务发到相关 ...
- pc和H5响应式方案
pc响应式:(所有应用在pc端) 解决方案1.媒体查询 2.flex,百分比 3.栅格布局 媒体查询 @media screen and (max-width:768px) @media scree ...
- Unable to open debugger port (127.0.0.1:57046): java.net.SocketException "so
原因分析: 出现这个报错的原因是因为端口被占用导致的. 解决方法: 解决方法主要两种:修改端口配置(推荐).关闭占用端口的进程(不推荐). 方式一:修改端口配置(推荐) 被占用的端口可能是本地端口,也 ...
- django.db.migrations.exceptions.MigrationSchemaMissing和raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
1.使用Python3.7 + Django2.2 + MySQL 5.5 在执行(python manage.py migrate)命令时出现错误django.db.migrations.excep ...
- Nginx作为web静态资源服务器——跨域访问
跨站访问 为什么浏览器禁止跨域访问 Nginx跨站访问 Syntax:add_header name value [always]; Default:—— Context:http,serve ...