Blame lazarus/jumper/unit1.pas

db2532
unit Unit1;
db2532
db2532
{$mode objfpc}{$H+}
db2532
db2532
interface
db2532
db2532
uses
db2532
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
db2532
  ExtCtrls, LCLType;
db2532
db2532
type
db2532
db2532
  { TForm1 }
db2532
db2532
  TForm1 = class(TForm)
db2532
    Image1: TImage;
db2532
    Label1: TLabel;
db2532
    Label2: TLabel;
db2532
    Timer1: TTimer;
db2532
    procedure FormCreate(Sender: TObject);
db2532
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
db2532
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
db2532
    procedure FormPaint(Sender: TObject);
db2532
    procedure Timer1Timer(Sender: TObject);
db2532
  private
db2532
    { private declarations }
db2532
  public
db2532
    { public declarations }
db2532
  end;
db2532
db2532
const
db2532
  speed = 10;
db2532
  jump = 40;
db2532
  gravity = 2;
db2532
db2532
var
db2532
  Form1: TForm1;
db2532
  blocks: array of TRect;
db2532
  px, py, vx, vy: integer;
db2532
  leftKey, rightKey: boolean;
db2532
  score, maxScore: integer;
db2532
db2532
implementation
db2532
db2532
uses Math;
db2532
db2532
{$R *.lfm}
db2532
db2532
procedure GenerateBlock(i: integer; top: integer);
db2532
var
db2532
    width: integer;
db2532
begin
db2532
    width := Random(Form1.ClientWidth div 6) + Form1.ClientWidth div 6;
db2532
    blocks[i].Left := Random(Form1.ClientWidth - width);
db2532
    blocks[i].Right := blocks[i].Left + width;
db2532
    blocks[i].Top := top;
db2532
    blocks[i].Bottom := blocks[i].Top + 10;
db2532
end;
db2532
db2532
db2532
procedure GenerateBlocks;
db2532
var
db2532
  i: integer;
db2532
begin
db2532
  Randomize;
db2532
  SetLength(blocks, 10);
db2532
  for i := 0 to length(blocks)-1 do begin
db2532
    GenerateBlock(i, Random(Form1.ClientHeight));
db2532
  end;
db2532
end;
db2532
db2532
procedure ScrollBlocks(dy: integer);
db2532
var
db2532
  i: integer;
db2532
begin
db2532
  if dy = 0 then exit;
db2532
  score := score + dy;
db2532
  if score > maxScore then maxScore := score;
db2532
  py := py + dy;
db2532
db2532
  for i := 0 to length(blocks)-1 do begin
db2532
    blocks[i].Top := blocks[i].Top + dy;
db2532
    blocks[i].Bottom := blocks[i].Bottom + dy;
db2532
    if blocks[i].Top < 0 then begin
db2532
      GenerateBlock(i, Form1.ClientHeight + Random(abs(dy)));
db2532
    end else
db2532
    if blocks[i].Top > Form1.ClientHeight then begin
db2532
      GenerateBlock(i, -10 - Random(abs(dy)));
db2532
    end;
db2532
  end;
db2532
end;
db2532
db2532
procedure TForm1.FormCreate(Sender: TObject);
db2532
begin
db2532
  GenerateBlocks;
db2532
  px := (blocks[0].Left + blocks[0].Right) div 2;
db2532
  py := blocks[0].Top;
db2532
  vx := 0; vy := 0;
db2532
end;
db2532
db2532
procedure TForm1.FormPaint(Sender: TObject);
db2532
var
db2532
  i: integer;
db2532
begin
db2532
  Canvas.Brush.Color := clBlack;
db2532
  for i := 0 to length(blocks)-1 do begin
db2532
    Canvas.FillRect(blocks[i]);
db2532
  end;
db2532
db2532
  Canvas.Draw(px - Image1.Picture.Bitmap.Width div 2,
db2532
              py - Image1.Picture.Bitmap.Height,
db2532
              Image1.Picture.Bitmap);
db2532
end;
db2532
db2532
procedure TForm1.Timer1Timer(Sender: TObject);
db2532
var
db2532
  i, j: integer;
db2532
begin
db2532
  vy := vy + gravity;
db2532
db2532
  if leftKey then px := px - speed;
db2532
  if rightKey then px := px + speed;
db2532
db2532
  if vy > 0 then begin
db2532
    for j := 0 to vy-1 do begin
db2532
      for i := 0 to length(blocks)-1 do begin
db2532
        if (py = blocks[i].Top) and (px > blocks[i].Left) and (px < blocks[i].Right) then begin
db2532
          vy := -jump;
db2532
          break;
db2532
        end;
db2532
      end;
db2532
      if vy <= 0 then break;
db2532
      py := py + 1;
db2532
    end;
db2532
  end else begin
db2532
    py := py + vy;
db2532
  end;
db2532
db2532
  ScrollBlocks(ClientHeight div 2 - py);
db2532
  Label1.Caption := 'Score: ' + IntToStr(score);
db2532
  Label2.Caption := 'Max score: ' + IntToStr(maxScore);
db2532
  Label1.Refresh;
db2532
  Label2.Refresh;
db2532
db2532
  Refresh;
db2532
end;
db2532
db2532
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
db2532
  Shift: TShiftState);
db2532
begin
db2532
  if Key = VK_LEFT then leftKey := true;
db2532
  if Key = VK_RIGHT then rightKey := true;
db2532
  if Key = VK_SPACE then GenerateBlocks;
db2532
end;
db2532
db2532
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
db2532
  Shift: TShiftState);
db2532
begin
db2532
  if Key = VK_LEFT then leftKey := false;
db2532
  if Key = VK_RIGHT then rightKey := false;
db2532
end;
db2532
db2532
end.
db2532