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.

.




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)