TGB Card Deck Example
by David Higgins · 01/12/2007 (7:23 pm) · 7 comments
I just thought I'd post a cleaned up version of this code, along with the image used that allows the code to function properly.
This code allows you to create an ImageMap from the image below (or an image laid out the same way) and turn it into a usable Card Deck for just about any TGB related card game.
The below code is an example that displays some of the cards in a spread, dealt from the deck (top card out) and spread across the screen nicely.
To help explain the code, here's the imageMap I used:

Here is the ImageMapDataBlock that builds the deckImageMap referenced in the code above, using the PNG file above.

Update: Fixed a bug in the image map code, thanks Matthew Harris for pointing it out.
Update: Added example usage code and ImageMapDataBlock
Update: Updated the clearSimSet() function -- thanks again to Matt Harris for pointing out the flaw.
This code allows you to create an ImageMap from the image below (or an image laid out the same way) and turn it into a usable Card Deck for just about any TGB related card game.
$CARD_TYPE_CLUB = 0;
$CARD_TYPE_HEART = 1;
$CARD_TYPE_DIAMOND = 2;
$CARD_TYPE_SPADE = 3;
// $CARD_VALUE_1 -> $CARD_VALUE_10 can be created, or just assumed Static ;)
// the $CARD_VALUE_J-A is useful in case statements
// to perform specific logic based on those cards, usually 1-10 are not
// special in any way, so there's no need to pre-define them for code readability
$CARD_VALUE_J = 11;
$CARD_VALUE_Q = 12;
$CARD_VALUE_K = 13;
$CARD_VALUE_A = 1; // Ace is first, or last? :)
datablock t2dSceneObjectDatablock(CardDataBlock)
{
cardFace = $CARD_TYPE_HEART;
cardValue = $CARD_VALUE_A;
};
function CreateNewDeck(%sort, %scenegraph)
{
%cardDeck = new SimSet();
for(%x = 0; %x < 4; %x++)
{
for(%y = 1; %y < 14; %y++)
{
%card = new t2dStaticSprite()
{
defaultConfig = CardDataBlock;
scenegraph = %scenegraph;
size = "8.875 12.000";
imageMap = deckImageMap;
cardFace = %x;
cardValue = %y;
//cardSomething = %y;
//cardAnotherThing = %x;
position = deckHolder.getPosition();
frame = (%y - 1) + (%x * 13);
};
echo("Adding: " @ %card.cardSomething @ " -- " @ %card.cardFace);
%cardDeck.add(%card);
}
}
if(%sort $= "" || !%sort) return %cardDeck;
echo("Sorting Deck...");
// now to randomize the card deck
%cardDeckSorter = %cardDeck;
%cardDeck = new SimSet();
while(%cardDeckSorter.getCount() > 0)
{
%card = %cardDeckSorter.getObject(getRandom(0, %cardDeckSorter.getCount()-1));
%cardDeck.add(%card);
%cardDeckSorter.remove(%card);
}
return %cardDeck;
}
function clearSimSet(%set)
{
[b]while(%set.getCount() > 0)[/b]
{
%obj = %set.getObject(0);
%set.remove(%obj);
%obj.delete();
}
}
function dealNext(%deck)
{
%card = %deck.getObject(0);
%deck.remove(%card);
return %card;
}The below code is an example that displays some of the cards in a spread, dealt from the deck (top card out) and spread across the screen nicely.
echo("SCENEGRAPH: " @ sceneWindow2D.getSceneGraph());
$cardDeck = CreateNewDeck(true, sceneWindow2D.getSceneGraph());
// generic code to display the codes in a spread
// to show how dealing works -- echos to the console
// to show the cardValue and cardFace values -- should
// match the card spread
$lastX = -40;
$lastY = -9;
for(%x = 0; %x < 3; %x++)
{
for(%y = 0; %y < 7; %y++)
{
%card = dealNext($cardDeck);
%card.setPosition($lastX, $lastY);
%face = "Club";
switch(%card.cardFace)
{
case $CARD_TYPE_CLUB: %face = "Club";
case $CARD_TYPE_DIAMOND: %face = "Diamond";
case $CARD_TYPE_HEART: %face = "Heart";
case $CARD_TYPE_SPADE: %face = "Spade";
}
echo("Card: " @ %card.cardValue @ " -- Face: " @ %face);
$lastX += 1 + getWord(%card.getSize(), 1);
}
$lastX = -40;
$lastY += 4 + getWord(%card.getSize(),0);
}To help explain the code, here's the imageMap I used:

Here is the ImageMapDataBlock that builds the deckImageMap referenced in the code above, using the PNG file above.
new t2dImageMapDatablock(deckImageMap) {
imageName = "TGBCardDemo/Data/images/deck";
imageMode = "CELL";
frameCount = "-1";
filterMode = "SMOOTH";
filterPad = "1";
preferPerf = "1";
cellRowOrder = "1";
cellOffsetX = "0";
cellOffsetY = "0";
cellStrideX = "0";
cellStrideY = "0";
cellCountX = "13";
cellCountY = "4";
cellWidth = "71";
cellHeight = "96";
preload = "1";
allowUnload = "0";
};
Update: Fixed a bug in the image map code, thanks Matthew Harris for pointing it out.
Update: Added example usage code and ImageMapDataBlock
Update: Updated the clearSimSet() function -- thanks again to Matt Harris for pointing out the flaw.
About the author
#2
01/13/2007 (8:20 am)
This is Great I have been l;ooking into how this would be done. thanks so much for this contribution.
#3
01/13/2007 (5:55 pm)
Thank you David for the work, this will come in handy when I finally start working on my card game. :)
#4
01/14/2007 (1:19 am)
man now I feel even more like I should turn my little project into a resource, nice post
#5
01/14/2007 (6:47 am)
Awesome! Thanks for this! It's so weird that I was just starting to work on a card game and having some trouble and then you go and release help without even asking. This community is great! :D
#6
01/15/2007 (3:26 pm)
Go Mr. Higgins Go! Hey David, its your B-Day we gonna party like its your B-Day! Well done hehe.
#7
01/16/2007 (12:29 am)
Awesome! Be sure to submit as a resource.
Associate Andy Hawkins
Default Studio Name