Ok!! Mari kita buat dengan sebuah proyek pada Delphi 7 dengan menambahkan komponen Image1 dengan ukuran width dan height 600. Perhatikan gambar berikut
tambahkan procedure init pada private sebagai procedure pembuka permainan seperti gambar berikut
isikan procedure init sebagai berikut :
procedure TForm1.init;
var i:byte;
begin
image1.Canvas.Pen.Style:=pssolid;
image1.Canvas.Pen.Width:=3;
image1.Canvas.Brush.Color:=clwhite;
image1.Canvas.Pen.Color:=clblack;
image1.Canvas.Rectangle(0,0,600,600);
for i:=1 to 2 do begin
image1.Canvas.MoveTo(200*i,0);
image1.Canvas.LineTo(200*i,600);
image1.Canvas.MoveTo(0,200*i);
image1.Canvas.LineTo(600,200*i);
end;
for i:=0 to 8 do begin
skor[0,i]:=false;
skor[1,i]:=false;
end;
langkah:=0;
end;
Buat variable global seperti berikut
var isuser1:boolean;
skor:array[0..1,0..8] of boolean;
langkah:byte;
Kemudian pada Form Create isikan script berikut
procedure TForm1.FormCreate(Sender: TObject); begin isuser1:=true; init; end;
dan pada image1 buka event onmouseup isikan script berikut :
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var indexi,indexj:integer;
procedure cross();
begin
image1.Canvas.MoveTo(indexi*200+30,indexj*200+30);
image1.Canvas.LineTo(indexi*200+170,indexj*200+170);
image1.Canvas.MoveTo(indexi*200+30,indexj*200+170);
image1.Canvas.LineTo(indexi*200+170,indexj*200+30);
end;
procedure circle();
begin
image1.Canvas.Ellipse(indexi*200+30,indexj*200+30,indexi*200+170,indexj*200+170);
end;
function isawinner(user:byte):boolean;
begin
result:=(skor[user,0] and skor[user,1] and skor[user,2])or
(skor[user,3] and skor[user,4] and skor[user,5])or
(skor[user,6] and skor[user,7] and skor[user,8])or
(skor[user,0] and skor[user,3] and skor[user,6])or
(skor[user,1] and skor[user,4] and skor[user,7])or
(skor[user,2] and skor[user,5] and skor[user,8])or
(skor[user,0] and skor[user,4] and skor[user,8])or
(skor[user,2] and skor[user,4] and skor[user,6]);
end;
begin
indexi:=x div 200;
indexj:=y div 200;
if isuser1 then begin
cross;
skor[0,indexi+3*indexj]:=true;
if isawinner(0) then begin
messagebox(handle,'The Cross is a winner!!','Winner',MB_OK or MB_ICONINFORMATION);
init;
end else if langkah=8 then begin
messagebox(handle,'Draw !!','Draw',MB_OK or MB_ICONINFORMATION);
init;
end else inc(langkah);
end else begin
circle;
skor[1,indexi+3*indexj]:=true;
if isawinner(1) then begin
messagebox(handle,'The Cricle is a winner!!','Winner',MB_OK or MB_ICONINFORMATION);
init;
end else if langkah=8 then begin
messagebox(handle,'Draw !!','Draw',MB_OK or MB_ICONINFORMATION);
init;
end else inc(langkah);
end;
isuser1:=not isuser1;
end;
sebagai hasilnya seperti berikut
bisa juga dilihat videonya



makasih gan ilmunya, tapi bisa ga dibikin melawan cpu?
BalasHapusmakasih gan ilmunya, tapi bisa ngga bikin yg untuk melawan cpunya?
BalasHapusharusnya bisa sih...
Hapus