Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, ExtCtrls;
- type
- TForm1 = class(TForm)
- Image1: TImage;
- Timer1: TTimer;
- procedure FormCreate(Sender: TObject);
- procedure FormKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure Timer1Timer(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- texture: array [0..7] of TBitmap;
- map: array [0..25, 0..25] of integer;
- persPosX, persPosY, rotation: integer;
- IsFire:boolean;
- bollX, bollY, bollRot:integer;
- implementation
- {$R *.dfm}
- procedure readMap();
- var
- f: text;
- i, j: integer;
- begin
- AssignFile(f, 'res/map.txt');
- Reset(f);
- Read(f, persPosX);
- Read(f, persPosY);
- ReadLn(f);
- i := 0;
- while not Eof(f) do begin
- j := 0;
- while not Eoln(f) do begin
- Read(f, map[j, i]);
- Inc(j);
- end;
- ReadLn(f);
- Inc(i);
- end;
- CloseFile(f);
- end;
- procedure drawPers(x, y, rot: integer);//rot= 1-left, 2-right, 3-up, 4-down
- begin
- Form1.Image1.Canvas.Draw(x*20, y*20, texture[rot+1]);
- end;
- procedure drawMap();
- var x, y:integer;
- begin
- for x:=0 to Form1.Image1.Height div 20 do
- for y:=0 to Form1.Image1.Width div 20 do
- begin
- Form1.Image1.Canvas.Draw(x*20, y*20, texture[map[x, y]]);
- end
- end;
- procedure drawBoll(x, y: integer);
- begin
- Form1.Image1.Canvas.Draw(x*20, y*20, texture[6]);
- end;
- procedure drawExp(x, y: integer);
- begin
- Form1.Image1.Canvas.Draw(x*20, y*20, texture[7]);
- end;
- function Valid(dir:integer):boolean; //dir= 1-left, 2-right, 3-up, 4-down
- begin
- if dir = 1 then
- if persPosX>0 then
- if map[persPosX-1, persPosY] = 0 then
- Result:=true
- else
- Result:=false;
- if dir = 2 then
- if persPosX<25 then
- if map[persPosX+1, persPosY] = 0 then
- Result:=true
- else
- Result:=false;
- if dir = 3 then
- if persPosY>0 then
- if map[persPosX, persPosY-1] = 0 then
- Result:=true
- else
- Result:=false;
- if dir = 4 then
- if persPosY<25 then
- if map[persPosX, persPosY+1] = 0 then
- Result:=true
- else
- Result:=false;
- end;
- procedure explosive();
- begin
- IsFire:=false;
- if bollRot = 1 then
- begin
- drawExp(bollX-1, bollY);
- map[bollX-1, bollY]:=0;
- end;
- if bollRot = 2 then
- begin
- drawExp(bollX+1, bollY);
- map[bollX+1, bollY]:=0;
- end;
- if bollRot = 3 then
- begin
- drawExp(bollX, bollY-1);
- map[bollX, bollY-1]:=0;
- end;
- if bollRot = 4 then
- begin
- drawExp(bollX, bollY+1);
- map[bollX, bollY+1]:=0;
- end;
- end;
- procedure fire();
- begin
- if not IsFire then
- begin
- IsFire:=true;
- bollRot:=rotation;
- bollX:=persPosX;
- bollY:=persPosY;
- drawBoll(bollX, bollY);
- end;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- var x, y:integer;
- begin
- IsFire:=false;
- texture[1]:=TBitmap.Create; //gnd texture
- texture[0]:=TBitmap.Create; //white texture
- texture[2]:=TBitmap.Create; //persL
- texture[3]:=TBitmap.Create; //persR
- texture[4]:=TBitmap.Create; //persU
- texture[5]:=TBitmap.Create; //persD
- texture[6]:=TBitmap.Create; //boll
- texture[7]:=TBitmap.Create; //explosive
- texture[1].LoadFromFile('res/ground.bmp');
- texture[0].LoadFromFile('res/null.bmp');
- texture[2].LoadFromFile('res/persL.bmp');
- texture[3].LoadFromFile('res/persR.bmp');
- texture[4].LoadFromFile('res/persU.bmp');
- texture[5].LoadFromFile('res/persD.bmp');
- texture[6].LoadFromFile('res/boll.bmp');
- texture[7].LoadFromFile('res/explosive.bmp');
- readMap();
- drawMap();
- drawPers(persPosX, persPosY, 2);
- end;
- procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if key = 37 then
- begin
- rotation:=1;
- if Valid(1) then
- persPosX:=persPosX-1;
- end;
- if key = 39 then
- begin
- rotation:=2;
- if Valid(2) then
- inc(persPosX);
- end;
- if key = 38 then
- begin
- rotation:=3;
- if Valid(3) then
- persPosY:=persPosY-1;
- end;
- if key = 40 then
- begin
- rotation:=4;
- if Valid(4) then
- inc(persPosY);
- end;
- if key = 32 then fire();
- drawMap();
- drawPers(persPosX, persPosY, rotation);
- end;
- procedure TForm1.Timer1Timer(Sender: TObject);
- begin
- if IsFire then
- begin
- drawMap();
- drawBoll(bollX, bollY);
- drawPers(persPosX, persPosY, rotation);
- if bollRot = 1 then
- if bollX>0 then
- begin
- if map[bollX-1, bollY] = 0 then
- bollX:=bollX-1
- else
- explosive();
- end
- else
- explosive();
- if bollRot = 2 then
- if bollX<25 then
- begin
- if map[bollX+1, bollY] = 0 then
- bollX:=bollX+1
- else
- explosive();
- end
- else
- explosive();
- if bollRot = 3 then
- if bollY>0 then
- begin
- if map[bollX, bollY-1] = 0 then
- bollY:=bollY-1
- else
- explosive();
- end
- else
- explosive();
- if bollRot = 4 then
- if bollY<25 then
- begin
- if map[bollX, bollY+1] = 0 then
- bollY:=bollY+1
- else
- explosive();
- end
- else
- explosive();
- end else begin
- drawMap();
- drawPers(persPosX, persPosY, rotation);
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement