📄 untscore.pas
字号:
{
接龙游戏分数显示窗口
黄文林 2006-02-12
}
unit untScore;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Cards, ExtCtrls;
type
TfrmScore = class(TForm)
btnOK: TButton;
imgEnd: TImage;
imgScore: TImage;
procedure btnOKClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
FNum: Integer; //得到分数次数(小局数)
FEnd: Boolean; //一局结束标志
FMidPlace: array[0..3] of Integer; //每玩家分数的中间对齐点
FScoreColor: array[0..3] of TColor; //当前分数颜色
procedure DrawScorce(AScore: TPlayerScore); //画最新分数
procedure DrawName; //画名字
public
{ Public declarations }
property Num: Integer read FNum;
procedure ClearScore; //清除分数
procedure SetScore(AScore: TPlayerScore); //得到分数
end;
var
frmScore: TfrmScore;
implementation
uses
untMain;
{$R *.dfm}
const
MinHeight = 120; //窗口客户区最小高度
MaxScore = 300; //最大分数线(一局结束分数)
NameTop = 3; //名字top
ScoreTop = 20; //首行分数top
ScoreRowHeight = 15; //分数行高
procedure TfrmScore.DrawName;
var
i, x, y, w: Integer;
s: String;
begin
//清除原名字
imgScore.Canvas.Rectangle(0, 0, imgScore.Width, ScoreTop - 1);
//画名字
for i := 0 to 3 do
begin
s := frmMain.CardDraw.GetPlayerName(i);
w := imgScore.Canvas.TextWidth(s);
x := FMidPlace[i] - w div 2;
y := NameTop;
imgScore.Canvas.Font.Color := clBlack;
imgScore.Canvas.TextOut(x, y, s);
end;
end;
procedure TfrmScore.DrawScorce(AScore: TPlayerScore);
var
i, x, y, w: Integer;
s: String;
begin
//画最新分数
for i := 0 to 3 do
begin
s := IntToStr(AScore[i]);
w := imgScore.Canvas.TextWidth(s);
x := FMidPlace[i] - w div 2;
y := ScoreTop + ScoreRowHeight * (FNum - 1);
imgScore.Canvas.Font.Color := FScoreColor[i];
imgScore.Canvas.TextOut(x, y, s);
end;
end;
procedure TfrmScore.ClearScore;
begin
//清除分数区域
imgScore.Canvas.Rectangle(0, 0, imgScore.Width, imgScore.Height);
//初始化其它
Caption := '分数';
FNum := 0;
FEnd := False;
imgEnd.Visible := False;
ClientHeight := MinHeight;
DrawName;
end;
procedure TfrmSCore.SetScore(AScore: TPlayerScore);
var
i, j, k: Integer;
s: string;
begin
Inc(FNum);
//计算显示颜色
j := 0;
k := 0;
for i := 1 to 3 do
begin
if AScore[i] < AScore[j] then j := i;
if AScore[i] > AScore[k] then k := i;
end;
for i := 0 to 3 do
if AScore[i] = AScore[j] then
FScoreColor[i] := clBlue
else if (AScore[i] = AScore[k]) or (AScore[i] >= MaxScore) then
FScoreColor[i] := clRed
else
FScoreColor[i] := clBlack;
//画名字和最新分数
DrawName;
DrawScorce(AScore);
//更新窗口标题,加上名次
j := 0;
for i := 1 to 3 do
if AScore[i] < AScore[0] then Inc(j);
case j of
0: s := '冠军';
1: s := '亚军';
2: s := '季军';
3: s := '垫底';
end;
Caption := '分数' + ' - ' + s;
//更新窗口高度
i := ScoreTop + FNum * ScoreRowHeight + 10;
if ClientHeight < i then
ClientHeight := i;
//检查一局游戏是否结束
for i := 0 to 3 do
if AScore[i] >= MaxScore then
begin
FEnd := True;
imgEnd.Visible := True;
end;
end;
procedure TfrmScore.btnOKClick(Sender: TObject);
begin
if FEnd then //如果一局结束, 清除分数
ClearScore;
Close;
end;
procedure TfrmScore.FormCreate(Sender: TObject);
var
i: Integer;
begin
imgEnd.Picture.Icon := Application.Icon; //取工程图标
imgScore.Canvas.Pen.Color := Color;
imgScore.Canvas.Brush.Color := Color;
for i := 0 to 3 do //初始每玩家分数的中间对齐点
FMidPlace[i] := 15 + ((imgScore.Width - 30) div 4) * i + (imgScore.Width - 30) div 8;
ClearScore;
end;
procedure TfrmScore.FormShow(Sender: TObject);
begin
//置窗口为主窗口中间
Top := frmMain.Top + (frmMain.Height - Height) div 2 ;
Left := frmMain.Left + (frmMain.Width - Width) div 2;
DrawName;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -