[游戏模版17] Win32 推箱子 迷宫
>_<:Here introduce a simple game:
>_<:resource
>_<:only can push a box and finally arrive the gate.

#include <windows.h>
// C 运行时头文件
#include <stdlib.h>
#include <cstdio>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <time.h>
#include <string>
#include <stack> HINSTANCE hInst; HBITMAP ball,tile,dis;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow;
int nowPos,prePos;//在上次贴图位置贴白色去除残留影响
bool FIND;
int rows=,cols=;
int kind[]={,,,,,,,,,,,,,,,,,,},KindNum=;
int bilv=/rows;
int Dis;//终点位置
int mapIndex[]={,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,}; int record[];//用来标记不可走方格或已经走过的方格
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
void CreateMiGong(int Hang);
void PreparePaint();//准备阶段绘图 int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow){ MSG msg;
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow)){
return FALSE;
}
// 主消息循环:
while (GetMessage(&msg, NULL, , )){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
} // 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
ATOM MyRegisterClass(HINSTANCE hInstance){
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = ;
wcex.cbWndExtra = ;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wcex.lpszMenuName = "Beautifulzzzz";
wcex.lpszClassName = "Beautifulzzzz";
wcex.hIconSm = NULL; return RegisterClassEx(&wcex);
} // 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
// 1.设定飞机的初始位置
// 2.设定鼠标位置及隐藏
// 3.设定鼠标光标移动区域
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ HBITMAP bmp; hInst = hInstance; // 将实例句柄存储在全局变量中 hWnd = CreateWindow("Beautifulzzzz","Beautifulzzzz", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, , CW_USEDEFAULT, , NULL, NULL, hInstance, NULL); if (!hWnd)
{
return FALSE;
} MoveWindow(hWnd,,,,,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd); hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
bufdc=CreateCompatibleDC(hdc); bmp=CreateCompatibleBitmap(hdc,cols*bilv,rows*bilv);
SelectObject(mdc,bmp); PreparePaint(); SetTimer(hWnd,,,NULL);
MyPaint(hdc); return TRUE;
} //
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
int wmId, wmEvent;
int rowNum,colNum;
int x,y,up,down,left,right;
PAINTSTRUCT ps; switch (message)
{
case WM_KEYDOWN:
rowNum=nowPos/cols;
colNum=nowPos%rows;
x=colNum*bilv;
y=rowNum*bilv; up=nowPos-cols;
down=nowPos+cols;
left=nowPos-;
right=nowPos+; switch(wParam){//上下左右
case VK_UP:
if(up>= && mapIndex[up])//往上走
{
prePos=nowPos;
nowPos=up; if(mapIndex[nowPos]==)
FIND=true; MyPaint(hdc);
}
else if(up>=cols && !mapIndex[up] && mapIndex[up-cols]==)//向上推箱子
{
mapIndex[up]=;
mapIndex[up-cols]=;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((up-cols)%rows),bilv*((up-cols)/cols),bilv,bilv,bufdc,,,SRCCOPY); prePos=nowPos;
nowPos=up; MyPaint(hdc);
}break;
case VK_DOWN:
if(down<=cols*rows- && mapIndex[down])//往下走
{
prePos=nowPos;
nowPos=down; if(mapIndex[nowPos]==)
FIND=true; MyPaint(hdc);
}
else if(down<=cols*rows-cols- && !mapIndex[down] && mapIndex[down+cols]==)//向下推箱子
{
mapIndex[down]=;
mapIndex[down+cols]=;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((down+cols)%rows),bilv*((down+cols)/cols),bilv,bilv,bufdc,,,SRCCOPY); prePos=nowPos;
nowPos=down; MyPaint(hdc);
}break;
case VK_LEFT:
if(left>=rowNum*cols && mapIndex[left])//往左走
{
prePos=nowPos;
nowPos=left; if(mapIndex[nowPos]==)
FIND=true; MyPaint(hdc);
}
else if(left>=rowNum*cols+ && !mapIndex[left] && mapIndex[left-]==)//往左推箱子
{
mapIndex[left]=;
mapIndex[left-]=;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((left-)%rows),bilv*((left-)/cols),bilv,bilv,bufdc,,,SRCCOPY); prePos=nowPos;
nowPos=left; MyPaint(hdc);
}break;
case VK_RIGHT:
if(right<=(rowNum+)*cols- && mapIndex[right])//往右走
{
prePos=nowPos;
nowPos=right; if(mapIndex[nowPos]==)
FIND=true; MyPaint(hdc);
}
else if(right<=(rowNum+)*cols- && !mapIndex[right] && mapIndex[right+]==)//往右推箱子
{
mapIndex[right]=;
mapIndex[right+]=;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((right+)%rows),bilv*((right+)/cols),bilv,bilv,bufdc,,,SRCCOPY); prePos=nowPos;
nowPos=right; MyPaint(hdc);
}break;
}
break;
case WM_TIMER:
A:MyPaint(hdc);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
goto A;// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(ball);
DeleteObject(tile); KillTimer(hWnd,);
ReleaseDC(hWnd,hdc);
PostQuitMessage();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
} void MyPaint(HDC hdc){
char* str;
int rowNum,colNum;
int x,y;
int up,down,left,right; //清除上次贴图
rowNum=prePos/cols;
colNum=prePos%rows;
x=colNum*bilv;
y=rowNum*bilv;
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,bilv,bilv,bufdc,,,WHITENESS); //小球贴图
rowNum=nowPos/cols;
colNum=nowPos%rows;
x=colNum*bilv;
y=rowNum*bilv;
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,bilv,bilv,bufdc,,,SRCCOPY); if(!FIND){
str = "找寻出口中...";
}else{
str="找到出口了!";
cols=rows=kind[(++KindNum)%];
PreparePaint();
} rowNum=Dis/cols;
colNum=Dis%rows;
x=colNum*bilv;
y=rowNum*bilv;
SelectObject(bufdc,dis);
BitBlt(mdc,x,y,bilv,bilv,bufdc,,,SRCCOPY); TextOutA(hdc,,,str,strlen(str));
BitBlt(hdc,,,cols*bilv,rows*bilv,mdc,,,SRCCOPY);
}
/*生成迷宫函数*/
void CreateMiGong(int Hang){
srand((unsigned)time(NULL));
for(int i=;i<Hang*Hang;i++)
mapIndex[i]=rand()%;
mapIndex[rand()%(Hang*Hang)]=;
mapIndex[Dis=rand()%(Hang*Hang)]=;
}
/*准备阶段贴图*/
void PreparePaint(){
bilv=/rows;
tile=(HBITMAP)LoadImageA(NULL,"tile.bmp",IMAGE_BITMAP,bilv,bilv,LR_LOADFROMFILE);
ball=(HBITMAP)LoadImageA(NULL,"ball.bmp",IMAGE_BITMAP,bilv,bilv,LR_LOADFROMFILE);
dis=(HBITMAP)LoadImageA(NULL,"dis.bmp",IMAGE_BITMAP,bilv,bilv,LR_LOADFROMFILE); int rowNum,colNum,x,y;
CreateMiGong(cols);
//按照mapIndex数组中的定义进行迷宫拼接
//贴上终点
for(int i=;i<rows*cols;i++){
record[i]=mapIndex[i]; rowNum=i/cols;//列编号
colNum=i%rows;//行编号
x=colNum*bilv;//求贴图x坐标
y=rowNum*bilv;//求贴图y坐标 SelectObject(bufdc,tile); if(!mapIndex[i])//墙
BitBlt(mdc,x,y,bilv,bilv,bufdc,,,SRCCOPY);
else {
if(mapIndex[i]==){//迷宫入口
nowPos=i;
mapIndex[i]=;
}
BitBlt(mdc,x,y,bilv,bilv,bufdc,,,WHITENESS);
}
}
prePos=cols*rows+;//第一次在窗口外绘图不影响效果,以后记录上一次小球位置并贴图覆盖原来小球影像
FIND=false;
}
[游戏模版17] Win32 推箱子 迷宫的更多相关文章
- [游戏模版2] Win32最小框架
>_<:Just the minimum Win32 frame don't have any other special function. //{{NO_DEPENDENCIES}} ...
- [游戏模版18] Win32 五子棋
>_<:Learning its AI logic. >_<:resource >_<:code: #include <windows.h> // C ...
- [游戏模版3] Win32 画笔 画刷 图形
>_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and s ...
- [游戏模版4] Win32 显示鼠标位置
>_<:use MOUSE_MOVE message refresh the position information. >_<:use LOWORD(lParam) get ...
- [游戏模版5] Win32 折线 弧线
>_<:first build some points put in poly1[],poly2[] and poly3[] in the function of InitInstance ...
- [游戏模版6] Win32 graph
>_<:there in the MyPaint(...) function respectively use Ellipse(...) draw ellipse, use RoundRe ...
- [游戏模版7] Win32 最简单贴图
>_<:this is the first using mapping. >_<:There will be introducing how to do: First load ...
- [游戏模版8] Win32 透明贴图
>_<:The same with previous introduction. In the InitInstance fanction make a little change: &g ...
- [游戏模版9] Win32 半透明 图像处理
>_<:Previous part we talk about how to map a transparent picture, and this time we will solve ...
随机推荐
- UART UVM验证平台平台搭建总结
tb_top是整个UVM验证平台的最顶层:tb_top中例化dut,提供时钟和复位信号,定义接口以及设置driver和monitor的virual interface,在intial中调用run_te ...
- 2016.02.02 JS事件
今天看完JS事件部分,所剩时间不多,务必分秒珍惜.
- c++学习笔记——聚合类
聚合类定义:1.所有的成员都是public的. 2.没有定义任何构造函数. 3.没有类内初始值. 4.没有基类,也没有virtual函数. 聚合类的初始化:我们可以提供一个花括号括起来的成员函数初始值 ...
- 把 TBytes 转换为十六进制字符串
function BytestoHexString(ABytes: TBytes; len: Integer): AnsiString; begin SetLength(Result, len*) ...
- Groovy basic
1. print println "Hello Groovy!" you can use java in Groovy System.out.println("Hello ...
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...
- UI组件之Group
当Group旋转或缩放时,它的孩子们正常绘制,并且Batch变换后正确的旋转或缩放. 绘制Group前,Batch flush使得变换可以设置.有很多Group时这将可能成为性能瓶颈.如果在一组演员不 ...
- node.js 基础学习笔记3 -http
http模块,其中封装了一个高效的HTTP服务器和一个建议的HTTP客户端 http.server是一个基于事件的HTTP服务器 http.request则是一个HTTP客户端工具,用户向服务器发送请 ...
- MVC学习(四)几种分页的实现(3)
在这篇MVC学习(四)几种分页的实现(2)博文中,根据URL中传入的两个参数(页码数,首页.上一页.下一页.末页的标记符)来获得对应的分页数据, 只是传入的参数太多,调用起来不太方便(标记符不能够写错 ...
- 剑指offer题目41-50
面试题41:和为S的连续正整数序列 import java.util.ArrayList; public class Solution { public ArrayList<ArrayList& ...