Blame lazarus/platformer/unit1.pas

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