Thema: Delphi Pac-Man Hilfe

Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#17

AW: Pac-Man Hilfe

  Alt 11. Apr 2015, 14:35
Was ich ebenfalls vermisse ist das Konzept. Das könnte z.B. in etwa so aussehen (Siehe unten). Bin mir aber nicht sicher, zum Beispiel Sir Rufo kann doch sowas normalerweise immer recht gut.
Delphi-Quellcode:
unit uPacMan;

interface

uses
  Types, SysUtils, Dialogs, Classes, Graphics;

type
  TPacManStyle = (pmsPacMan, pmsRedGuy, pmsPinkGuy, pmsCyanGuy, pmsOrangeGuy);

  TPacManPowerStyle = (pmpDefault, pmpPower);

  TPacManOrientation = (pmoCenter, pmoLeft, pmoRight, pmoTop, pmoBottom);

  TPacManGuy = class
  private
    FStyle: TPacManStyle;
    FPowerStyle: TPacManPowerStyle;
    FOrientation: TPacManOrientation;
    FX, FY: integer;
  public
    property Style: TPacManStyle read FStyle write FStyle;
    property PowerStyle: TPacManPowerStyle read FPowerStyle write FPowerStyle;
    property Orientation: TPacManOrientation read FOrientation write FOrientation;
    property X: integer read FX write FX;
    property Y: integer read FY write FY;
  end;

  TPacManGuys = array[TPacManStyle] of TPacManGuy;

  TPacManCellStatus = (pmcEmpty, pmcWall, pmcPellet, pmcPowerPellet,
    pmcPacMan, pmcRedGuy, pmcPinkGuy, pmcCyanGuy, pmcOrangeGuy);

  TPacManCell = class
  private
    FX, FY, FWidth, FHeight: integer;
    FStatus: TPacManCellStatus;
  public
    procedure Draw(Canvas: TCanvas; Bitmap: TBitmap);
    property X: integer read FX write FX;
    property Y: integer read FY write FY;
    property Width: integer read FWidth write FWidth;
    property Height: integer read FHeight write FHeight;
    property Status: TPacManCellStatus read FStatus write FStatus;
  end;

  TPacManGrid = class
  private
    FColCount, FRowCount: integer;
    FCells: array of array of TPacManCell;
    function GetCell(X, Y: integer): TPacManCell;
  public
    property Cell[X, Y: integer]: TPacManCell read GetCell; default;
    property ColCount: integer read FColCount;
    property RowCount: integer read FRowCount;
    constructor Create(const ColCount, RowCount, Width, Height: integer);
    destructor Destroy; override;
  end;

  TPacManGame = class
  private
    FGuys: TPacManGuys;
    FGrid: TPacManGrid;
    function GetGuy(Style: TPacManStyle): TPacManGuy;
  public
    property Guy[Style: TPacManStyle]: TPacManGuy read GetGuy;
    property Grid: TPacManGrid read FGrid;
    constructor Create(const ColCount, RowCount, Width, Height: integer);
    destructor Destroy; override;
  end;

implementation

{ TPacManCell }

procedure TPacManCell.Draw(Canvas: TCanvas; Bitmap: TBitmap);
var
  Rect: TRect;
begin
  Rect.Left := FX * FWidth;
  Rect.Top := FY * FHeight;
  Rect.Right := Rect.Left + FWidth;
  Rect.Bottom := Rect.Top + FHeight;
  Canvas.MoveTo(Rect.Left, Rect.Top);
  Canvas.LineTo(Rect.Right, Rect.Top);
  Canvas.LineTo(Rect.Right, Rect.Bottom);
  Canvas.LineTo(Rect.Left, Rect.Bottom);
  Canvas.LineTo(Rect.Left, Rect.Top);
  if Bitmap <> nil then
  begin
    Rect.Left := Rect.Left + 4;
    Rect.Top := Rect.Top + 4;
    Rect.Right := Rect.Right - 4;
    Rect.Bottom := Rect.Bottom - 4;
    Canvas.StretchDraw(Rect, Bitmap);
  end;
end;

{ TPacManGrid }

constructor TPacManGrid.Create(const ColCount, RowCount, Width, Height: integer);
var
  X, Y: integer;
begin
  FColCount := ColCount;
  FRowCount := RowCount;
  SetLength(FCells, FColCount, FRowCount);
  for X := 0 to FColCount - 1 do
    for Y := 0 to FRowCount - 1 do
    begin
      FCells[X, Y] := TPacManCell.Create;
      FCells[X, Y].X := X;
      FCells[X, Y].Y := Y;
      FCells[X, Y].Width := Width div ColCount;
      FCells[X, Y].Height := Height div RowCount;
    end;
end;

destructor TPacManGrid.Destroy;
var
  X, Y: integer;
begin
  for X := 0 to FColCount - 1 do
    for Y := 0 to FRowCount - 1 do
      FCells[X, Y].Free;
  SetLength(FCells, 0);
  inherited;
end;

function TPacManGrid.GetCell(X, Y: integer): TPacManCell;
begin
  Result := FCells[X, Y];
end;

{ TPacManGame }

constructor TPacManGame.Create(const ColCount, RowCount, Width, Height: integer);
var
  Style: TPacManStyle;
begin
  FGrid := TPacManGrid.Create(ColCount, RowCount, Width, Height);
  for Style := pmsPacMan to pmsOrangeGuy do
  begin
    FGuys[Style] := TPacManGuy.Create;
    FGuys[Style].Style := Style;
  end;
end;

destructor TPacManGame.Destroy;
var
  Style: TPacManStyle;
begin
  FGrid.Free;
  for Style := pmsPacMan to pmsOrangeGuy do
    FGuys[Style].Free;
  inherited;
end;

function TPacManGame.GetGuy(Style: TPacManStyle): TPacManGuy;
begin
  Result := FGuys[Style];
end;

end.

Geändert von Bjoerk (11. Apr 2015 um 15:09 Uhr) Grund: TPacManGuys eingeführt.
  Mit Zitat antworten Zitat