以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  CIW版推箱子 (转载,VC++实现)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=54595)


--  作者:goodtolove001
--  发布时间:10/29/2007 5:22:00 PM

--  CIW版推箱子 (转载,VC++实现)
这是一个Win版的推箱子,无聊之作.

程序名称: CIW_BLUE版 推箱子(附加一个地图编辑器)
实现语言: C++ & Win API

因为里面用到了大量图片,如果你想要这个游戏的源代码就留下你的信箱,我会第一时间发给你的.

程序代码简单介绍(程序分4个类):

CTitle :  实现一些标题的移动(这个可有可没,美化而已)
CMap   :  实现读取地图
CPaint :  实现把画面呈现出来
CMam   : 实现人物的移动,重新开始,悔棋,等

还有一个可以编辑地图的程序,这里就不介绍了.

下面是程序的入口函数:


--  作者:goodtolove001
--  发布时间:10/29/2007 5:23:00 PM

--  
#include "CMap.h"
#include "CMan.h"
#include "CPaint.h"
#include "CTitle.h"

#include <windows.h>
#include <cstdio>
#include <ctime>


///////////////////////////////////   游戏主要变量初始化    ///////////////////////////////

CMap map;
int CMap::m_numOfMap = 0;
CMan man(map);
char *pRoleBmpStr[8] = {"bmp//人物左1.bmp", "bmp//人物左2.bmp", "bmp//人物右1.bmp", "bmp//人物右2.bmp", "bmp//人物上1.bmp", "bmp//人物上2.bmp", "bmp//人物下1.bmp", "bmp//人物下2.bmp" };
CPaint paint("bmp//bg.bmp",pRoleBmpStr, "bmp//箱子.bmp", "bmp//格子.bmp", "bmp//空地.bmp", "bmp//围墙.bmp", "bmp//重合.bmp", "bmp//完成.bmp", "bmp//Win.bmp",30, 30 );
CTitle title_1("bmp//c.bmp");
CTitle title_2("bmp//i.bmp");
CTitle title_3("bmp//w.bmp");
CTitle title_4("bmp//title.bmp");

///////////////////////////////////  游戏主要变量初始化    ///////////////////////////////


//////////////////////////////////      窗口函数声明     ////////////////////////////////


--  作者:goodtolove001
--  发布时间:10/29/2007 5:23:00 PM

--  
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//////////////////////////////////      窗口函数声明     ////////////////////////////////

/////////////////////////////////       窗口句柄声明      ////////////////////////////////

HINSTANCE hInst;
HWND hWnd;

HWND hRestartBtn ;
HWND hPreviousBtn;
HWND hMapList;
HWND hOpenMapBtn;
HWND hOpenMapEditBtn;
HWND hUpdateMapBtn;
HWND hDeleteMapBtn;

////////////////////////////////         窗口句柄声明      ////////////////////////////////


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{


--  作者:goodtolove001
--  发布时间:10/29/2007 5:23:00 PM

--  
HDC hdc;
MSG msg;

srand( time(0) );
paint.m_xOffsetOfPaint = 50;
paint.m_yOffsetOfPaint = 68;

map.ReadMap("map//1.map");
    man.GetXAndYOfMan(map);

MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))  
{
return FALSE;
}


    while( msg.message!=WM_QUIT )
    {
        if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
else
{
paint.m_paintNow = GetTickCount();
if( paint.m_paintNow - paint.m_paintPass > 20 )
{
hdc = GetDC(hWnd);
paint.Paint(man, map, hdc );

title_1.Move(hWnd, hdc );
title_2.Move( hWnd, hdc );
title_3.Move(hWnd, hdc );
title_4.Move(hWnd, hdc );

ReleaseDC(hWnd, hdc);
}
}
    }

return msg.wParam;


--  作者:goodtolove001
--  发布时间:10/29/2007 5:24:00 PM

--  
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);  
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "CIW";
wcex.hIconSm = NULL;

return RegisterClassEx(&wcex);
}

void Change(char* str)
{
for(int i=0; i < (int)strlen(str); i++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] -= 'A' - 'a';
}
}
}
void AboutMe()
{
HWND h=FindWindowEx(NULL,NULL,NULL,"microsoft internet explorer") ;

    ShellExecute(h,"open","http://281011131.qzone.qq.com/",NULL,NULL,SW_SHOWNORMAL);
}
void ReadMapFile()
{
SendMessage(hMapList, CB_RESETCONTENT, 0, 0);
WIN32_FIND_DATA fd;
    char myFileName[100];
GetCurrentDirectory(100, myFileName);
strcat(myFileName, "\\map\\*.*");
    HANDLE hFile = FindFirstFile(myFileName, &fd);
char ch[4] = {0,0,0,0};
    do
{
if( fd.cFileName[0] != '.' )
{

if( fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
ch[0] = fd.cFileName[strlen(fd.cFileName) - 3];
ch[1] = fd.cFileName[strlen(fd.cFileName) - 2];
ch[2] = fd.cFileName[strlen(fd.cFileName) - 1];
Change(ch);
if(strcmp(ch, "map") == 0)
{
SendMessage(hMapList, CB_ADDSTRING, 0, (LPARAM)fd.cFileName);
}
}
}
}while( FindNextFile(hFile, &fd) );

SendMessage(hMapList, CB_SETCURSEL, 0, 0);
}

void DeleteMapFile()
{

int index = SendMessage(hMapList, CB_GETCURSEL, 0, 0);
if( index >= 0)
{
char selFile[100];
char mapName[100];
SendMessage(hMapList, CB_GETLBTEXT, index, (LPARAM)selFile);
sprintf(mapName, "map\\%s", selFile);
DeleteFile(mapName);
ReadMapFile();
}
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;

hWnd = CreateWindow("CIW", "推箱子 CIW_BLUE 制作" ,WS_MINIMIZEBOX | WS_SYSMENU,
500, 500, 300, 200, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,100,50,780,585,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
     
hMapList =CreateWindow("COMBOBOX", NULL,  CBS_SORT | CBS_DROPDOWNLIST| WS_CHILD| WS_VISIBLE, 600, 110, 140, 500, hWnd, NULL, hInst, NULL);
hRestartBtn  = CreateWindow("BUTTON", "重新开始", BS_DEFPUSHBUTTON |WS_CHILD | WS_VISIBLE, 600, 30, 100, 30, hWnd, NULL, hInst, NULL);
hPreviousBtn = CreateWindow("BUTTON", "悔棋", BS_DEFPUSHBUTTON |WS_CHILD | WS_VISIBLE, 600, 70, 100, 30, hWnd, NULL, hInst, NULL);
hOpenMapBtn = CreateWindow("BUTTON", "选择地图", BS_DEFPUSHBUTTON |WS_CHILD | WS_VISIBLE, 600, 150, 100, 30, hWnd, NULL, hInst, NULL);
    hOpenMapEditBtn = CreateWindow("BUTTON", "增加地图", BS_DEFPUSHBUTTON |WS_CHILD | WS_VISIBLE, 600, 180, 100, 30, hWnd, NULL, hInst, NULL);
hDeleteMapBtn = CreateWindow("BUTTON", "删除地图", BS_DEFPUSHBUTTON |WS_CHILD | WS_VISIBLE, 600, 210, 100, 30, hWnd, NULL, hInst, NULL);


--  作者:goodtolove001
--  发布时间:10/29/2007 5:24:00 PM

--  
ReadMapFile();
AboutMe();
return TRUE;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{


    HDC hdc;
switch (message)
{
case WM_KEYDOWN:

if(wParam == VK_ESCAPE)
{

SendMessage(hWnd, WM_COMMAND, 0, (LPARAM)hRestartBtn);
}

hdc = GetDC(hWnd);
man.GameLoop(map, wParam);

if(man.IsWin(map ))     
{
    man.m_direction = VK_DOWN;

man.m_numOfMove = 0;
InvalidateRect(hWnd, NULL, true);
paint.WinPaint( hdc);
PlaySound (TEXT ("Sound//Win.WAV"), NULL, SND_FILENAME | SND_ASYNC) ;
            Sleep(2000);

int index = SendMessage(hMapList, CB_GETCURSEL, 0, 0);
int allIndex= SendMessage(hMapList, CB_GETCOUNT, 0, 0);
if( index + 1 < allIndex )
{
char selFileName[100];
char mapName[100];
SendMessage(hMapList, CB_GETLBTEXT, index + 1, (LPARAM) (LPCSTR)selFileName);
sprintf(mapName, "map\\%s", selFileName);
map.ReadMap(  mapName );
man.GetXAndYOfMan(map);
SetFocus(hWnd);
SendMessage(hMapList, CB_SETCURSEL, index + 1, (LPARAM) (LPCSTR)selFileName);
}
else if(index + 1 == allIndex)
{

char selFileName[100];
char mapName[100];
SendMessage(hMapList, CB_SETCURSEL, 0, (LPARAM) (LPCSTR)selFileName);
SendMessage(hMapList, CB_GETLBTEXT, 0, (LPARAM) (LPCSTR)selFileName);
sprintf(mapName, "map\\%s", selFileName);
map.ReadMap(  mapName );
man.GetXAndYOfMan(map);
SetFocus(hWnd);

}
else
{
man.Restart(map);
man.GetXAndYOfMan(map);
SetFocus(hWnd);
}

}

ReleaseDC(hWnd,hdc);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_COMMAND:

if((HWND) lParam == hRestartBtn)
{

if(MessageBox(hWnd, "真的要重新开始吗?", "提示", MB_YESNO) ==IDYES)
{
InvalidateRect(hWnd, NULL, true);
man.Restart(map);
man.GetXAndYOfMan(map);
SetFocus(hWnd);
}

}

if((HWND) lParam == hPreviousBtn)
{
man.Previous(map);
SetFocus(hWnd);
}

if((HWND) lParam ==hOpenMapBtn)
{

int index = SendMessage(hMapList, CB_GETCURSEL, 0, 0);
if( index != -1 )
{
char selFileName[100];
char mapName[100];
memset(mapName, 0, 100);
SendMessage(hMapList, CB_GETLBTEXT, index, (LPARAM) (LPCSTR)selFileName);
sprintf(mapName, "map\\%s", selFileName);
map.ReadMap(  mapName );

SendMessage(hWnd, WM_COMMAND, 0, (LPARAM)hRestartBtn);
SetFocus(hWnd);
}
}

if((HWND) lParam ==hOpenMapEditBtn)
{
if(WinExec("推箱子地图制作.exe", SW_SHOW) == ERROR_FILE_NOT_FOUND  )
{
MessageBox(hWnd, "找不到地图编辑器,请确认和本程序在同个目录.", "错误提示", MB_OK | MB_ICONERROR);
}

}

if((HWND) lParam ==hDeleteMapBtn)
{
if(MessageBox(hWnd, "真的要删除当前选择的这个地图吗?", "提示", MB_YESNO ) ==IDYES)
{
DeleteMapFile();
SetFocus(hWnd);
}
}
if( HIWORD(wParam) == CBN_SELCHANGE ||  HIWORD(wParam) ==CBN_KILLFOCUS)
{
SetFocus(hWnd);
}

if( HIWORD(wParam) ==CBN_SETFOCUS)
{
ReadMapFile();

}

break;

case WM_RBUTTONDOWN:
man.Previous(map);
break;

case WM_CLOSE:
if(MessageBox(hWnd, "真的要退出游戏吗?", "提示", MB_YESNO ) ==IDYES)
{
ExitProcess(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


--  作者:goodtolove001
--  发布时间:10/29/2007 5:26:00 PM

--  
游戏的运行界面:
http://hiphotos.baidu.com/ciw_blue/pic/item/5ae0c1af7b150dc77dd92ace.jpg
地图编辑器运行界面:
http://hiphotos.baidu.com/ciw_blue/pic/item/3fa5e6d45f25350aa08bb778.jpg
--  作者:qdhqf
--  发布时间:12/13/2007 7:17:00 AM

--  
haha, 通过看别人的代码学的快些,
能不能给我发一份啊
qdhqf@163.com
不胜感激!
--  作者:forever1005
--  发布时间:3/25/2008 10:55:00 AM

--  可以将你的源代码发过来吗
forever_1005@163.com 谢谢 一定带地图编辑器哦
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
2,824.219ms