>_<: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. CSS3常用选择器(二)

    本文继续介绍css3新增的选择器. 1.选择器 first-child.last-child.nth-child 和 nth-last-child 利用这几个选择器能够针对一个父元素中的第一个子元素. ...

  2. C# 定制 Attribute 简单使用

    所谓 “定制Attribute”,就是一个类的实例,它被序列化成驻留在元数据的一个字节流. 我们可以使用 Attribute 来保存注释: namespace AttributeDemo { [Att ...

  3. C#去掉list集合中的重复数据

    List<string> conList= new List<string>(); List<string> listII = new List<string ...

  4. DIOCP之EchoServer分析

    constructor TfrmMain.Create(AOwner: TComponent);begin inherited Create(AOwner); sfLogger.setAppender ...

  5. SVM3 Soft Margin SVM

    之前分为两部分讨论过SVM.第一部分讨论了线性SVM,并且针对线性不可分的数据,把原始的问题转化为对偶的SVM求解.http://www.cnblogs.com/futurehau/p/6143178 ...

  6. artDialog测试

    artDialog测试 <script src="../Content/artDialog/v4/artDialog.source.js?skin=default">& ...

  7. 【Tyvj1601】魔兽争霸(主席树,树套树)

    题意:要求在N个数的序列中支持以下操作: 1:将第X个元素加上Y 2:询问当前K大值 n<=30000,m<=50000 思路:树状数组套主席树 Tyvj又炸了,还不知道对不对 ..12] ...

  8. C# 生成条形码

    原文地址:http://www.cnblogs.com/xcsn/p/4514759.html 引用BarcodeLib.dll(百度云中有)生成条形 protected void Button2_C ...

  9. Java关键字总结及详解

    Java关键字是Java的保留字,这些保留字不能用来作为常量.变量.类名.方法名及其他一切标识符的名称. 一.基本数据类型 Java中有八种基本数据类型,六种数字类型(四个整数型.六中浮点型),一种字 ...

  10. javascript 的 clientX用法

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...