VAE范例Apple制作过程精.docx
- 文档编号:17615219
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:23
- 大小:32.09KB
VAE范例Apple制作过程精.docx
《VAE范例Apple制作过程精.docx》由会员分享,可在线阅读,更多相关《VAE范例Apple制作过程精.docx(23页珍藏版)》请在冰点文库上搜索。
VAE范例Apple制作过程精
VAE范例:
Apple制作过程
我的主页介绍VAE的文章终于写成几篇了,我便忍不住要开发一个完整的小游戏并写一篇文章来展示展示。
所以,这个Apple就新鲜出炉了。
关于Apple的代码请见。
谢谢。
事先声明一下:
图片取自金山打字通2003的“拯救苹果”。
如果金山公司觉得这侵犯了金山公司的利益的话,
请告诉我,我会另换图片。
开始制作了!
事先作一下准备。
说是VAE的范例,当然是先下载VAE,然后把vaeDataFile.pas和vaeBmex.pas扔进去。
在准备两张图片,一张是Back.bmp,做背景;另一张是Apple.bmp,当然是苹果啦。
我依稀记得在几千年前开发Apple的时候,突然心血来潮,使用了Class来编程。
所以,大家一定要再建立一个Display.pas来放TApple。
哈哈哈……
上面五个文件都放在同一个文件夹里,然后再文件夹里建立一个新的工程叫Apple.exe,现在正式开工。
当把vae的两个文件加入工程并且新建Display.pas后,Apple.dpr肯定会变成这样的:
programApple;
uses
Forms,
untApplein'untApple.pas'{frmApple},
Displayin'Display.pas',
vaeBmexin'vaeBmex.pas',
vaeDataFilein'vaeDataFile.pas';
{$R*.res}
begin
Application.Initialize;
Application.CreateForm(TfrmApple,frmApple);
Application.Run;
end.
如果不是这样的话,那就要赶快改一改了。
接着,打开Display.pas,开始编写TApple的代码。
先在interface后加入代码如下
unitDisplay;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,vaeBmex;
TApple的功能是控制Apple.exe的行为和显示方式。
在新的游戏开始后,苹果们会掉下来,并且每个苹果上面印有一个字母。
只有按下相应的字母键,该苹果才会消去。
如果不行让10个苹果掉了下来,游戏就结束了。
所以,玩Apple一定会以失败告终的。
因此,Apple提供了一个最高分记录,所以,就当超过最高分为胜利吧。
哈哈哈……
Apple.exe一共有五个版面。
1:
Logo
2:
开始画面
3:
游戏画面
4:
结束画面
5:
难度选择画面
我偷懒了一下,把第1和第4合并了起来。
因此,建立五个常数:
const
MaxLeft=10;//掉了10个苹果就输了。
R_Static=0;//开始画面
R_Running=1;//游戏画面
R_ShowScore=2;//Logo;结束画面
R_Difficulty=4;//难度选择画面
然后,在implementation上面写一个空的TApple框架:
type
TApple=class(TObject)
private
protected
published
public
end;
我们需要3个TBmex,一个用来放置背景图片,一个用来放置苹果图片,还有一个做为游戏画面的缓冲区。
于是,我就在private里加入了三个变量:
FBack:
TBmex;FApple:
TBmex;FBuffer:
TBmex;。
在游戏画面的上方有一段操作说明文字。
这段文字是要程序来设定,而不是TApple来设定的,所以,我又加入了一个FCaption:
String;并在public加入了propertyCaption:
StringreadFCaptionwriteFCaption;以便让程序可以修改这段说明文字。
程序必须知道当前状态(与显示画面差不多),才能正确的控制TApple。
所以,我又加入了FRunning:
Integer;并在public加入了propertyRunning:
IntegerreadFRunning;以便让程序知道当前状态,并且防止程序改变当前状态使得TApple被破坏。
游戏在运行的时候,除了苹果的位置以外,还依赖于三个参数:
1:
剩余量。
来记录在掉几个苹果游戏就结束了。
2:
当前分数。
3:
苹果生产速度。
因此,又有了:
FLeft:
Integer;
FScore:
Integer;
FAppleBuilder:
Integer;
与
propertyLeft:
IntegerreadFLeft;
propertyScore:
IntegerreadFScore;
propertyAppleBuilder:
IntegerreadFAppleBuilderwriteFAppleBuilder;
在游戏中,我们需要关于苹果的一些信息。
有字母,位置,有没有显示这三个量,因此,我就果断地在TApple与type中间加入了:
TAppleImp=record
X,Y:
Integer;
Speed:
Integer;
Used:
Boolean;
end;
并在private里写道:
Apples:
array[65..90]ofTAppleImp;
FAppleBuilder是时间间隔,所以,要有一个变量来记录时间,于是TimeAB:
Integer;就光荣地诞生了。
Apple.exe为了吸引人们去玩,于是有了一个最高分的概念,所以,我挥一挥手,就不知道从哪里来了一个FHighestScore:
Integer;。
在结束画面的时候,如果最高分被打破的话,就要先是一句话,所以,我就顺手拿了一个FMadeHighest:
Boolean;,并且在显示之后会自动变为False。
当最高分被打破的时候,Apple就要在硬盘上记录分数。
于是,TApple就要求程序在CreateTApple时输入分数文件。
于是,为了记录分数文件的文件名,FScoreFile:
String;与propertyShowScoreCaption:
BooleanreadFShowScoreCaptionwriteFShowScoreCaption;便呱呱落地了。
我上面写道,我把Logo与结束画面合起来。
为了区别,就有了一个变量FShowScoreCaption:
Boolean;。
为True时就是Logo。
现在,大家所看到的代码必须是:
unitDisplay;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,vaeBmex;
const
MaxLeft=10;
R_Static=0;
R_Running=1;
R_ShowScore=2;
R_Difficulty=4;
type
TAppleImp=record
X,Y:
Integer;
Speed:
Integer;
Used:
Boolean;
end;
TApple=class(TObject)
private
FBack:
TBmex;
FApple:
TBmex;
FBuffer:
TBmex;
FCaption:
String;
FRunning:
Integer;
FLeft:
Integer;
FScore:
Integer;
FAppleBuilder:
Integer;
FShowScoreCaption:
Boolean;
FScoreFile:
String;
FHighestScore:
Integer;
FMadeHighest:
Boolean;
Apples:
array[65..90]ofTAppleImp;
TimeAB:
Integer;
protected
published
public
propertyCaption:
StringreadFCaptionwriteFCaption;
propertyRunning:
IntegerreadFRunning;
propertyLeft:
IntegerreadFLeft;
propertyScore:
IntegerreadFScore;
propertyAppleBuilder:
IntegerreadFAppleBuilderwriteFAppleBuilder;
propertyShowScoreCaption:
BooleanreadFShowScoreCaptionwriteFShowScoreCaption;
end;
implementation
end.
否则的话,赶快改!
!
!
我依稀记得在写完这段声明之后,一大堆富有建设性的代码就跟着涌了出来。
TApple的第一件事就是Create与Free。
于是,我先机械地在published里写道:
constructorCreate(ScoreFile:
String);
procedureFree;
当然,接着就是那些富有建设性的代码了。
constructorTApple.Create(ScoreFile:
String);
varSL:
TStringList;//用来打开分数文件
begin
//建立TBmex
FBack:
=TBmex.Create;
FApple:
=TBmex.Create;
FBuffer:
=TBmex.Create;
FBuffer.Canvas.Font.Name:
='ArialBlack';
//接下来的这一行代码是没有意义的。
我只是忘了删掉而已,顺便保持代码与文章的一致性。
FRunning:
=R_Static;
//设置一些东西
FShowScoreCaption:
=False;
FMadeHighest:
=False;
FScoreFile:
=ScoreFile;
//打开分数文件
SL:
=TStringList.Create;
SL.LoadFromFile(FScoreFile);
FHighestScore:
=StrToInt(SL[0]);
SL.Free;
end;
Free的代码我就不介绍了。
一定是那些东西。
procedureTApple.Free;
begin
FBack.Free;
FApple.Free;
FBuffer.Free;
inheritedFree;//如果你不怕内存泄漏的话,这句代码你可以考虑删掉。
end;
接下来,在private里写一些工具代码。
1:
把背景图复制到缓冲区。
procedureTApple.DrawBack;
begin
FBuffer.Draw(FBack,0,0);
end;
2:
打印字符串。
字符串是白色的,外面还包围着一圈黑色。
怎么办呢?
procedureTApple.DrawText(X,Y:
Integer;Caption:
String);
varC,R:
Integer;
begin
FBuffer.TextOut(X,Y,Caption,clWhite,clBlack);
end;
有了VAE就是不一样。
哈哈哈……
3:
画苹果。
现在不知要复制图片,还要画一个字母:
procedureTApple.DrawApple(X,Y:
Integer;C:
Char);
varW,H:
Integer;
begin
DrawTrans(FBuffer,FApple,X-FApple.Widthdiv2,Y-FApple.Heightdiv2);
FBuffer.Canvas.Brush.Style:
=bsClear;
FBuffer.Canvas.Font.Size:
=24;
FBuffer.Canvas.Font.Color:
=clWhite;
W:
=FBuffer.Canvas.TextWidth(C);
H:
=FBuffer.Canvas.TextHeight(C);
FBuffer.Canvas.TextOut(X-Wdiv2,Y-Hdiv2,C);
end;
X,Y代表的是中心位置。
为了使字符被画在中间,字符的位置自然就是(X-Wdiv2,Y-Hdiv2)了。
4:
画透明图像。
procedureTApple.DrawTrans(Dst,Src:
TBmex;Left,Top:
Integer);
begin
Dst.DrawTrans(Src,0,0,Src.Width,Src.Height,Left,Top,0);
end;
DrawApple就有用到这个。
5:
获得空闲的苹果。
functionTApple.GetUnusedApple:
Integer;
varI,C:
Integer;
begin
C:
=0;
forI:
=65to90do
ifApples[I].Usedthen
C:
=C+1;
ifC=26then
result:
=-1
else
begin
repeat
I:
=Random(26)+65;
untilnotApples[I].Used;
result:
=I;
end;
end;
苹果的字符信息被苹果在数组里的位置隐含了。
如果所有的苹果都显示了的话,就返回-1。
否则,随机返回一个还没被显示的苹果。
以后,就可以在public里写代码了。
TApple并不知道程序的窗体的大小,因此,就有了一个SetSize,用于传入窗体的大小,并可以用来设置FBuffer的大小:
procedureTApple.SetSize(Width,Height:
Integer);
begin
FBuffer.Width:
=Width;
FBuffer.Height:
=Height;
end;
接着,程序必须向TApple传入两张图片文件,才能将结果正确地显示出来:
procedureTApple.SetBack(FileName:
String);
begin
FBack.LoadFromFile(FileName);
end;
procedureTApple.SetApple(FileName:
String);
begin
FApple.LoadFromFile(FileName);
end;
接下来,写一个把FBuffer画到Form上面的代码:
procedureTApple.PaintToForm(Form:
TForm);
begin
FBuffer.DrawToCanvas(Form.Canvas,0,0);
end;
接着,写几段打开画面的代码。
1:
Logo/结束画面:
procedureTApple.ShowScore;
begin
FRunning:
=R_ShowScore;
Draw;
end;
Draw例程以后再来。
如果想显示Logo的话,就先把ShowScoreCaption设置为True,然后再调用ShowScore。
2:
难度选择画面:
procedureTApple.ShowDifficulty;
begin
FRunning:
=R_Difficulty;
Draw;
end;
3:
开始画面:
procedureTApple.Reset;
begin
FRunning:
=R_Static;
end;
4:
结束,回到开始画面:
procedureTApple.StopGame;
varI:
Integer;
begin
forI:
=65to90do
Apples[I].Used:
=False;
FRunning:
=R_Static;
end;
先把所有的苹果都隐藏了之后再设置FRunning。
5:
游戏画面:
procedureTApple.NewGame;
varI:
Integer;
begin
//初始化随机数发生器
Randomize;
//隐藏所有苹果
forI:
=65to90do
Apples[I].Used:
=False;
//当前时间设置为0
TimeAB:
=0;
//打开游戏画面
FRunning:
=R_Running;
//剩余总数设置为MaxLeft
FLeft:
=MaxLeft;
//当前分数设置为0
FScore:
=0;
end;
TApple的最后三段代码,就是富有建设性的代码的核心部分,来了!
先写击键后消除苹果的代码:
procedureTApple.HitKey(Key:
Word);
begin
//如果按键代表的苹果存在的话
ifApples[key].Usedthen
begin
//增加分数
FScore:
=FScore+1;
//隐藏被击中的苹果
Apples[Key].Used:
=False;
end;
end;
用位置代替字符的好处就在这里,在检测的时候不需要用循环。
然后写游戏过程的代码。
这个有点长:
procedureTApple.Run;
varNewApple:
Integer;
I:
Integer;
SL:
TStringList;
begin
ifTimeAB=0then//如果当前时间是0的话,那么,就必须生产一个苹果。
begin
//获得空闲的苹果
NewApple:
=GetUnusedApple;
//如果返回的是 –1,那么,代表所有的苹果都被显示了,不能生产苹果。
ifNewApple>-1then
begin
//接触苹果的隐藏
Apples[NewApple].Used:
=True;
//设定苹果的高度
Apples[NewApple].Y:
=-FApple.Heightdiv2;
//设定苹果的速度
Apples[NewApple].Speed:
=Random(3)+1;
//设定苹果的位置
Apples[NewApple].X:
=Random(FBuffer.Width-FApple.Width)+FApple.Widthdiv2;
end;
end;
TimeAB:
=TimeAB+1;
//当前时间超过FAppleBuilder的话,就要重置为0,否则,将永远没有苹果产生。
ifTimeAB=FAppleBuilderthen
TimeAB:
=0;
//移动苹果
forI:
=65to90do
ifApples[I].Usedthen
Apples[I].Y:
=Apples[I].Y+Apples[I].Speed;
//如果苹果掉地上了,就要扣FLeft了。
forI:
=65to90do
ifApples[I].Usedthen
ifApples[I].Y>=FBuffer.Height-FApple.Heightdiv2then
begin
//隐藏苹果
Apples[I].Used:
=False;
FLeft:
=FLeft-1;
end;
//如果FLeft被扣完了的话
ifFLeft=0then
begin
//打开结束画面
FRunning:
=R_ShowScore;
//如果分数打破了纪录
ifFScore>FHighestScorethen
begin
//显示相应的语句
FMadeHighest:
=True;
FHighestScore:
=FScore;
//更改分数文件的内容
SL:
=TStringList.Create;
SL.Text:
=IntToStr(FScore);
SL.SaveToFile(FScoreFile);
SL.Free;
end;
end;
//刷新
Draw;
end;
最后,就是显示代码了:
procedureTApple.Draw;
varI:
Integer;
T:
String;
begin
DrawBack;
caseFRunningof
R_ShowScore:
//显示一些字符串。
begin
FBuffer.Canvas.Font.Size:
=12;
FBuffer.Canvas.Brush.Style:
=bsClear;
ifFShowScoreCaptionthen
begin
T:
='WelcometotheApple!
';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,130,T);
T:
='Vczh''sfirstcartoon-stylegame.';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,150,T);
T:
='Developer:
Vczh';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,170,T);
T:
='WebSite:
';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,190,T);
T:
='PicturesfromKingSoft';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,210,T);
T:
='Pressanykeytostart.';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,230,T);
end
else
begin
T:
='Youarelost!
Yourscoreis'+IntToStr(FScore)+'.';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,170,T);
ifFMadeHighestthen
begin
T:
='Youmadethehighestscore!
';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div2,190,T);
FMadeHighest:
=False;
end;
end;
exit;
end;
R_Difficulty:
begin
DrawText
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VAE 范例 Apple 制作 过程