Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What component to derive my 'TCard' from? (game)

I am trying to make a TCard component for a game. What class should I derive it from?

This is for a card game like MTG or yu gi oh. The card should have a blank image, and when created it will load either a front or back view.

If it loads the front view, it will then have to have a few labels (for properties like power/cost/def/text). Cards must be clickable.

 type
   TCard = class(zzzzzzzzz)
   private

Now once that is done, do I have to add anything to the constructor/destructor? Currently I have:

constructor TCard.Create(AOwner: Tcomponent);
begin
  inherited Create(AOwner);
end;

{******************************************************************************}
{ Free any resources allocated to component                                    }
destructor TCard.Destroy;
begin
  inherited Destroy;
end;

Also I think I added the onclick parts right but not sure. In the published area I have

 {Inherited properties}

 property OnMouseDown;
 property OnMouseMove;
 property OnMouseUp;
 property OnClick;
 property OnDblClick;

etc...

like image 336
Glen Morse Avatar asked Dec 03 '25 21:12

Glen Morse


1 Answers

It depends on what you want to do, but typically there are two ancestors for visible controls:

  • TGraphicControl: This is a descendant of TControl that implements a Canvas for you. You can just override the Paint method to start drawing. Controls like this support mouse interactions, but cannot get keyboard focus.

  • TCustomControl: This a descendant of TWinControl. It also implements a Canvas and allows you to override the Paint method to draw any content. Because it descends from TWinControl, it actually has a handle and can gain keyboard focus and process keyboard messages.

An other good candidate is TPanel (or rather TCustomPanel). It inherits from TCustomControl, so it has the same graphical properties, but it also contains functionality to draw borders and align child controls. I doubt if you would need this, though.

Of course you can derive directly from TControl or TWinControl, but then you will have to take care of some of this stuff yourself.

Note that it is better to put the actual card game logic in a separate class and only create visual controls for drawing. If you do that, you can still choose whether you want to have separate controls for each card, or you can choose to draw your whole card game on a single control or even directly on the form. I doubt if Windows' card games like Free Cell and Solitaire have over 50 graphics controls.

like image 195
GolezTrol Avatar answered Dec 05 '25 23:12

GolezTrol