您的位置:首页 > 其它

[游戏模版17] Win32 推箱子 迷宫

2014-05-17 15:58 246 查看
>_<: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=5,cols=5;
int kind[]={5,8,8,10,10,10,16,16,16,16,20,20,20,20,25,25,25,25,25},KindNum=0;
int bilv=400/rows;
int Dis;//终点位置
int mapIndex[160000]={0,2,0,0,0,0,0,0,
0,1,0,1,1,1,1,0,
0,1,0,1,0,1,1,0,
0,1,0,0,0,1,1,0,
0,1,1,1,1,1,1,0,
0,1,0,0,0,0,1,0,
0,0,1,1,1,1,1,0,
0,0,0,0,0,0,3,0};

int record[160000];//用来标记不可走方格或已经走过的方格
// 此代码模块中包含的函数的前向声明:
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, 0, 0)){
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        = 0;
wcex.cbWndExtra        = 0;
wcex.hInstance        = hInstance;
wcex.hIcon            = NULL;
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
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, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,10,10,640,480,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,1,220,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-1;
right=nowPos+1;

switch(wParam){//上下左右
case VK_UP:
if(up>=0 && mapIndex[up])//往上走
{
prePos=nowPos;
nowPos=up;

if(mapIndex[nowPos]==3)
FIND=true;

MyPaint(hdc);
}
else if(up>=cols && !mapIndex[up] && mapIndex[up-cols]==1)//向上推箱子
{
mapIndex[up]=1;
mapIndex[up-cols]=0;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((up-cols)%rows),bilv*((up-cols)/cols),bilv,bilv,bufdc,0,0,SRCCOPY);

prePos=nowPos;
nowPos=up;

MyPaint(hdc);
}break;
case VK_DOWN:
if(down<=cols*rows-1 && mapIndex[down])//往下走
{
prePos=nowPos;
nowPos=down;

if(mapIndex[nowPos]==3)
FIND=true;

MyPaint(hdc);
}
else if(down<=cols*rows-cols-1 && !mapIndex[down] && mapIndex[down+cols]==1)//向下推箱子
{
mapIndex[down]=1;
mapIndex[down+cols]=0;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((down+cols)%rows),bilv*((down+cols)/cols),bilv,bilv,bufdc,0,0,SRCCOPY);

prePos=nowPos;
nowPos=down;

MyPaint(hdc);
}break;
case VK_LEFT:
if(left>=rowNum*cols && mapIndex[left])//往左走
{
prePos=nowPos;
nowPos=left;

if(mapIndex[nowPos]==3)
FIND=true;

MyPaint(hdc);
}
else if(left>=rowNum*cols+1 && !mapIndex[left] && mapIndex[left-1]==1)//往左推箱子
{
mapIndex[left]=1;
mapIndex[left-1]=0;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((left-1)%rows),bilv*((left-1)/cols),bilv,bilv,bufdc,0,0,SRCCOPY);

prePos=nowPos;
nowPos=left;

MyPaint(hdc);
}break;
case VK_RIGHT:
if(right<=(rowNum+1)*cols-1 &&  mapIndex[right])//往右走
{
prePos=nowPos;
nowPos=right;

if(mapIndex[nowPos]==3)
FIND=true;

MyPaint(hdc);
}
else if(right<=(rowNum+1)*cols-2 &&  !mapIndex[right] && mapIndex[right+1]==1)//往右推箱子
{
mapIndex[right]=1;
mapIndex[right+1]=0;
SelectObject(bufdc,tile);
BitBlt(mdc,bilv*((right+1)%rows),bilv*((right+1)/cols),bilv,bilv,bufdc,0,0,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,1);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

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,0,0,WHITENESS);

//小球贴图
rowNum=nowPos/cols;
colNum=nowPos%rows;
x=colNum*bilv;
y=rowNum*bilv;
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,bilv,bilv,bufdc,0,0,SRCCOPY);

if(!FIND){
str = "找寻出口中...";
}else{
str="找到出口了!";
cols=rows=kind[(++KindNum)%19];
PreparePaint();
}

rowNum=Dis/cols;
colNum=Dis%rows;
x=colNum*bilv;
y=rowNum*bilv;
SelectObject(bufdc,dis);
BitBlt(mdc,x,y,bilv,bilv,bufdc,0,0,SRCCOPY);

TextOutA(hdc,430,10,str,strlen(str));
BitBlt(hdc,10,10,cols*bilv,rows*bilv,mdc,0,0,SRCCOPY);
}
/*生成迷宫函数*/
void CreateMiGong(int Hang){
srand((unsigned)time(NULL));
for(int i=0;i<Hang*Hang;i++)
mapIndex[i]=rand()%2;
mapIndex[rand()%(Hang*Hang)]=2;
mapIndex[Dis=rand()%(Hang*Hang)]=3;
}
/*准备阶段贴图*/
void PreparePaint(){
bilv=400/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=0;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,0,0,SRCCOPY);
else {
if(mapIndex[i]==2){//迷宫入口
nowPos=i;
mapIndex[i]=1;
}
BitBlt(mdc,x,y,bilv,bilv,bufdc,0,0,WHITENESS);
}
}
prePos=cols*rows+1;//第一次在窗口外绘图不影响效果,以后记录上一次小球位置并贴图覆盖原来小球影像
FIND=false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: