Results 1 to 9 of 9

Thread: Object Oriented programin (step one of many)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Hi,

    I've continued with my next step , which in the end turned out harder then I thought (atleast for me) ...
    What I did is...define a map now, and draw it onscreen using shapes... then I've created my monster class which can navigate inside this maze...
    Directions are choosen from available directions on random when I hit the wall...

    I think it turned out quiet good...anyway I am dead tired , here is the code, I'm off to play one more goodnight SC2 , then off to sleep.

    Code:
    unit MainFrm;
    // idea is
    // I check available directions...
    // then I set a direction
    // when I would hit a wall...I check again available directions...
    // then I set a direction...
    // could be good
    interface
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;
    type
      TMain = class(TForm)
        Panel1: TPanel;
        Panel2: TPanel;
        Button1: TButton;
        Button2: TButton;
        Timer1: TTimer;
        Edit1: TEdit;
        procedure DrawMap();
        procedure DrawMonster();
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      TDirection = ( dUp, dDown , dLeft, dRight );
      TMonster = class
        strict private
          FName : string;
          FStartX,FStartY : byte;
          FSetDir : TDirection;
          Direction : array of TDirection;
        published
          property Name : string
            read FName write FName;
          property StartX : byte
            read FStartX write FStartX;
          property StartY : byte
            read FStartY write FStartY;
          property SetDir : TDirection
            read FSetDir write FSetDir;
          constructor Create( Name : string ; StartX,StartY : byte ; SetDir : TDirection );
          procedure GetPossibleDirections();
          procedure OnMove;
      end;
    
    
    
      TMapLine = array [0..19] of Integer;
      TMap = array [0..14] of TMapLine;
    const
      Map: Array [0..14,0..19] of Integer = ((1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1),
                                             (1,0,1,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1),
                                             (1,0,1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,1),
                                             (1,0,1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,1),
                                             (1,0,1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,1),
                                             (1,0,1,1,1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,1),
                                             (1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1),
                                             (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1));
    
    var
      Main: TMain;
      Monster : array of TMonster;
      // hacky way later fix
      AI : array of TShape;
    
    
    implementation
    {$R *.dfm}
    constructor TMonster.Create(Name: string ; StartX,StartY : byte; SetDir : TDirection);
    begin
      FName := Name;
      FStartX := StartX;
      FStartY := StartY;
      FSetDir := SetDir;
    end;
    procedure TMonster.GetPossibleDirections;
    begin
      // debug
      Main.Edit1.Text:='';
      // empty directions
      SetLength(Direction,0);
      // no wall found up, way is free
      if Map[StartY-1,StartX] <> 1 then
        begin
          SetLength(Direction,length(Direction)+1);
          Direction[High(Direction)]:=dUp;
          Main.Edit1.Text:=Main.Edit1.Text+' UP ';
        end;
      // no wall found down, way is free
      if Map[StartY+1,StartX] <> 1 then
        begin
          SetLength(Direction,length(Direction)+1);
          Direction[High(Direction)]:=dDown;
          Main.Edit1.Text:=Main.Edit1.Text+' DOWN ';
        end;
      // no wall found left , way is free
      if Map[StartY,StartX-1] <> 1 then
        begin
          SetLength(Direction,length(Direction)+1);
          Direction[High(Direction)]:=dLeft;
          Main.Edit1.Text:=Main.Edit1.Text+' LEFT ';
        end;
      // no wall found right , way is free
      if Map[StartY,StartX+1] <> 1 then
        begin
          SetLength(Direction,length(Direction)+1);
          Direction[High(Direction)]:=dRight;
          Main.Edit1.Text:=Main.Edit1.Text+' RIGHT ';
        end;
    end;
    procedure TMonster.OnMove;
    begin
      if SetDir = dUp then
        begin
          if Map[StartY-1,StartX] = 1 then
            begin
              GetPossibleDirections();
              SetDir := Direction[random(length(Direction))];
            end;
        end;
      if SetDir = dDown then
        begin
          if Map[StartY+1,StartX] = 1 then
            begin
              GetPossibleDirections();
              SetDir := Direction[random(length(Direction))];
            end;
        end;
      if SetDir = dRight then
        begin
          if Map[StartY,StartX+1] = 1 then
            begin
              GetPossibleDirections();
              SetDir := Direction[random(length(Direction))];
            end;
        end;
      if SetDir = dLeft then
        begin
          if Map[StartY,StartX-1] = 1 then
            begin
              GetPossibleDirections();
              SetDir := Direction[random(length(Direction))];
            end;
        end;
      if SetDir = dUp then
        StartY:=StartY-1;
      if SetDir = dDown then
        StartY:=StartY+1;
      if SetDir = dRight then
        StartX:=StartX+1;
      if SetDir = dLeft then
        StartX:=StartX-1;
    
    end;
    procedure TMain.Button1Click(Sender: TObject);
    var i : integer;
    begin
      DrawMap();
      // we do 2 monsters for now
      SetLength(Monster,2);
      SetLength(AI,2);
      Monster[0] := TMonster.Create('Robert',1,1,dDown);
      Monster[1] := TMonster.Create('Devid',3,1,dDown);
      for i := 0 to length(Monster)-1 do
        begin
          AI[i] := TShape.Create(self);
          AI[i].Parent:=Panel2;
          AI[i].Top:=Monster[i].StartY*(Panel2.Height div 15);
          AI[i].Left:=Monster[i].StartX*(Panel2.Width div 20);
          AI[i].Height:=Panel2.Height div 15;
          AI[i].Width:=Panel2.Width div 20;
          AI[i].Brush.Color:=clGreen;
          AI[i].Pen.Color:=clGreen;
          AI[i].Visible:=true;
        end;
    end;
    procedure TMain.Button2Click(Sender: TObject);
    begin
      Timer1.Enabled:=true;
    end;
    procedure TMain.DrawMonster;
    var i : integer;
    begin
      // re-draw monster at new position
      for i := 0 to length(Monster)-1 do
        begin
          AI[i].Top:=Monster[i].StartY*(Panel2.Height div 15);
          AI[i].Left:=Monster[i].StartX*(Panel2.Width div 20);
        end;
    end;
    procedure TMain.Timer1Timer(Sender: TObject);
    begin
      Monster[0].OnMove;
      Monster[1].OnMove;
      DrawMonster();
    end;
    procedure TMain.DrawMap;
    var x,y: integer;
        Wall : TShape;
    begin
        for y := 0 to 14 do
          for x := 0 to 19 do
              begin
                if Map[y,x] = 1 then
                  begin
                    Wall := TShape.Create(self);
                    Wall.Parent:=Panel2;
                    Wall.Top:=y*(Panel2.Height div 15);
                    Wall.Left:=x*(Panel2.Width div 20);
                    Wall.Height:=Panel2.Height div 15;
                    Wall.Width:=Panel2.Width div 20;
                    Wall.Brush.Color:=clRed;
                    Wall.Pen.Color:=clRed;
                    Wall.Visible:=true;
                  end;
              end;
    end;
    end.
    the code might be a little ugly I was In a hurry to make 2 monsters walk around....

    USAGE!!!!
    1. Click Draw
    2. Click Move

    Otherwise EXCEPTION...
    Attached Files Attached Files

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •