这是我一个游戏程序中调入数据文件的例程,实际上都是一些记录型的数据。
type
TEnemyAdvent = record
f: Integer; { Frame No }
c: TEnemyType; { Class Type }
x: Integer; { X }
y: Integer; { Y }
end;
var
EnemyAdventTable: array of TEnemyAdvent;
procedure InitializeDataFromFile;
var
i, DCount: integer;
Enemy: TEnemyAdvent;
eFile: file of TEnemyAdvent;
begin
try
AssignFile(eFile, DatFileName);//DatFileName--被调入的数据文件
ReSet(eFile);
{Object Count}
Read(eFile, Enemy);
DCount := Enemy.f;//总记录数
SetLength(EnemyAdventTable, DCount); //设置数据上标
{Data Objects}
for i := 0 to DCount - 1 do
begin
with Enemy do
begin
Read(eFile, Enemy);
EnemyAdventTable.f := f;
EnemyAdventTable.c := c;
EnemyAdventTable.x := x;
EnemyAdventTable.y := y;
end;
end;
{Data of Movement}
with Enemy do
begin
{Display Width/Height}
Read(eFile, Enemy);
FaceBit := iif(FaceBit = 0, f, iif(f <> FaceBit, FaceBit, f));
FaceWidth := iif(FaceWidth = 0, x, iif(x <> FaceWidth, FaceWidth, x));
FaceHeight := iif(FaceHeight = 0, y, iif(y <> FaceHeight, FaceHeight, y));
{PlayerSprite}
Read(eFile, Enemy);
iLPlayer := f;
iAPlayer := x;
iMPlayer := y;
iLife := iLPlayer;
{UFO}
Read(eFile, Enemy);
iLUFO := f;
iAUFO := x;
iMUFO := y;
{Attacker}
Read(eFile, Enemy);
iLAttacker := f;
iAAttacker := x;
iMAttacker := y;
{Boss}
Read(eFile, Enemy);
iLBoss := f;
iABoss := x;
iMBoss := y;
{Main Scene}
Read(eFile, Enemy);
PalleteDelay := f;
BounceSpeed := x;
BGScroll := y;
end;
finally
CloseFile(eFile);
end;
end;
这个过程实质上是把数据文件中的内容加载到一个记录型数据数组EnemyAdventTable中,
在此后任何调用的地方便无须对数据文件进行频繁的读写。