Results 1 to 2 of 2

Thread: Need Source Code

  1. #1

    Need Source Code

    Hello all,
    Was wondering if anyone can give me the source code for a typical RPG battle (a la Final Fantasy) please and also the source code for a game menu... it'd be really apreciated.
    Thanks
    Omega3k

  2. #2

    Need Source Code

    I remember that Bobby had a RPG battle thingy a while back on his old Delphi Sanctuary site (not the new design). It was called "_something_ Crossing" (I can't remember the first word). See if he has it sitting around (private message him or post here).

    About the game menu: clarify what you need. Bear in mind that at the simplest level, you could do this (untested; I wrote all this in Notepad):

    [pascal][background=#FFFFFF][comment=#0000FF][normal=#000000][number=#C00000][reserved=#000000][string=#00C000]uses
    Windows, Classes, { ...other stuff };

    const
    DEFAULT_TEXT_HEIGHT = 16;
    DEFAULT_SPACING = 8;

    type
    TMenu = class
    private
    FOptions: array of string;
    FSelected: Integer;
    FTextHeight: Integer;
    FSpacing: Integer;
    FStartPosition: TPoint;

    procedure SetOption(Index: Integer; const NewValue: string);
    function GetOption(Index: Integer): string;
    public
    constructor Create(const TheOptions: array of string);
    destructor Destroy; override;

    procedure Draw;
    procedure MoveToPreviousOption;
    procedure MoveToNextOption;

    // file i/o functions, assume you've already opened a textfile
    procedure SaveToFile(var InputFile: TextFile);
    procedure LoadFromFile(var InputFile: TextFile);

    property TextHeight: Integer read FTextHeight write FTextHeight;
    property Spacing: Integer read FSpacing write FSpacing;
    property StartPosition: TPoint read FStartPosition write FStartPosition;
    property Selected: Integer read FSelected;

    property Options[Index: Integer]: string read GetOption write SetOption;
    end;

    implementation

    constructor TMenu.Create(const TheOptions: array of string);
    var
    i: Integer;
    begin
    inherited Create;

    // copy over our choices
    SetLength(FOptions, Length(TheOptions));
    for i := Low(FOptions) to High(FOptions) do
    FOptions[i] := TheOptions[i]

    FTextHeight := DEFAULT_TEXT_HEIGHT;
    FSpacing := DEFAULT_SPACING;
    FStartPosition := Point(0, 0);
    FSelected := Low(FOptions);
    end;

    destructor TMenu.Destroy;
    begin
    FOptions := nil;
    inherited;
    end;

    procedure TMenu.Draw;
    var
    i: Integer;
    NextY: Integer;
    begin
    NextY := FStartPosition.Y;

    for i := Low(FOptions) to High(FOptions) do
    begin
    if i = FSelected then
    // todo: draw selected option at FStartPosition.X, NextY
    // use some highlighting (different foreground/background, or whatever
    // maybe a small image beside the text like an arrow
    else
    // todo: draw normal option at FStartPosition.X, NextY

    Inc(NextY, FTextHeight + FSpacing);
    end;
    end;

    procedure TMenu.MoveToPreviousOption;
    begin
    if FSelected = Low(FOptions) then
    FSelected := High(FOptions)
    else
    Dec(FSelected);
    end;

    procedure TMenu.MoveToNextOption;
    begin
    if FSelected = High(FOptions) then
    FSelected := Low(FOptions)
    else
    Inc(FSelected);
    end;

    procedure TMenu.SaveToFile(var InputFile: TextFile);
    var
    i: Integer;
    begin
    WriteLn(InputFile, FStartPosition.x, ' ', FStartPosition.y);
    WriteLn(InputFile, FSpacing);
    WriteLn(InputFile, FTextHeight);
    WriteLn(InputFile, FSelected);
    WriteLn(InputFile, Length(FOptions));
    for i := Low(FOptions) to High(FOptions) do
    WriteLn(InputFile, FOptions[i]);
    end;

    procedure TMenu.LoadFromFile(var InputFile: TextFile);
    var
    i: Integer;
    NumOptions: Integer;
    begin
    ReadLn(InputFile, FStartPosition.x, FStartPosition.y);
    ReadLn(InputFile, FSpacing);
    ReadLn(InputFile, FTextHeigth);
    ReadLn(InputFile, FSelected);
    ReadLn(InputFile, NumOptions);
    SetLength(FOptions, NumOptions);
    for i := Low(FOptions) to High(FOptions) do
    ReadLn(InputFile, FOptions[i]);
    end;

    procedure TMenu.SetOption(Index: Integer; const NewValue: string);
    begin
    FOptions[Index] := NewValue;
    end;

    function TMenu.GetOption(Index: Integer): string;
    begin
    Result := FOptions[Index];
    end;[/pascal]
    You get the idea there? To extend it, you could give the options a bounding area (like a rectangle). This lets you check the mouse too -- you'd check if the mouse was over a given rectangle:

    [pascal][background=#FFFFFF][comment=#0000FF][normal=#000000][number=#C00000][reserved=#000000][string=#00C000]for i := Low(FOptionAreas) to High(FOptionAreas) do
    begin
    if (MouseX >= FOptionAreas[i].Left) and (MouseX < FOptionAreas[i].Right) and
    (MouseY >= FOptionAreas[i].Top) and (MouseY < FOptionAreas[i].Bottom) then
    begin
    // only select the option if it's not already selected
    if FSelected <> i then SelectThisOption(i);
    Exit;
    end;
    end;[/pascal]
    Bearing in mind that the above snippet can't be plugged in without some changes, of course. You can take this further: if you're going to use images instead of plain text strings then it won't necessarily be in a rectangle. In that case, you can store a mask and have an additional check to see if the user has clicked a non-transparent pixel. Only do this check if it's inside the bounding rectangle, though, since it may be a little slower (depending on how you implement the check).

    The next step is to make the GUI more interesting. You could make the options themselves into a separate class, rather than storing them as only strings. You'd give the options save/load methods, plus background pictures and other handy properties.

    Think about adding events (OnSelect, OnUnselect, OnMouseEnter/Move/Exit, OnKeyPress, and so on). These can be done in the usual fashion.

    The important point: game menus only need to be as complicated as you want. You can simply loop over an array of strings with an integer value to say what the current one is, or you can generate a full-blown GUI system. It's up to you.

    EDIT: added an array property so that you can fiddle with the option strings.
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

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
  •