给你一个思路:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
btnRestart: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure btnRestartClick(Sender: TObject);
private
FBackBmp: TBitmap; //保存棋盘;
FBlackBmp: TBitmap; //保存黑子;
FWhiteBmp: TBitmap; //保存白子;
FIsWhite: Boolean; //当前是否是白子;
FCheckerWidth: Integer; //棋子所占方格的宽度;
function PointToRect(x, y: Integer): TRect;
procedure DoDrawBackImage;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
FBackBmp := TBitmap.Create; //保存棋盘;
FBlackBmp := TBitmap.Create; //保存黑子;
FWhiteBmp := TBitmap.Create; //保存白子;
//加载图片;
FBackBmp.LoadFromFile('棋盘.bmp');
FBlackBmp.LoadFromFile('黑子.bmp');
FWhiteBmp.LoadFromFile('白子.bmp');
FIsWhite := False; //黑子先走;
DoDrawBackImage;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FBackBmp.Free;
FBlackBmp.Free;
FWhiteBmp.Free;
end;
function TForm1.PointToRect(x, y: Integer): TRect;
begin
//这里假设x和y为左上顶点坐标;
//实用你要先将x、y换算为左上顶点坐标;
Result := Rect(x, y, x + FCheckerWidth, y + FCheckerWidth);
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin //画棋子;
with Canvas do
if FIsWhite then
CopyRect(PointToRect(x, y), FWhiteBmp.Canvas,
Rect(0, 0, FWhiteBmp.Width, FWhiteBmp.Height))
else
CopyRect(PointToRect(x, y), FBlackBmp.Canvas,
Rect(0, 0, FBlackBmp.Width, FBlackBmp.Height));
end;
procedure TForm1.DoDrawBackImage;
begin
Canvas.Draw(0, 0, FBackBmp);
end;
procedure TForm1.btnRestartClick(Sender: TObject);
begin //重新开始;
DoDrawBackImage;
FIsWhite := False;
end;
end.
其它的就你自己想了,毕竟是你要写程序。[
]