>_<: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. UVa 10300 - Ecological Premium

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  2. 【Django】Django model与数据库操作对应关系(转)

    Django对数据库的操作分用到三个类:Manager.QuerySet.Model. Manager的主要功能定义表级方法(表级方法就是影响一条或多条记录的方法),我们可以以models.Manag ...

  3. Genome-wide Complex Trait Analysis(GCTA)-全基因组复杂性状分析

    GCTA(全基因组复杂性状分析)工具开发目的是针对复杂性状的全基因组关联分析,评估SNP解释的表型方差所占的比例(该网站地址:http://cnsgenomics.com/software/gcta/ ...

  4. Hdu OJ 5884-Sort (2016 ACM/ICPC Asia Regional Qingdao Online)(二分+优化哈夫曼)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题目大意:有n个有序的序列,对于第i个序列有ai个元素. 现在有一个程序每次能够归并k个序列, ...

  5. MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型

    目录 简易版CMS后台管理系统开发流程 MVC5+EF6 简易版CMS(非接口) 第一章:新建项目 MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型 MVC5+EF6 简易版CMS(非接口 ...

  6. quick-cocos2d-x之testlua之VisibleRect.lua

    require "extern" --这个类找到了可视区域的9个点的坐标:左上.上的中点.右上.左的中点.左下.下的中点.右下.右的中点.一般用于使用相对坐标的场合,解决自适应屏幕 ...

  7. jetty 最后版本类库树, 基本上大多数应用都够了

    d:\jetty-distribution-8.1.17.v20150415\lib\annotations\javax.annotation-1.1.0.v201108011116.jarjavax ...

  8. 【Java】XML解析之JDOM

    JDOM介绍 JDOM是一个开源项目,它基于树型结构,利用纯JAVA的技术对XML文档实现解析.生成.序列化以及多种操作.使用jdom需要引入jdom.jar包. XML生成及解析 代码如下: pac ...

  9. 解决Failed to load class "org.slf4j.impl.StaticLoggerBinder"

    Hibernate使用SLF4J API记录日志,所以在Hibernate的lib中,不再提供Log4J的包,而大部分框架依然使用Log4J记录日志,这样导致了兼容性问题. 解决办法,两步: 一.在编 ...

  10. 表A的数据减去表B ,最终得到表C

    ==========表A数据如下:                 表B数据如下:cr     zc     lx                     cr    zc    lx100   10 ...