4:结束,回到开始画面: procedure TApple.StopGame; var I:Integer; begin
for I:=65 to 90 do Apples[I].Used:=False; FRunning:=R_Static;
end;
先把所有的苹果都隐藏了之后再设置FRunning。 5:游戏画面:
procedure TApple.NewGame; var I:Integer;
begin
//初始化随机数发生器 Randomize; //隐藏所有苹果 for I:=65 to 90 do
Apples[I].Used:=False; //当前时间设置为0 TimeAB:=0; //打开游戏画面
FRunning:=R_Running; //剩余总数设置为MaxLeft FLeft:=MaxLeft; //当前分数设置为0
FScore:=0; end;
TApple的最后三段代码,就是富有建设性的代码的核心部分,来了! 先写击键后消除苹果的代码: procedure TApple.HitKey(Key:Word); begin
//如果按键代表的苹果存在的话 if Apples[key].Used then begin
//增加分数
FScore:=FScore+1; //隐藏被击中的苹果 Apples[Key].Used:=False; end;
end;
用位置代替字符的好处就在这里,在检测的时候不需要用循环。 然后写游戏过程的代码。这个有点长: procedure TApple.Run; var NewApple:Integer;
I:Integer;
SL:TStringList; begin
if TimeAB=0 then//如果当前时间是0的话,那么,就必须生产一个苹果。 begin
//获得空闲的苹果
NewApple:=GetUnusedApple;
//如果返回的是 –1,那么,代表所有的苹果都被显示了,不能生产苹果。 if NewApple>-1 then begin
//接触苹果的隐藏
Apples[NewApple].Used:=True; //设定苹果的高度
Apples[NewApple].Y:=-FApple.Height div 2; //设定苹果的速度
Apples[NewApple].Speed:=Random(3)+1; //设定苹果的位置
Apples[NewApple].X:=Random(FBuffer.Width-FApple.Width)+FApple.Width div 2; end; end;
TimeAB:=TimeAB+1;
//当前时间超过FAppleBuilder的话,就要重置为0,否则,将永远没有苹果产生。 if TimeAB=FAppleBuilder then TimeAB:=0; //移动苹果
for I:=65 to 90 do
if Apples[I].Used then
Apples[I].Y:=Apples[I].Y+Apples[I].Speed; //如果苹果掉地上了,就要扣FLeft了。 for I:=65 to 90 do
if Apples[I].Used then
if Apples[I].Y>=FBuffer.Height-FApple.Height div 2 then begin
//隐藏苹果
Apples[I].Used:=False; FLeft:=FLeft-1; end;
//如果FLeft被扣完了的话 if FLeft=0 then begin
//打开结束画面
FRunning:=R_ShowScore; //如果分数打破了纪录 if FScore>FHighestScore then
begin
//显示相应的语句 FMadeHighest:=True; FHighestScore:=FScore; //更改分数文件的内容 SL:=TStringList.Create; SL.Text:=IntToStr(FScore); SL.SaveToFile(FScoreFile); SL.Free; end; end; //刷新
Draw; end;
最后,就是显示代码了: procedure TApple.Draw; var I:Integer; T:String; begin DrawBack;
case FRunning of
R_ShowScore://显示一些字符串。 begin
FBuffer.Canvas.Font.Size:=12;
FBuffer.Canvas.Brush.Style:=bsClear; if FShowScoreCaption then begin
T:='Welcome to the Apple!';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,130,T); T:='Vczh''s first cartoon-style game.';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,150,T); T:='Developer:Vczh';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,170,T); T:='Web Site:http://vczh.cstc.net.cn';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,190,T); T:='Pictures from KingSoft';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,210,T); T:='Press any key to start.';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,230,T); end else
begin
T:='You are lost!Your score is '+IntToStr(FScore)+'.';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,170,T);
if FMadeHighest then
begin
T:='You made the highest score!';
DrawText((FBuffer.Width-FBuffer.Canvas.TextWidth(T))div 2,190,T); FMadeHighest:=False; end; end; exit; end; R_Difficulty:
begin
DrawText(200,150,'1:Easy'); DrawText(200,170,'2:Normal'); DrawText(200,190,'3:Hard'); DrawText(200,210,'4:Impossible'); DrawText(200,230,'F3:Back'); end; else
begin//游戏画面或者开始画面 //显示没有被隐藏的苹果 for I:=65 to 90 do
if Apples[I].Used then
DrawApple(Apples[I].X,Apples[I].Y,Chr(I)); FBuffer.Canvas.Font.Size:=12;
FBuffer.Canvas.Brush.Style:=bsClear; //显示标题
DrawText(2,2,FCaption); //显示最高分
DrawText(2,22,'Higest Score:'+IntToStr(FHighestScore)); //如果游戏正在进行的话
if FRunning=R_Running then begin
//显示剩余量和分数
DrawText(2,42,'Left:'+IntToStr(FLeft)); DrawText(2,62,'Score:'+IntToStr(FScore)); end; end;
end; end;
TApple大功告成!
不过,不要高兴得太早,程序部分还没有写。
程序部分倒是简单,只要响应键盘就行了。 先来几个变量:
Apple:TApple;//辛辛苦苦建立起来的TApple如果没有使用的话,那就是浪费了。
AppPath:String;//程序所在的位置,学了VB6的App.Path。 OnCreate事件
procedure TfrmApple.FormCreate(Sender: TObject); begin
//获得程序的位置
AppPath:=ExtractFilePath(ParamStr(0)); //新建TApple并设置
Apple:=TApple.Create(AppPath+'Score.txt'); Apple.SetSize(ClientWidth,ClientHeight); Apple.SetBack(AppPath+'Back.bmp');
Apple.SetApple(AppPath+'Apple.bmp');
Apple.Caption:='F2:New Game; F3:Stop; F4:Pause; Esc:Exit.'; //使窗体居中
Left:=(Screen.Width-Width)div 2;
Top:=(Screen.Height-Height)div 2; //用ShowScore显示Logo画面 Apple.ShowScoreCaption:=True; Apple.ShowScore; Apple.Draw; end;
OnDestory事件
procedure TfrmApple.FormDestroy(Sender: TObject); begin
Apple.Free; end;
OnPaint事件
procedure TfrmApple.FormPaint(Sender: TObject); begin
Apple.PaintToForm(self); end;
OnKeyUp事件
procedure TfrmApple.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin
if Apple.Running=R_ShowScore then begin
//如果显示Logo或分数的话,就回到开始画面 Apple.ShowScoreCaption:=False; Apple.StopGame; Apple.Draw; FormPaint(self); end else
case key of
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库游戏编程文章搜集资料(8)在线全文阅读。
相关推荐: