All pastes #358629 Raw Edit

Untitled

public text v1 · immutable
·published 2007-02-16 10:08 UTC
rendered paste body
unit HraUnit;

interface

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

type
  TRybka = class
    bmp: array[0..1, 0..3, 0..9] of TBitmap;
    x, y, dx, dy, faza,smer,uroven,life: Integer;
    constructor Create(xx, yy: Integer);
    procedure kresli(c: TCanvas);
    destructor Destroy; override;
  end;

  TMinca = class
    bmp: array [0..2,0..9] of TBitmap;
    x, y, dy, faza,uroven: Integer;
    constructor Create(xx, yy: Integer);
    procedure kresli(c: TCanvas);
    destructor Destroy; override;
  end;

  TChobotnica = class
    bmp: array [0..9] of TBitmap;
    x, y, dx, dy, faza: Integer;
    constructor Create(xx, yy: Integer);
    procedure kresli(c: TCanvas);
    destructor Destroy; override;
  end;

  THra = class
  private
    c: TCanvas;
    body,pozx,pozy: Integer;
    rybky: array of TRybka;
    pozadie: TBitmap;
    mince: array of TMinca;
    hrac: TChobotnica;
    poz: array of record x, y: Integer; end;  // zoznam pozícií
  public
    constructor Create(cc: TCanvas);
    function tik: Boolean;              // tikanie èasovaèa
    procedure klaves(key: Word);        // stláèanie klávesov
  end;

implementation

{TRybka}

constructor TRybka.Create(xx, yy: Integer);
var
 b:Tbitmap;
 i,j:integer;
begin
x:=xx;
y:=yy;
dy:=0;
life:=1100;
b:=Tbitmap.Create;
b.LoadFromFile('rybkaVlavo.bmp');
for i:=0 to 3 do
 for j :=0 to 9 do
 begin
 bmp[0,i,j]:=TBitmap.Create;
 bmp[0,i,j].Width:=80;
 bmp[0,i,j].Height:=80;
 bmp[0,i,j].Transparent:=True;
 bmp[0,i,j].PixelFormat:=pf24bit;
 bmp[0,i,j].Canvas.Draw(j*-80,i*-80,b);
 end;
b.free;

b:=Tbitmap.Create;
b.LoadFromFile('rybkaVpravo.bmp');
for i:=0 to 3 do
 for j :=0 to 9 do
 begin
 bmp[1,i,j]:=TBitmap.Create;
 bmp[1,i,j].Width:=80;
 bmp[1,i,j].Height:=80;
 bmp[1,i,j].Transparent:=True;
 bmp[1,i,j].PixelFormat:=pf24bit;
 bmp[1,i,j].Canvas.Draw(j*-80,i*-80,b);
 end;
b.free;

end;

procedure TRybka.kresli(c: TCanvas);
begin
faza:=(faza+1) mod 9;
if dx<0 then
 c.Draw(x,y,bmp[0,uroven,faza]);
if dx>0 then
 c.Draw(x,y,bmp[1,uroven,faza]);
end;

destructor TRybka.Destroy;
var i,j:integer;
begin
for i:=0 to 3 do
 for j :=0 to 9 do
 bmp[0,i,j].free;
 for i:=0 to 3 do
 for j :=0 to 9 do
 bmp[1,i,j].free
end;

{TMinca}

constructor TMinca.Create(xx, yy: Integer);
var
 b:Tbitmap;
 i,j:integer;
begin
x:=xx;
y:=yy;
dy:=0;
b:=Tbitmap.Create;
b.LoadFromFile('peniazky.bmp');
for i:=0 to 2 do
 for j :=0 to 9 do
 begin
 bmp[i,j]:=TBitmap.Create;
 bmp[i,j].Width:=72;
 bmp[i,j].Height:=72;
 bmp[i,j].Transparent:=True;
 bmp[i,j].PixelFormat:=pf24bit;
 bmp[i,j].Canvas.Draw(j*-72,i*-72,b);
 end;
b.free;
end;

procedure TMinca.kresli(c: TCanvas);
begin
faza:=(faza+1) mod 9;
inc(y);
c.draw(x,y,bmp[uroven,faza]);

end;

destructor TMinca.Destroy;
begin
end;

{TChobnotnica}

constructor TChobotnica.Create(xx, yy: Integer);
var
 b:Tbitmap;
 i:integer;
begin
x:=xx;
y:=yy;
dx:=20;
dy:=20;
b:=Tbitmap.Create;
b.LoadFromFile('chobotnica.bmp');
for i:=0 to 9 do
 begin
 bmp[i]:=TBitmap.Create;
 bmp[i].Width:=60;
 bmp[i].Height:=60;
 bmp[i].Transparent:=True;
 bmp[i].PixelFormat:=pf24bit;
 bmp[i].Canvas.Draw(i*-60,0,b);
 end;
b.free;

end;

procedure TCHobotnica.kresli(c: TCanvas);
begin
faza:=(faza+1) mod 9;
c.Draw(x,y,bmp[faza]);
end;

destructor TChobotnica.Destroy;
var i:integer;
begin
for i:=0 to 9 do
bmp[i].free;

end;

{THra}

constructor THra.Create(cc: TCanvas);
var t:textfile;
 i:integer;

begin
c:=cc;
body:=400;
pozadie:=Tbitmap.create;
pozadie.LoadFromFile('akvarium.bmp');
hrac:=Tchobotnica.Create(200,200);
AssignFile(t,'pozicie.txt');
reset(t);
while not Eof(t) do
begin
SetLength(poz,length(poz)+1);
read(t,poz[High(poz)].x);
read(t,poz[High(poz)].y);
end;



end;

function Thra.tik: Boolean;              // tikanie èasovaèa
var
 i,x:integer;
begin


c.Draw(0,0,pozadie);
hrac.kresli(c);

if body > 99 then
begin
SetLength(rybky,length(rybky)+1);

rybky[High(rybky)]:=TRybka.Create(poz[high(rybky)].x,(poz[High(rybky)].y));
rybky[High(rybky)].dx:=Random(4)-2;
rybky[High(rybky)].dy:=Random(4)-2;
rybky[High(rybky)].uroven:=0;
dec(body,100);

end;

if rybky<>nil  then
 for i:=0 to high(rybky) do
 begin
 rybky[i].kresli(c);
 end;
i:=-1;
if rybky<>nil then

while i < High(rybky) do
   begin
    inc(i);
    if rybky[i].uroven>0 then
     if random(100)=0 then begin
     SetLength(mince, Length(mince)+1);
     mince[high(mince)] := TMinca.Create(rybky[i].x,rybky[i].y);
     mince[high(mince)].uroven:= rybky[i].uroven-1;
     end;
    inc(rybky[i].y,rybky[i].dy);
    inc(rybky[i].x,rybky[i].dx);
    if rybky[i].x<0 then rybky[i].dx:=-rybky[i].dx;;
    if rybky[i].y<0 then rybky[i].dy:=-rybky[i].dy;;
    if rybky[i].x>580 then rybky[i].dx:=-rybky[i].dx;
    if rybky[i].y>400 then rybky[i].dy:=-rybky[i].dy;
    dec(rybky[i].life);
    if rybky[i].life>1100 then rybky[i].uroven:=0
    else if (rybky[i].life>800) and (rybky[i].life<1000) then rybky[i].uroven:=1
    else if (rybky[i].life>400) and (rybky[i].life<801) then rybky[i].uroven:=2
    else if (rybky[i].life>0) and (rybky[i].life<401) then rybky[i].uroven:=3
    else if (rybky[i].life<1) then begin
       rybky[i].free;
       rybky[i]:=rybky[high(rybky)];
       SetLength(rybky,Length(rybky)-1);
       end;
   if rybky<>nil then begin
         if (ABS(rybky[i].x-hrac.x)<30) and (ABS(rybky[i].y-hrac.y)<30) then
  begin
  rybky[i]:=rybky[high(rybky)];
  SetLength(rybky,Length(rybky)-1);
  end;

  x:=0;
  while x < High(mince)+1 do
  begin
  if (ABS(mince[x].x-hrac.x) <30) and (ABS(mince[x].y-hrac.y) <30) then
    begin
    if mince[i].uroven=0 then inc(body,10);
    if mince[i].uroven=1 then inc(body,20);
    if mince[i].uroven=2 then inc(body,30);
       mince[i].free;
    mince[i]:=mince[high(mince)];
    SetLength(mince,Length(mince)-1);
    i:=high(mince)+1;
    end else inc(x);


                end;

 end;


if mince<> nil then begin
   for i:= 0 to High(mince) do
    mince[i].kresli(c);
   end;


c.Font.Height := 20;
c.Font.Color := clYellow;
c.TextOut(550,10,IntToStr(body));
if (body<100) and ((rybky)=nil) then result:=false;
end;
end;

procedure Thra.klaves(key: Word);        // stláèanie klávesov
begin
case key of
vk_right:if hrac.x < 580 then inc(hrac.x,hrac.dx);
vk_left:if hrac.x >0 then dec(hrac.x,hrac.dx);
vk_up:if hrac.y>0 then dec(hrac.y,hrac.dy);
vk_down:if hrac.y < 420 then inc(hrac.y,hrac.dy);
end;

end;




end.