Senin, 16 April 2012

GAME KARTU MENGGUNAKAN DELPHI 7


Permainan kartu bukanlah permainan yang asing bagi para penggunakomputer. Pada setiap penginstalasian sistem operasi standarWindows Xp, permainan ini selalu telah terintegrasi pada sistemoperasi tersebut, dan tentunya telah siap juga untuk digunakandengan berbagai jenis aturan permainannya. Dalam bab ini, kita akanmencoba merancang varian lain dari permainan kartu, dengan aturantetap seperti permainan kartu lainnya, menyusun kartu dari kartudengan nilai tertinggi hingga kartu dengan nilai terendah.
Mari kawan kita Merancang Formnya…
Tambahkan icon SpeedButton1, SpeedButton2, SpeedButton3,SpeedButton4, SpeedButton5, SpeedButton6, MainMenu1, Timer1,ImageList1 dan Image1, pada form1. Atur berbagai property berikut :


SpeedButton1
Caption


Name
B_back
SpeedButton2
Caption
New

Name
B_donne
SpeedButton3
Caption


Name
B_fam
SpeedButton4
Caption


Name
B_habit
SpeedButton5
Caption


Name
B_nom
SpeedButton6
Caption


Name
B_vierge
 


Atur Main Menu sedemikian hingga seperti tampilan berikut :

 Untuk item Option pada Main Menu, penambahan berbagai sub-item lainnya diserahkan kepada pembaca.
Sehingga form yang terbentuk adalah sebagai berikut :
 

Nah Setelah keseluruhan listing dituliskan, maka program siap di eksekusi.
Adapun tampilan awal program adalah sebagai berikut :


Nih Saya kasih Listing/Kode Programnya…
Cekidot>>>

Kembali ke Form, klik 2 kali pada Timer1, setelah muncul halaman code editor, tuliskan listing berikut :

procedure TForm1.Timer1Timer(Sender: TObject);
var ch : string[1];
begin
inc(time);
if (time mod 60 < 10) then ch := '0'
else ch := ' ';
Image.canvas.FillRect(Rect(185,20,240,40));
Image.Canvas.Font.Size := 15;
Image.canvas.TextOut(185,20,inttostr(time div60) + ' : ' + ch + inttostr(time mod 60));
Image.Canvas.font.size := 8;
end;

Pada MainMenu, sorot File, lalu klik pada item New Game, lalu
tuliskan kode berikut :
procedure TForm1.B_DonneClick(Sender: TObject);
begin
Donne;
end;

Sorot File, lalu klik pada item Undo, lalu tuliskan kode berikut :

procedure TForm1.Annuler1Click(Sender: TObject);
var i, j : byte;
begin
if((couple = 0) and((T_Annul[1].rgb > 0) or (T_Annul[1].xtab > 0))) then
Begin
if (T_Annul[1].rgb > 0) then
T_Btapis[T_Annul[1].rgb] := T_Annul[1];
if (T_Annul[1].xtab > 0) Then
T_Htapis[T_Annul[1].xtab,T_Annul[1].ytab] := T_Annul[1];
if (T_Annul[2].rgb >0) then
T_Btapis[T_Annul[2].rgb] := T_Annul[2];
if (T_Annul[2].xtab > 0) Then
T_Htapis[T_Annul[2].xtab,T_Annul[2].ytab] := T_Annul[2];
end;
if ((T_annul[1].xtab = 0) and (T_annul[1].rgb =0) and (cartetir > 0)) then
begin
T_jeu[T_Btapis[carten].num] := false;
tirannul := T_Btapis[carten].num;
destruction(T_Btapis[carten]);
dec(cartetir);
Image.canvas.TextOut(10, 326,inttostr(31 - cartetir) + 'cartes ');
end;
i:= 0;
repeat inc(i) until (T_Btapis[i].num > 0);
j := i;
repeat inc(j) until (T_Btapis[j].num = 0);
if ((i < 32) and (i > 0)) then
begin
carte1 := i;
carten := j - 1;
end
else
begin
carte1 := 0;
carten := 0;
end;
if (couple = 1) then
begin
couple := 0; 
T_couple[1] := T_Htapis[1,1];
end;
T_face[9] := T_Btapis[carten];
FaceRefresh;
DrawRefresh;
exit;
end;
Klik pada halaman Image1, pada bagian Object Inspector, klik pada page control events,
Klik 2 kali pada ComboBox item OnMouseDown, lalu tuliskan kode berikut :
procedure TForm1.ImageMouseDown(Sender: TObject; Button:
TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i, j : integer;
function clickon(x1, y1, x2, y2 : word) : boolean;
begin
result := ((x >= x1) and (x <= x2) and (y >=y1) and(y <= y2));
end;
begin
timer1.enabled := true;
if clickon(T_face[i].x1, T_face[i].y1,T_face[i].x2, T_face[i].y2) then
begin
inc(couple);
T_couple[couple] := T_face[i];
Image.canvas.copyrect(rect(0,50,T_couple[couple].x2-T_couple[couple].x1,130),Image.canvas, rect(T_couple[couple].x1,
T_couple[couple].y1, T_couple[couple].x2,
T_couple[couple].y2));
Image.canvas.copymode := cmnotsrccopy;
Image.canvas.copyrect(rect(T_couple[couple].
x1, T_couple[couple].y1,
T_couple[couple].x2, T_couple[couple].y2),
Image.canvas, rect(0,50,T_couple[couple].x2
- T_couple[couple].x1,130));
Image.canvas.copymode := cmsrccopy;
Image.canvas.fillrect(rect(0,50,60,130));
end;
if clickon(5,5,90,65) then
Form1.B_DonneClick(sender);
if clickon(90,5,175,65) then
AnnulerlClick(Sender);
if clickon(560,5,610,65) then
Quitter1Click(Sender);
if clickon(8,345,70,427) then
Form1.B_BackClick(sender);
{gestion des couples de cartes}
{couple avant-derniere carte piochee/carte<>derniere carte piochee}
if (((T_couple[1].rgb = carten-1) and (T_Couple[2].rgb <> carten) and (carten > carte1+1) and (couple = 2))
or ((T_couple[2].rgb = carten-1) and (T_Couple[1].rgb <> carten) and (carten > carte1+1) and (couple = 2))) then
begin
couple := 0;
T_couple[1] := T_Htapis[1,1];
T_couple[2] := T_Htapis[1,1];
DrawRefresh;
end;
if ((coup le >= 2) and ((T_couple[1].num mod 13) + (T_couple[2].num mod 13) = 13)) then
begin
T_Annul[1] := T_couple[1];
T_Annul[2] := T_couple[2];
destruction(T_couple[1]);
destruction(T_couple[2]);
T_couple[1] := T_Htapis[1,1];
T_couple[2] := T_Htapis[1,1];
inc(cartepaire, 2);
if (carte1 > 0) then
begin
i := 0; repeat inc(i) until
(T_Btapis[i].num > 0);
j := i; repeat inc(j) until
(T_Btapis[j].num = 0);
if ((i < 32) and (i > 0)) then
Begin
carte1 := i; carten := j- 1; end
else begin
carte1 := 0; carten := 0;
end;
end;
drawRefresh;
couple := 0;
end;
if (couple >= 2) then
begin
T_couple[1] := T_Htapis[1,1];
T_couple[2] := T_Htapis[1,1];
DrawRefresh; couple := 0;
end;
if ((couple >= 1) and (T_couple[1].num mod 13 = 0)) then
begin
T_Annul[1] := T_couple[1]; T_Annul[2] := T_couple[1];
destruction(T_couple[1]);
inc(cartepaire);
if (carte1 > 0) then
begin

i := 0;
repeat inc(i) until
(T_Btapis[i].num > 0);
j := i;
repeat inc(j) until
(T_Btapis[j].num = 0);
if ((i < 32) and (i > 0)) then begin
carte1 := i;
carten := j - 1;
end
else begin
carte1 := 0; 
carten := 0; 
end;
end;
T_couple[1] : = T_Htapis[1,1];
DrawRefresh;
couple := 0;
end;
FaceRefresh;
{le jeu est fini ?}
if (cartepaire >= 52) then
begin
Timer1.Enabled := false;
Winner := True;
with TBestTimeDlg.Create(Self) do
try
ShowModal;
Finally
Free;
end;
Winner := False;
end;
end;

Klik 2 kali pada ComboBox item OnMouseMove, lalu tuliskan kode berikut :

procedure TForm1.ImageMouseMove(Sender: TObject; Shift:TShiftState;
X, Y: Integer);
function clickon(x1, y1, x2, y2 : word) : boolean;
begin
result := ((x > x1) and (x < x2) and (y > y1) and (y < y2));
end;
begin
if (clickon(5,5,90,65) and not(donneon)) then
begin
ImageLst.Draw(Image.Canvas, 5 , 5, 3);
Image.canvas.TextOut(30, 60, 'Donne’);
donneon := true;
end;
if (not(clickon(5,5, 90,65)) and donneon) then
begin
ImageLst.Draw(Image.Canvas, 5 , 5, 2);
Image.Canvas.FillRect(Rect(30,60,63,71));
donneon := false;
end;
if (clickon(90,5,175,65) and not(arrowon)) then
begin
ImageLst.Draw(Image.Canvas, 90, 5, 1);
Image.canvas.TextOut(100, 60, 'Annuler');
arrowon := true;
end;
if (not(clickon(90,5,175,65)) and arrowon) then
begin
ImageLst.Draw(Image.Canvas, 90, 5, 0);
Image.Canvas.FillRect(Rect(100,60,150,71));
arrowon := false;
end;
if (clickon(560,5,610,65) and not(dooron)) then
begin
ImageLst.Draw(Image.Canvas, 560, 5, 5);
Image.canvas.TextOut(570, 70, 'Quitter');
dooron := true;
end;
if (not (clickon(560,5,610,65)) and dooron) then
begin
ImageLst.Draw(Image.Canvas, 560, 5, 4);
Image.Canvas.FillRect (Rect(570,70,610,81));
dooron := false;
end;
end;

Klik dua kali pada halaman Form1, lalu tuliskan kode berikut :

procedure TForm1.FormCreate(Sender: TObject);
begin
form1.width := 640;
form1.height := 480;
form1.top := 0;
form1.left := 0;
color := clgreen;
Image.canvas.brush.color := clGreen;
Image.canvas.fillrect(rect(0,0,Image.width,
Image.Height));
ImageLst.Draw(Image.Canvas, 5 , 5, 2);
ImageLst.Draw(Image.Canvas, 90 , 5, 0);
ImageLst.Draw(Image.Canvas, 560, 5, 4);
Form1.B_DonneClick(Sender);
End;

Tidak ada komentar:

Posting Komentar