📄 游戏开发中心----游戏实验室 - 博客园.htm
字号:
</FONT><FONT face=宋体 color=#99ccff size=2><BR>
CurrentFrame++; <BR></FONT></FONT><FONT face=宋体></FONT></FONT><FONT
face=Arial><FONT face=宋体 color=#99ccff size=2> if(
CurrrentFrame==3 )<BR>
CurrentFrame = 0; </FONT><FONT face=宋体 color=#808080 size=2>//
这个步骤能够保证动作的循环 </FONT><FONT face=宋体 color=#99ccff size=2><BR>}</FONT><FONT
face=Fixedsys color=#99ccff size=2><BR></FONT><FONT face=Arial><FONT
face=Arial size=2><BR> 不知道这么个函数你能否看懂。之所以要采用 static
静态变量,是因为我们这个函数程序要循环运行。如果直接写个 int 的话,每次执行都会被赋回原值 0,那么图片就不会变了。
<BR><BR> 上面这段代码其实并不是那么理想。因为程序自己在那儿一个劲地运行,完全没有我们控制的份儿。别急!来看下面这份修改过的伪代码:
<BR><BR></FONT></FONT><FONT face=宋体 color=#99ccff size=2>#define
m_Width 32 </FONT><FONT face=宋体 color=#808080
size=2>// 每个动作的 宽度 </FONT><FONT face=宋体 color=#99ccff size=2><BR>#define
m_Height 48 </FONT><FONT face=宋体 color=#808080 size=2>//
每个动作的 高度 </FONT><FONT face=宋体 size=2><BR><BR></FONT><FONT face=宋体
color=#99ccff size=2>void ShowThePic() <BR>{<BR> static
RECT rect; </FONT><FONT face=宋体 color=#808080
size=2>// 矩形对象,用于精确定位所要的当前动作 </FONT><FONT face=宋体 color=#99ccff
size=2><BR> static int CurrentFrame = 0; </FONT><FONT
face=宋体 color=#808080 size=2>// 当前动作的编号 </FONT><FONT face=宋体 color=#99ccff
size=2><BR> static int Direction = 0;
</FONT><FONT face=宋体 color=#808080 size=2>// 当前的方向 </FONT><FONT face=宋体
color=#99ccff size=2><BR> rect.top = Direction;
rect.bottom = (Direction+1)*m_Height; </FONT><FONT face=宋体 color=#808080
size=2>//对当前矩形的大小定义,数学的问题哦 </FONT><FONT face=宋体 color=#99ccff
size=2><BR> rect.left = CurrentFrame; rect.right =
(CurrentFrame+1)*m_Width; </FONT><FONT face=宋体 color=#808080
size=2>//根据英文的意思也可以知道在给谁赋值 </FONT><FONT face=宋体 color=#99ccff
size=2><BR> BltPicToScreen();
</FONT><FONT face=宋体 color=#808080 size=2>// 一个伪函数,作用是将当前矩形内的图形复制到屏幕上。
</FONT><FONT face=宋体 color=#99ccff size=2><BR><BR> if(
LeftArrowDown() ) </FONT><FONT face=宋体 color=#808080 size=2>// 如果 左箭头 被按下
</FONT><FONT face=宋体 color=#99ccff size=2><BR> {
<BR> Direction =
1; </FONT><FONT face=宋体 color=#808080 size=2>// 赋值方向为 1
</FONT><FONT face=宋体 color=#99ccff
size=2><BR> CurrentFrame++;
<BR> } <BR><BR> if( RightArrowDown() )
<BR> { <BR>
Direction = 3; <BR>
CurrentFrame++; <BR> } <BR><BR> if(
UpArrowDown() ) <BR> {
<BR> Direction = 2;
<BR> CurrentFrame++;
<BR> } <BR><BR> if( DownArrowDown() )
<BR> { <BR>
Direction = 0; <BR>
CurrentFrame++; <BR> } <BR><BR>
if(CurrrentFrame==3)<BR>
CurrentFrame = 0; </FONT><FONT face=宋体 color=#808080 size=2>//
这个步骤能够保证动作的循环 </FONT><FONT face=宋体 color=#99ccff size=2><BR>} </FONT><FONT
face=Arial
size=2><BR><BR> 经过这么一修改,问题再次得到解决。上面的代码就能够响应用户的操作了。当然,你这么直接在程序里输入这些代码是不行的:)因为是“伪代码”。你得根据实际情况,自己相应地做些修改,才能使程序运行!
<BR><BR> (题外话:不晓得这么进行教学,你是否能够一点一点的积累到知识?这种教程是不是合适?请到论坛内发表意见,我真的很想能够摸索出大家接受的方法,有利教学)
<BR><BR> 欢迎回到教程中来!有人或许会问,为什么这么麻烦要把图形放在这么个图片里头,不一个动作一个图啊?呵呵,想想,那要多少图片啊,很难于管理的。况且,这样并不能避免使用这种常用手法,因为
RECT 已经是一个成员。游戏里不能不用他的! <BR><BR> 哇,口水干了。(其实是手累了:)我们下回再见吧。 <BR><BR> 下回预告:
<BR> 少年侦探柯南为了查清楚事情的真相,他......(?还真的预告啊?)
<BR> 不好意思,习惯性用语了:)在下回,我会讲一下,如何将今天的代码整合到程序中去。<BR><BR><BR><BR></FONT><B><A
name="三、游戏的基石: 窗口 的建立">三、游戏的基石: 窗口 的建立</A></B><FONT
size=2><BR><BR> Welcome back!很高兴再次与你相遇教程。废话不多说,继续。
<BR><BR> 上次的代码片段,说实在,拿在手里没有用。为啥?因为不能运行的啊:)我们这次就按照上次的安排,讲如何将显示图片的代码片段整合到程序中,让它能够具体的体现出来。
<BR><BR> 我将在这儿讨论 Windows 编程,而不是 MFC 。关于 MFC 和 Windows 的不同和各自的优点,我们不多涉及了。
MFC 就是微软的一个封装开发库,极大程度的降低了 Windows 开发的复杂。但是,我们还是继续 Windows 编程,呵呵。
<BR><BR> 我们来看,一个基本的 Windows 程序是一个窗口对吧:)我们所玩的游戏,其实也是有窗口的,只不过是看不见的,而且被
DirectDraw 掩盖了而已。那么,如何创建一个窗口呢?我们来看: <BR><BR></FONT><FONT face=宋体
color=#99ccff size=2>BOOL Init(HINSTANCE hInst, int nCmdShow)
<BR>{<BR> HWND
hWnd;
</FONT><FONT face=宋体 color=#808080 size=2>// 窗口的句柄,就是一个储存窗口的对象
</FONT><FONT face=宋体 color=#99ccff size=2><BR> WNDCLASS
WndClass;
</FONT><FONT face=宋体 color=#808080 size=2>// 用于注册窗口的对象</FONT><FONT face=宋体
color=#99ccff size=2> <BR><BR> WndClass.style =
CS_HREDRAW | CS_VREDRAW; </FONT><FONT face=宋体 color=#808080 size=2>//
定义窗口的类型 </FONT><FONT face=宋体 color=#99ccff size=2><BR>
WndClass.lpfnWndProc = WinProc; </FONT><FONT
face=宋体 color=#808080 size=2>// 指定了窗口消息的处理函数 ** 关键!</FONT><FONT face=宋体
color=#99ccff size=2> <BR> WndClass.cbClsExtra =
0;
</FONT><FONT face=宋体 color=#808080 size=2>// 没有特定意义 </FONT><FONT face=宋体
color=#99ccff size=2><BR> WndClass.cbWndExtra =
0;
</FONT><FONT face=宋体 color=#808080 size=2>// 没有特定意义 </FONT><FONT face=宋体
color=#99ccff size=2><BR> WndClass.hInstance =
hInst; </FONT><FONT
face=宋体 color=#808080 size=2>// 窗口的实例 </FONT><FONT face=宋体 color=#99ccff
size=2><BR> WndClass.hIcon = LoadIcon(hInst,
IDI_APPLICATION); </FONT><FONT face=宋体 color=#808080 size=2>//
指定窗口的图标</FONT><FONT face=宋体 color=#99ccff size=2> <BR>
WndClass.hCursor = LoadCursor(hInst, IDC_ARROW); </FONT><FONT
face=宋体 color=#808080 size=2>// 指定程序的指针 </FONT><FONT face=宋体 color=#99ccff
size=2><BR> WndClass.hbrBackground =
(HBRUSH)GetStockObject(BLACK_BRUSH); </FONT><FONT face=宋体 color=#808080
size=2>// 指定背景色为黑色</FONT><FONT face=宋体 color=#99ccff size=2>
<BR> WndClass.lpszMenuName =
NULL; </FONT><FONT face=宋体
color=#808080 size=2>// 指定菜单为无,游戏不需要 </FONT><FONT face=宋体 color=#99ccff
size=2><BR> WndClass.lpszClassName = "GDIM_GAME_ENGINE";
</FONT><FONT face=宋体 color=#808080 size=2>//这个程序注册的名字</FONT><FONT face=宋体
color=#99ccff size=2> <BR> </FONT><FONT size=2><FONT
face=宋体
color=#99ccff>RegisterClass(&WndClass);
</FONT><FONT face=宋体 color=#808080>// 注册程序 </FONT><FONT face=宋体
color=#99ccff><BR><BR> hWnd =
CreateWindowEx(WS_EX_TOPMOST, "GDIM_GAME_ENGINE", "GDIM_GAME", WS_POPUP,
112, 84, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInst, NULL);
<BR> </FONT><FONT face=宋体 color=#808080>//
这段代码就是实现了一个窗口的创建 <BR></FONT><FONT face=宋体 color=#99ccff>
</FONT><FONT face=宋体 color=#808080>// 我们可以通过来检测是否创建成功。如果失败将返回一个 FALSE 的值。
</FONT><FONT face=宋体 color=#99ccff><BR> if(!hWnd) return
FALSE; <BR> ShowWindow(hWnd, nCmdShow);
</FONT><FONT face=宋体 color=#808080>// 显示窗口</FONT><FONT face=宋体
color=#99ccff> <BR>
UpdateWindow(hWnd);
</FONT><FONT face=宋体 color=#808080>// 更新窗口 </FONT><FONT face=宋体
color=#99ccff><BR>}
</FONT><BR><BR> 好了,到这儿呢,我们就成功的创建了一个窗口。我们回头看我注了“**”的地方,关于这个函数,我们得写个同名函数来处理
Windows 的消息。呵呵,不好意思,再看一个函数: <BR><BR></FONT></FONT><FONT size=2><FONT
face=宋体 color=#99ccff>BOOL bActive =
FALSE; </FONT><FONT face=宋体
color=#808080>// 用于判断程序是否运行的变量 </FONT><FONT face=宋体
color=#99ccff><BR><BR>long PASCAL WinProc(HWND hWnd, UINT message, WPARAM
wParam, LPARAM lParam)<BR>{ <BR> switch(message)
<BR> { <BR> case WM_ACTIVATE:
<BR> bActive =
TRUE; </FONT><FONT face=宋体 color=#808080>//
当程序被激活时,赋值 TRUE </FONT><FONT face=宋体
color=#99ccff><BR> break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -