Nugroho's blog.: Flappy Bird Like in Delphi

## Thursday, April 13, 2017

### Flappy Bird Like in Delphi

Remember the infamous Flappy Bird? Yup, I will create the program based on that algorithm.

I use tshape as "bird" wtih name "kotak" and array of "pipe" with name "pipa"

```procedure tform1.nilaiAwal;
var i:integer;
begin
for i:=1 to 7 do begin
pipa[i]:=tshape.Create(form1);
pipa[i].Parent:=form1;
pipa[i].Left:=400+200*i;
pipa[i].Height:=70+random(70);
end;
//
kotak:=tShape.Create(form1);
kotak.Parent:=form1;
kotak.Left:=200;
//
a:=-7;
v:=40;       y:=0;
dt:=1;       skor:=0;
jalan:=false;
vx:=10;
edit1.Enabled:=false;
end;
```

The code below using Euler Method to move "the pipe" and "the bird"
```procedure tform1.proses;
var i:integer;
begin
while jalan=true do begin
for i:=1 to 7 do begin
pipa[i].Left:=pipa[i].Left-vx;
if pipa[i].Left+pipa[i].Width<0 then begin
pipa[i].Left:=200*7;   pipa[i].Height:=70+random(90);
end;
end;
v:=v+a*dt;      y:=y+v*dt;
kotak.Top:=round(y);        tabrak;
edit1.Text:=intToStr(skor);
application.ProcessMessages;sleep(100);
end;
end;

```

And the scoring (or loosing) is here

```procedure tform1.tabrak;
var i:integer;
begin
for i:=1 to 7 do begin
if (kotak.Left>pipa[i].Left)
and (kotak.Left<pipa[i].left+pipa[i].Width)
then begin
if(kotak.Top<pipa[i].Height)
then  jalan:= false
else skor:=skor+1;

end;
end;
end;
```

Here's the full code
```unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, extCtrls, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
procedure proses;
procedure nilaiAwal;
procedure tabrak;
procedure FormCreate(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
kotak: tShape;
pipa: array[1..7] of tShape;
a,v,y,dt:real;
vx,skor:integer;
jalan:boolean;
implementation

{\$R *.dfm}
procedure tform1.nilaiAwal;
var i:integer;
begin
for i:=1 to 7 do begin
pipa[i]:=tshape.Create(form1);
pipa[i].Parent:=form1;
pipa[i].Left:=400+200*i;
pipa[i].Height:=70+random(70);
end;
//
kotak:=tShape.Create(form1);
kotak.Parent:=form1;
kotak.Left:=200;
//
a:=-7;
v:=40;       y:=0;
dt:=1;       skor:=0;
jalan:=false;
vx:=10;
edit1.Enabled:=false;
end;
procedure tform1.proses;
var i:integer;
begin
while jalan=true do begin
for i:=1 to 7 do begin
pipa[i].Left:=pipa[i].Left-vx;
if pipa[i].Left+pipa[i].Width<0 then begin
pipa[i].Left:=200*7;   pipa[i].Height:=70+random(90);
end;
end;
v:=v+a*dt;      y:=y+v*dt;
kotak.Top:=round(y);        tabrak;
edit1.Text:=intToStr(skor);
application.ProcessMessages;sleep(100);
end;
end;
procedure tform1.tabrak;
var i:integer;
begin
for i:=1 to 7 do begin
if (kotak.Left>pipa[i].Left)
and (kotak.Left<pipa[i].left+pipa[i].Width)
then begin
if(kotak.Top<pipa[i].Height)
then  jalan:= false
else skor:=skor+1;

end;
end;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
jalan:=true;
v:=40;
proses;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
nilaiAwal;
end;
end.

```
.