📄 planepanel.pas
字号:
PlaneDraw.LastPlane := True;
end;
function TPlanePanel.DrawAPlaneExactly(Head: TPos; Direction: Byte; Color: TColor): Boolean;
var i: Byte; nowPX, nowPY: smallint;
begin
Result := True;
for i := 0 to 9 do begin
nowPX := Head.X + DPlaneBody[Direction, i, 0];
nowPY := Head.Y + DPlaneBody[Direction, i, 1];
PlaneDraw.TenPoints[i].X := nowPX;
PlaneDraw.TenPoints[i].Y := nowPY;
if (nowPX in [0..GridCou - 1]) and (nowPY in [0..GridCou - 1]) then begin
Grids[nowPY, nowPX].Brush.Color := Color;
Grids[nowPY, nowPX].Pen.Width := 1;
if GridState[nowPY, nowPX].GridType <> 0 then
Result := False;
end else
Result := False;
end;
end;
procedure TPlanePanel.EraseAPlane();
var i: Byte; aX, aY: smallint;
begin
for i := 0 to 9 do begin
aX := PlaneDraw.TenPoints[i].X;
aY := PlaneDraw.TenPoints[i].Y;
if (aX in [0..GridCou - 1]) and (aY in [0..GridCou - 1]) then
with Grids[PlaneDraw.TenPoints[i].Y, PlaneDraw.TenPoints[i].X] do begin
if GridState[aY, aX].GridType = 0 then begin
with Grids[PlaneDraw.TenPoints[i].Y, PlaneDraw.TenPoints[i].X] do
Brush.Color := GridColor;
end else
Brush.Color := FillColor[GridState[aY, aX].HoldPlaneIdx - 1];
end;
end;
end;
procedure TPlanePanel.GridsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
var i: Byte; aX, aY: smallint;
begin
ActiveGridY := (Sender as TComponent).tag div GridCou;
ActiveGridX := (Sender as TComponent).tag mod GridCou;
if Mode = 1 then
if Button = mbRight then begin //取消这个飞机
if PlaneDraw.PlaneHeadSet = True then begin
EraseAPlane;
PlaneDraw.PlaneHeadSet := False;
PlaneDraw.LastPlane := False;
end;
end else if Button = mbLeft then begin
if PlaneDraw.PlaneHeadSet = False then begin //开始画飞机
if GridState[ActiveGridY, ActiveGridX].GridType <> 0 then exit; //不可以在这里画飞机
with PlaneDraw do begin
PlaneHeadSet := True;
thisPlaneAvailable := False;
PlaneHead.X := ActiveGridX;
PlaneHead.Y := ActiveGridY;
TenPoints[0].x := ActiveGridX;
TenPoints[0].y := ActiveGridY;
end;
Grids[ActiveGridY, ActiveGridX].Brush.Color := FillColor[PlaneDraw.NowPlaneCou];
end else begin //确认这个飞机
if PlaneDraw.thisPlaneAvailable = False then begin //不可画飞机
EraseAPlane;
PlaneDraw.PlaneHeadSet := False;
PlaneDraw.LastPlane := False;
PlaneDraw.OldDirection := 0;
exit;
end;
inc(PlaneDraw.NowPlaneCou);
Planes[PlaneDraw.NowPlaneCou].TenPoints := PlaneDraw.TenPoints;
Planes[PlaneDraw.NowPlaneCou].Dirction := PlaneDraw.OldDirection;
for i := 0 to 9 do begin
aX := PlaneDraw.TenPoints[i].X;
aY := PlaneDraw.TenPoints[i].Y;
GridState[aY, aX].GridType := 2;
GridState[aY, aX].HoldPlaneIdx := PlaneDraw.NowPlaneCou;
end;
GridState[PlaneDraw.PlaneHead.y, PlaneDraw.PlaneHead.X].GridType := 1; //Plane Head
PlaneDraw.LastPlane := False;
PlaneDraw.PlaneHeadSet := False;
PlaneDraw.OldDirection := 0;
if PlaneDraw.NowPlaneCou = PlaneDraw.AppointedPlaneCou then begin //Finish drawing plane
Mode := 2;
if Assigned(FOnNewGameStart) then FOnNewGameStart;
end else
if Assigned(FOnOnePlaneDrawed) then FOnOnePlaneDrawed;
end;
end;
if Mode = 3 then begin //Guess Map
if GridState[ActiveGridY, ActiveGridX].Visited = True then begin //已被猜过
if Assigned(OnSameGridGuessed) then OnSameGridGuessed;
end else
SetAGuessed(ActiveGridX, ActiveGridY);
end;
end;
procedure TPlanePanel.ShowPlane(idx: Byte);
var i: Byte;
begin
DrawAPlaneExactly(Planes[idx].TenPoints[0], Planes[idx].Dirction, FillColor[idx - 1]);
for i := 0 to 9 do
with Planes[idx].TenPoints[i] do
GridState[Y, X].Visited := True;
end;
procedure TPlanePanel.SetAGuessed(x, y: Byte);
var tGrid: Byte;
begin
GridState[y, x].Visited := True;
tGrid := GridState[Y, X].GridType;
inc(GuessData.tryCount);
case tGrid of
0: begin //打空了 ,该电脑猜了
GridState[y, x].GridType := 3;
Grids[y, x].Brush.Style := bsDiagCross;
Grids[y, x].Brush.Color := clBlack;
end;
2: begin //打中机身 ,该电脑猜了
GridState[y, x].GridType := 2;
Grids[y, x].Pen.Width := 2;
inc(GuessData.Hit);
end;
1: begin //打中机头,该电脑猜了
GridState[y, x].GridType := 1;
ShowPlane(GridState[y, x].HoldPlaneIdx);
inc(GuessData.Hit);
inc(GuessData.KnockDown);
end;
end;
if Assigned(OnOneGridGuessed) then OnOneGridGuessed(x, y, tGrid, GridState[y, x].HoldPlaneIdx);
if GuessData.KnockDown = PlaneDraw.AppointedPlaneCou then
if Assigned(FOnPlanesAllKnockedDown) then FOnPlanesAllKnockedDown(True);
end;
procedure TPlanePanel.CleanHeadSetButNotDrawnPlane;
begin
if PlaneDraw.PlaneHeadSet = True then begin
EraseAPlane;
PlaneDraw.PlaneHeadSet := False;
PlaneDraw.LastPlane := False;
PlaneDraw.OldDirection := 0;
end;
end;
procedure TPlanePanel.GenerateGame;
var i: Byte;
begin
CleanHeadSetButNotDrawnPlane; //清除已经开始画但还没画好的飞机
GeneratePlanes;
for i := 1 to PlaneDraw.NowPlaneCou do
DrawAPlaneExactly(Planes[i].TenPoints[0], Planes[i].Dirction, FillColor[i - 1]);
Mode := 2;
if Assigned(FOnNewGameStart) then FOnNewGameStart;
end;
procedure TPlanePanel.NewAsGuessMap;
var i, t: Byte;
begin
Mode := 0;
for i := 0 to 19 do
for t := 0 to 19 do begin
with Grids[i, t] do begin
Brush.Color := GridColor;
Brush.Style := bsSolid;
Pen.Width := 1;
end;
with GridState[i, t] do begin
GridType := 0;
HoldPlaneIdx := 0;
Visited := False;
end;
end;
with GuessData do begin
tryCount := 0;
Hit := 0;
KnockDown := 0;
end;
end;
procedure TPlanePanel.NewGame(AutoGenerate: Boolean; _AppointedPlaneCou: Byte; con_Play: Boolean = True; ForOnlineGame: Boolean = False);
var i, t: Byte; var s: TBrushStyle;
begin
with PlaneDraw do begin
AppointedPlaneCou := _AppointedPlaneCou;
NowPlaneCou := 0;
OldDirection := 0;
LastPlane := False;
PlaneHeadSet := False;
end;
if con_Play then s := bsSolid else s := bsDiagCross;
if ForOnlineGame = True then s := bsSolid;
for i := 0 to 19 do
for t := 0 to 19 do begin
with Grids[i, t] do begin
Brush.Color := GridColor;
Brush.Style := s;
Pen.Width := 1;
end;
GridState[i, t].GridType := 0;
end;
FGuessMapGridPanel.NewAsGuessMap;
if ForOnlineGame = True then begin
if AutoGenerate = True then
GenerateGame
else begin
Mode := 1;
if Assigned(FOnHandDrawPlaneBegin) then FOnHandDrawPlaneBegin;
end;
exit;
end;
if AutoGenerate = True then
GenerateGame
else
if con_Play = True then begin
Mode := 1;
if Assigned(FOnHandDrawPlaneBegin) then FOnHandDrawPlaneBegin;
end;
if con_Play = False then
if Assigned(FOnNewGameStart) then FOnNewGameStart;
end;
procedure TPlanePanel.ComputerWin;
begin
if Assigned(FOnPlanesAllKnockedDown) then FOnPlanesAllKnockedDown(False);
end;
procedure TPlanePanel.ComputerHitPlayerPlaneHead;
begin
if Assigned(FComputerHitPlayerPlaneHead) then FComputerHitPlayerPlaneHead;
end;
procedure TPlanePanel.GeneratePlanes;
var i, x, y, Direction: Byte; nowPX, nowPY: Smallint; Result: Boolean;
begin
while PlaneDraw.NowPlaneCou < PlaneDraw.AppointedPlaneCou do begin
Randomize;
x := RandomRange(1, GridCou - 2);
y := RandomRange(1, GridCou - 2);
Direction := RandomRange(1, 4);
Result := True;
for i := 0 to 9 do begin
nowPX := X + DPlaneBody[Direction, i, 0];
nowPY := Y + DPlaneBody[Direction, i, 1];
if CheckPoint(nowPX, nowPY) then begin
with Planes[PlaneDraw.NowPlaneCou + 1] do begin
TenPoints[i].X := nowPX;
TenPoints[i].Y := nowPY;
end;
if GridState[nowPY, nowPX].GridType <> 0 then begin
Result := False; //A Plane is not available
break;
end;
end else begin
Result := False; //A Plane is not available
break;
end;
end;
if Result = False then continue;
Planes[PlaneDraw.NowPlaneCou + 1].Dirction := Direction;
GridState[Y, X].GridType := 1;
GridState[Y, X].HoldPlaneIdx := PlaneDraw.NowPlaneCou + 1;
for i := 1 to 9 do begin
nowPX := X + DPlaneBody[Direction, i, 0];
nowPY := Y + DPlaneBody[Direction, i, 1];
GridState[nowPY, nowPX].GridType := 2;
end;
inc(PlaneDraw.NowPlaneCou);
end;
end;
function CheckPoint(x, y: smallint): Boolean;
begin
if (x in [0.._GridCou - 1]) and (y in [0.._GridCou - 1]) then Result := True else Result := False;
end;
procedure Register;
begin
RegisterComponents('Outer Components', [TPlanePanel]);
end;
initialization
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -