I use variable s, an array of string type variable.
For delay, or controlling the speed, I use application.processmessages and sleep() combo command.
This code fills cell with blank (space) value that corresponds with s, except one cell. This one cell then "moves" to the right, into the cell next to it.
For this purpose I declare two integer type variable, sx and sy. This variable add itself by one every step. Based on this two variable, the cell that should be filled with star is decided.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure proses;
procedure updateGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
jalan: boolean = false;
s: array [0..4,0..4]of string;
sx,sy:integer;
implementation
{$R *.dfm}
procedure tform1.updateGrid;
var i,j:integer;
begin
for i:= 0 to 4 do begin
for j:=0 to 4 do begin
s[i,j]:='';
if (i=sy) and (j=sx) then s[i,j]:='*';
stringgrid1.Cells[j,i]:=s[i,j];
end;
end;
end;
procedure tform1.proses;
begin
while jalan=true do begin
updateGrid;
sx:=sx+1;
if sx=5 then begin
sx:=0;
sy:=sy+1;
if sy=5 then sy:=0;
end;
application.ProcessMessages; sleep(200);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var i,j:integer;
begin
sy:=0; sx:=0;
for i:= 0 to 4 do begin
for j:= 0 to 4 do begin
s[i,j]:='';
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
jalan := not jalan;
proses;
end;
end.
Bermain dengan stringgrid di Delphi. Kali ini menggerakkan bintang (asterisk) menyusuri kotak-kotak sel stringgrid. Senjata utamanya selain variabel s bertipe array string, juga perintah sleep() untuk mengatur kecepatan gerak.


No comments:
Post a Comment