>_<: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 推箱子 迷宫的更多相关文章

  1. [游戏模版2] Win32最小框架

    >_<:Just the minimum Win32  frame don't have any other special function. //{{NO_DEPENDENCIES}} ...

  2. [游戏模版18] Win32 五子棋

    >_<:Learning its AI logic. >_<:resource >_<:code: #include <windows.h> // C ...

  3. [游戏模版3] Win32 画笔 画刷 图形

    >_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and s ...

  4. [游戏模版4] Win32 显示鼠标位置

    >_<:use MOUSE_MOVE message refresh the position information. >_<:use LOWORD(lParam) get ...

  5. [游戏模版5] Win32 折线 弧线

    >_<:first build some points put in poly1[],poly2[] and poly3[] in the function of InitInstance ...

  6. [游戏模版6] Win32 graph

    >_<:there in the MyPaint(...) function respectively use Ellipse(...) draw ellipse, use RoundRe ...

  7. [游戏模版7] Win32 最简单贴图

    >_<:this is the first using mapping. >_<:There will be introducing how to do: First load ...

  8. [游戏模版8] Win32 透明贴图

    >_<:The same with previous introduction. In the InitInstance fanction make a little change: &g ...

  9. [游戏模版9] Win32 半透明 图像处理

    >_<:Previous part we talk about how to map a transparent picture, and this time we will solve ...

随机推荐

  1. 配置 node.js 环境

    安装 Node.js 1. 下载 Node.js, 首先到官网 http://nodejs.org/download/ 的下载页面下载 Windows 版本, 这里有两种版本,推荐 .msi 的安装程 ...

  2. Mosquitto-Ubuntu 14.04快速安装问题解决

    Mosquitto是一个轻量级的MQTT Broker,支持很多种系统. 下载与安装:http://mosquitto.org/download/ 注意:由于客户端paho工程进展较快,目前需要使用最 ...

  3. 性能改善之For与Foreach

    关于For与Foreach的区别,博客园里已经有好多这样文章了,都分析的挺好:http://www.cnblogs.com/jobs/archive/2004/07/17/25218.aspx  不过 ...

  4. iOS应用之微信支付集成-直接前端集成

    所有信息的生成都在前端完成,包括对订单进行sign签名以及MD5签名加密(此方法相对来说有些复杂,没有官方给的方法简单).注:官方给的是v3&v4支付流程,签名和加密都是在服务器端进行,由于没 ...

  5. node.js 基础学习笔记2

    Module和Package是Node.js最重要的支柱. Node.j 提供require函数来调用其他模块,而且模块都是基于文件.模块和包区别是透明的,因此常常不作区分. 1.模块和文件一一对应. ...

  6. 小甲鱼python视频弟十二讲(关于字符串的方法及注释下)

    1,ljust(width[, fillchar])  width -- 指定字符串长度. fillchar -- 填充字符,默认为空格. 用法:返回一个原字符串左对齐,并使用空格填充至指定长度的新字 ...

  7. Python使用中文注释和输出中文(原创)

    刚开始学习python,需要在Python中注释中文和输出中文,现在开始尝试: 仅为初步学习参考,高手请绕行. -------------------------------------------- ...

  8. [MyBean-插件]MyBean通用报表免费无限制版本发布

      [优点]    1.开发时无需安装报表组件(可以直接用编译好的文件,注意版权说明,请自行编译一次相应的报表插件文件).    2.无带包烦恼所有版本Delphi都可以使用,不拖累Delphi版本的 ...

  9. jquery css

    jQuery提供css()的方法来实现嵌入式改变元素样式,css()方法在使用上具有多样性.其中一种接受两个输入参数:样式属性和样式值,它们之间用逗号分开.比如我们要改变链接颜色,我们可以使用下面的代 ...

  10. boa移植

    1.交叉编译 2.复制文件 配置文件boa.conf 移动到/etc/boa/ 目录下 可执行文件boa移动到/usr/sbin/目录下 3.修改配置文件 4.将Linux系统上/etc/mime.t ...