Nugroho's blog.: Walking Star in Delphi's Stringgrid.

Tuesday, May 16, 2017

Walking Star in Delphi's Stringgrid.

This code moves the star from cell to cell on string grid.

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:

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)